c# - 如何在sql查询中使用逗号分隔的动态字符串?

标签 c# asp.net sql web-services sql-server-2008

你好,我在 visual studio 2010 中构建了一个网络服务。我得到了一些保存在字符串中的 id,如下所示:

string room_ids="5,11,99,42";

它们之间用逗号隔开。我创建了一个 foreach 循环来拆分 id 和逗号,并在我的 sql 查询中使用它们,直到 id 完成。但它不起作用。我收到一条错误消息:

将数据类型 nvarchar 转换为数字时出错

这是我的代码,在此先感谢您的帮助!

internal static List<RAUM> Raum(string RAUMKLASSE_ID, string STADT_ID, string GEBAEUDE_ID, string REGION_ID)
{       
    List<RAUM> strasseObject = new List<RAUM>();
    string[] allegebaude = GEBAEUDE_ID.Split(new char[] { ',' });

    foreach (string gebaudeid in allegebaude)
    {
        Trace.WriteLine("SIND JETZT DRINNE");
        Trace.WriteLine(gebaudeid);

        using (SqlConnection con = new SqlConnection(@"Data Source=Localhost\SQLEXPRESS;Initial Catalog=BOOK-IT-V2;Integrated Security=true;"))
        using (SqlCommand cmd = new SqlCommand(@"SELECT r.BEZEICHNUNG AS BEZEICHNUNG, r.ID AS ID FROM RAUM r WHERE RAUMKLASSE_ID = ISNULL(@Raumklasse_ID, RAUMKLASSE_ID) AND STADT_ID = ISNULL(@Stadt_ID, STADT_ID) AND GEBAEUDE_ID = ISNULL(@gebaudeid,GEBAEUDE_ID ) AND REGION_ID = ISNULL(@Region_ID, REGION_ID)", con))
        {
            con.Open();
            if (!StringExtensions.IsNullOrWhiteSpace(RAUMKLASSE_ID))
                cmd.Parameters.AddWithValue("@Raumklasse_ID", RAUMKLASSE_ID);
            else
                cmd.Parameters.AddWithValue("@Raumklasse_ID", DBNull.Value);

            if (!StringExtensions.IsNullOrWhiteSpace(STADT_ID))
                cmd.Parameters.AddWithValue("@Stadt_ID", STADT_ID);
            else
                cmd.Parameters.AddWithValue("@Stadt_ID", DBNull.Value);

            if (!StringExtensions.IsNullOrWhiteSpace(GEBAEUDE_ID))
                cmd.Parameters.AddWithValue("@gebaudeid", GEBAEUDE_ID);
            else
                cmd.Parameters.AddWithValue("@gebaudeid", DBNull.Value);

            if (!StringExtensions.IsNullOrWhiteSpace(REGION_ID))
                cmd.Parameters.AddWithValue("@Region_ID", REGION_ID);
            else
                cmd.Parameters.AddWithValue("@Region_ID", DBNull.Value);



            using (SqlDataReader rdr = cmd.ExecuteReader())
            {
                while (rdr.Read())
                {
                    if (rdr["BEZEICHNUNG"] != DBNull.Value && rdr["ID"] != DBNull.Value)
                    {
                        strasseObject.Add(new RAUM()
                        {
                            RaumName = rdr["BEZEICHNUNG"].ToString(),
                            RaumID = rdr["ID"].ToString()

                        });
                    }
                }
            }

        }
    }
    return strasseObject;
}

最佳答案

如果您已经拥有以逗号分隔的字符串形式的 ID(称为 IDstring),那么您可以这样做:

sqlQuery = "SELECT Columns FROM table WHERE ID IN (" + IDstring + ")";

在您的特定情况下,不要拆分原始字符串 (GEBAEUDE_ID),而是按原样使用它:

   // Don't use a foreach loop any more
   string gebaudeIdSection = " AND GEBAEUDE_ID IN (" + GEBAEUDE_ID + ") ";
   if (string.IsNullOrEmpty(GEBAUDE_ID)) { gebaudeIdSection = ""; } // if there are no ids, let's remove that part of the query completely.
   using (SqlConnection con = new SqlConnection(@"Data Source=Localhost\SQLEXPRESS;Initial Catalog=BOOK-IT-V2;Integrated Security=true;"))
   using (SqlCommand cmd = new SqlCommand(@"SELECT r.BEZEICHNUNG AS BEZEICHNUNG, r.ID AS ID FROM RAUM r WHERE RAUMKLASSE_ID = ISNULL(@Raumklasse_ID, RAUMKLASSE_ID) AND STADT_ID = ISNULL(@Stadt_ID, STADT_ID)" + gebaudeIdSection + " AND REGION_ID = ISNULL(@Region_ID, REGION_ID)", con))
   { // The rest of the code is the same as before...

关于c# - 如何在sql查询中使用逗号分隔的动态字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11096117/

相关文章:

c# - 我如何在我的 ASP MVC 应用程序的 View 中调用 javascript 函数

c# - 如何在 Azure 中以编程方式接收电子邮件?

c# - 我不可见的下拉列表在 visual studio 中不再可见

c# - SqlConnection 问题,ASP.net C#

java - 检查where条件下不为空

c# - Enumerable.Contains 与 MethodInfo

c# - 使用 Silverlight 捕获音频

C# 图形.CopyFromScreen "parameter is not valid"

c# - "Timeout while getting a connection from pool."Hangfire.Postgres

sql - 是否可以在 postgresql 命令中引用环境变量?