不,你不能传递一个字符串作为IN子句的参数。
但从你的数据库的点是喜欢它接收到这个指令
SqlCommand SqlString4 = new SqlCommand(@"Update quex set docudisp = 70
where docudisp = 60 and admindisp in (40,43,91)
and barcode in ('1,2,3,4,5')", con);
在这里,你可以尝试使用字符串连接生成SQL命令的情况下,只要你有超过值的完全控制在条码字符串
SqlCommand SqlString4 = new SqlCommand(@"Update quex set docudisp = 70
where docudisp = 60 and admindisp in (40,43,91)
and barcode in (" + barcode + ")", con);
还有this answer here on StackOverflow,显示技术使用的是使用表弗吉尼亚州一个参数(答案克服这个限制lued参数,而不是接受的参数)
但是,考虑到你知道IN子句中所需的参数数量,那么为什么不用特定的参数来构建一个具体的查询呢?
// Create the command outside the loop defining the 5 parameters required
SqlCommand SqlString4 = new SqlCommand(@"Update quex set docudisp = 70
where docudisp = 60 and admindisp in (40,43,91)
and barcode in (@b1, @b2, @b3, @b4, @b5)", con);
// Create the five parameters with a dummy integer
SqlString4.Parameters.AddWithValue("@b1", 0);
SqlString4.Parameters.AddWithValue("@b2", 0);
SqlString4.Parameters.AddWithValue("@b3", 0);
SqlString4.Parameters.AddWithValue("@b4", 0);
SqlString4.Parameters.AddWithValue("@b5", 0);
con.Open();
// Increment the loop with i+=5 because it is not clear
// if you have more that 5 items in the myCollection array
for (i = 0; i < totalbarcodes; i+=5)
{
SqlString4.Parameters["@b1"].Value = myCollection[i]);
SqlString4.Parameters["@b2"].Value = myCollection[i+1]);
SqlString4.Parameters["@b3"].Value = myCollection[i+2]);
SqlString4.Parameters["@b4"].Value = myCollection[i+3]);
SqlString4.Parameters["@b5"].Value = myCollection[i+4]);
try
{
SqlString4.ExecuteNonQuery();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
con.Close();
请注意,我已经改变了for循环的增量。如果你有收集更多的5个项目,你需要对其进行索引正确地将
您是否收到错误?这是否符合你的数据库 - 其中docudisp = 60,admindisp(40,43,91)和条形码(@barcode) - ? 即时消息只看到你更新一个值 - 设置docudisp = 70 请详细说明 – JanivZ