Using an 'IN' operator with a SQL Command Object and C# 2.0

后端 未结 3 791
离开以前
离开以前 2021-01-19 05:15

I would like to call a sql statement such as:

Select * From Table Where Column in (\'value1\', \'value2\', \'value3\')

Is it as simple as s

3条回答
  •  臣服心动
    2021-01-19 05:36

    Another option is to set the SqlCommand's commandtype to "text" and construct the entire Sql string in code... Assuming Column is a varchar, and you have the Values in a string arrray, named "paramValues"

           StringBuilder sbSql = new StringBuilder
                       ("Select * From Table Where Column in (");
           string[] paramValues = new string[] {"value1", "value2", "value3"};
           foreach (string val in paramValues)
              sbSql.Append("'" + val + "', ");
           sbSql = sbSql.Remove(sbSql.Length - 2, 2);
           sbSql.Append(")");
    
           SqlCommand cmd = new SqlCommand(sbSql.ToString());
           cmd.CommandType = CommandType.Text; 
    

提交回复
热议问题