2010-11-23 36 views
0

我在C#中的新手,与大厦C#帮助where子句来查询

我想建立查询字符串,我做了一些条件,每个条件添加另一个条件where子句

我想类似的东西:

// BUILD SELECT QUERY 
    string where = ""; 
    string[] where_arr = new string[]; 
    if (condition1) 
    { 
      where_arr[index++] = " field = 5 "; 
    } 
     if (condition2) 
    { 
      where_arr[index++] = " field2 = 7 "; 
    } 

    if (where_arr.Count>0) 
     where = " where" + String.Join(" and ", where_arr); 
    string sql = "select count(*) as count from mytable " + where; 

,但我不知道究竟如何声明所有的变量,如where_arr

回答

1
// BUILD SELECT QUERY 
string where = ""; 
List<string> where_arr = new List<string>(); 

if (condition1) 
{ 
    where_arr.Add(" field = 5 "); 
} 

if (condition2) 
{ 
    where_arr.Add(" field2 = 7 "); 
} 

if (where_arr.Count > 0) 
    where = " where" + String.Join(" and ", where_arr.ToArray()); 
string sql = "select count(*) as count from mytable " + where; 
+0

谢谢你,它的工作 – 2010-11-23 20:25:14