如何在IN子句sql informix中使用参数

时间:2014-02-20 07:44:26

标签: c# asp.net sql informix parameterized-query

如何在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 个答案:

答案 0 :(得分:3)

我有类似的东西

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("@p{0},", 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();