2012-06-19 32 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方法中的字符串。

相關問題