2012-06-19 33 views
0

你好,我建立一个Web服务在Visual Studio 2010中,我得到它被保存在一个字符串一些的id是这样的:如何在sql查询中使用以逗号分隔的动态字符串?

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

它们用逗号分隔。我创建了一个foreach循环来分割ID和逗号,并在我的SQL查询中使用它们,直到完成ID。但它不起作用。我得到一个错误,它说:

Error converting data type nvarchar to numeric

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

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; 
} 

回答

1

如果你已经有一个逗号分隔的字符串(称为IDstring)的ID,那么你可以做这样的事情:

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... 
+0

感谢您的帮助,我有一个小问题,我不能将gebaudeid设置为空,因为我的字符串是whith []。我该如何解决这个问题? –

+0

我不确定我的理解。你不用'gebaudeid'做任何事情。您需要使用传入函数的原始逗号分隔字符串“GEBAUDE_ID”,只需使用该字符串而不分割它。删除将字符串拆分为数组的代码位。 –

+0

它的工作原理,但当我让gebaude id空我得到一个错误:')'附近的语法不正确。 –

1

试试这个:

if (!StringExtensions.IsNullOrWhiteSpace(gebaudeid)) 
      cmd.Parameters.AddWithValue("@gebaudeid", Convert.ToInt32(gebaudeid)); 

你SH还应该将正确的参数传递给你的AddWithValue语句。您正在迭代您的ID列表,该列表为allegebaude。因此,您必须将gebaudeid参数传递给您的声明,而不是您现在正在执行的操作。

1

首先,我认为你必须纠正:

cmd.Parameters.AddWithValue("@gebaudeid", GEBAEUDE_ID); 

有:

cmd.Parameters.AddWithValue("@gebaudeid", gebaudeid); 

然后,尝试的ID转换成整数(例如,使用Convert.ToInt32(gebaudeid)),而不是通过他们作为AddWithValue方法中的字符串。

相关问题