Skip to main content
 首页 » 编程设计

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

2024年02月20日21落叶

你好,我在 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...