如何在 IN 子句 sql informix 中使用参数
Posted
技术标签:
【中文标题】如何在 IN 子句 sql informix 中使用参数【英文标题】:How to use parameters in IN Clause sql informix 【发布时间】:2014-02-20 07:44:25 【问题描述】:如何在IN子句sql informix中使用参数
StringBuilder cmdTxt = new StringBuilder();
cmdTxt.Append(" SELECT COUNT(emp_num) ");
cmdTxt.Append(" FROM tbl1 WHERE year IS NULL AND calc_year = ? AND camp IN (,,,)");
using (var myIfxCmd = new IfxCommand(cmdTxt.ToString(), con))
myIfxCmd.CommandType = CommandType.Text;
myIfxCmd.Parameters.Add("calc_year", IfxType.Integer);
if (con.State == ConnectionState.Closed)
con.Open();
myIfxCmd.Parameters[0].Value = calcYear;
res = int.Parse(myIfxCmd.ExecuteScalar().ToString());
我不知道如何处理将参数传递给camp IN (,,,,)//Set of numbers
【问题讨论】:
【参考方案1】:我有这样的事情
IDbCommand cmd = connection.CreateCommand(); //connection has been instantiated
cmd.CommandTimeout = connection.ConnectionTimeout;
var query = @"SELECT COUNT(emp_num)
FROM tbl1 WHERE year IS NULL AND calc_year = @year AND camp IN (0)";
cmd.Parameters.Add(new SqlParameter("@year", calcYear));
var sb = new StringBuilder();
//ids is a list/array of ids i want in the in clause
for (int i = 0; i < ids.Count; i++)
sb.AppendFormat("@p0,", i);
cmd.Parameters.Add(new SqlParameter("@p" + i, ids[i]));
if (sb.Length > 0) sb.Length -= 1;
string cmdText = string.Format(query, sb.ToString());
cmd.CommandText = cmdText;
cmd.Connection = connection;
var reader = cmd.ExecuteReader();
【讨论】:
以上是关于如何在 IN 子句 sql informix 中使用参数的主要内容,如果未能解决你的问题,请参考以下文章