2012-06-19 55 views
0

我在ASP.Net中構建一個Web服務,它向我發送一個房間列表。我如何設置sql查詢的參數可選?

參數是用逗號分隔的ID。

我將它們保存到一個字符串並構建一個sql select查詢。

當我發送所有4個參數,我一切工作正常,我得到一個結果。但是,當我發送少於4我得到一個錯誤。

System.Data.SqlClient.SqlException: Incorrect syntax near ')'. 

如何在sql查詢中設置我的參數可選,以便只選擇我輸入的值?

這裏是我到目前爲止的代碼:

internal static List<RAUM> Raum(string RAUMKLASSE_ID, string STADT_ID, string GEBAEUDE_ID, string REGION_ID) 
{ 
    List<RAUM> strasseObject = new List<RAUM>(); 
    string raumklasseid = RAUMKLASSE_ID; 
    string gebaudeid = GEBAEUDE_ID; 
    string stadtid = STADT_ID; 
    string regionid = REGION_ID; 

    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 IN (" + raumklasseid + ") AND STADT_ID IN (" + stadtid + ") AND GEBAEUDE_ID IN (" + gebaudeid + ") AND REGION_ID IN (" + regionid + ")", con)) 
    { 
     con.Open(); 

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

在此先感謝您的幫助。

+2

轉儲您的查詢,例如,你可能有這樣的:」 ... FROM RAUM r WHERE RAUMKLASSE_ID IN()...「這是無效的。只有當參數有一個值時,你才應該附加每個約束。 –

+0

我不知道你的意思。你能否向我解釋更多細節? –

+0

我用一些代碼添加了一個答案。 –

回答

1

試想參數REGION_ID是一個空字符串。查詢的那部分將是這樣的:

...AND REGION_ID IN()... 

由於AND REGION_ID IN (" + regionid + ")"regionid變量將用空字符串替換。這不是有效的SQL語法,所以你會得到這個異常。

聲明這樣的函數:

private static void AppendConstrain(StringBuilder query, string name, string value) 
{ 
    if (String.IsNullOrWhiteSpace(value)) 
     return; 

    if (query.Length > 0) 
     query.Append(" AND "); 

    query.AppendFormat("{0} IN ({1})", name, value); 
} 

然後改變你的代碼來構建這樣的查詢:

StringBuilder constrains = new StringBuilder(); 
AppendConstrain(contrains, "RAUMKLASSE_ID", RAUMKLASSE_ID); 
AppendConstrain(contrains, "GEBAEUDE_ID", GEBAEUDE_ID); 
AppendConstrain(contrains, "STADT_ID", STADT_ID); 
AppendConstrain(contrains, "REGION_ID", REGION_ID); 

StringBuilder query = 
    new StringBuilder("SELECT r.BEZEICHNUNG AS BEZEICHNUNG, r.ID AS ID FROM RAUM r"); 

if (constrains.Length > 0) 
{ 
    query.Append(" WHERE "); 
    query.Append(constrains); 
} 

using (SqlCommand cmd = new SqlCommand(query.ToString(), con)) 
{ 
    // Your code... 
} 
+0

謝謝我嘗試了您的代碼,但仍然收到相同的錯誤。我不知道爲什麼 –

+0

@ErayGeveci日誌query.ToString()和張貼在這裏。 –

+0

我不知道如何登錄服務器上的web服務。虐待搜索解決方案,併發布結果 –

0

它總是更好的方法來編寫存儲過程並傳遞參數。但在你的方法中,你應該分解你的查詢,因爲不確定值。所以,你的代碼是類似的東西..

測試它自己,我沒有檢查它

string raumklasseid = RAUMKLASSE_ID; 
     string gebaudeid = GEBAEUDE_ID; 
     string stadtid = STADT_ID; 
     string regionid = REGION_ID; 
     string whereClause = string.Empty; 

if (!string.IsNullorEmpty(raumklasseid)) 
{ 
    whereClause = "RAUMKLASSE_ID IN (" + raumklasseid + ")"; 
} 
if (!string.IsNullorEmpty(stadtid)) 
{ 
    if(string.IsNullorEmpty(whereClause) 
     whereClause = "STADT_ID IN (" + stadtid + ")"; 
    else 
     whereClause += "AND RSTADT_ID IN (" + stadtid + ")"; 
} 
if (!string.IsNullorEmpty(stadtid)) 
{ 
    if(string.IsNullorEmpty(whereClause) 
     whereClause = "STADT_ID IN (" + stadtid + ")"; 
    else 
     whereClause += "AND RSTADT_ID IN (" + stadtid + ")"; 
} 
if (!string.IsNullorEmpty(regionid)) 
{ 
    if(string.IsNullorEmpty(whereClause) 
     whereClause = "REGION_ID IN (" + regionid + ")"; 
    else 
     whereClause += "AND REGION_ID IN (" + regionid + ")"; 
} 

if(!string.IsNullorEmpty(whereClause) 
whereClause = "WHERE " + whereClause ; 

// now your cmd should be like that 

using (SqlCommand cmd = new SqlCommand(@"SELECT r.BEZEICHNUNG AS BEZEICHNUNG, r.ID AS ID FROM RAUM r " + whereClause , con))