2012-06-19 47 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))