查询表达式';中存在语法错误(缺少运算符);[代码]IN(';

本文关键字:运算符 IN 代码 错误 表达式 查询表 存在 查询 语法 | 更新日期: 2023-09-27 18:29:11

我在更新数据时遇到错误,错误显示:"查询表达式'[Code]in('中的语法错误(缺少运算符)。"

这是我在上出错的代码

OleDbDataReader dReader = cmd.ExecuteReader();

以下是出现错误的功能的完整代码:

private void UpdateQuantity()
        {
            int index = 0;
            string command = "UPDATE [Seranne] SET [Quantity] ='" + newVal + "' WHERE [Code] IN(";
            OleDbConnection conn = new OleDbConnection(connectionString);
            conn.Open();
            OleDbCommand cmd = new OleDbCommand(command, conn);
            cmd.Parameters.Add("Quantity", System.Data.OleDb.OleDbType.Integer);
            OleDbDataReader dReader = cmd.ExecuteReader();
            while (dReader.Read())
            {
                if (textBoxQuantityContainer[index].Value != 0 && textBoxQuantityContainer[index].Value >= Convert.ToDecimal(dReader["Quantity"].ToString()))
                {
                    newVal = Convert.ToDecimal(dReader["Quantity"].ToString()) - textBoxQuantityContainer[index].Value;
                }
                System.Media.SoundPlayer sound = new System.Media.SoundPlayer(@"C:'Windows'Media'Windows Notify.wav");
                sound.Play();
                MessageBox.Show("Updated Successfully", "Success");
                index += 1;
            }
            conn.Close();
            dReader.Close();
        }

这是前面的代码,其中的查询表达式"code IN":

string query = "SELECT [Quantity], [Description], [Price] FROM [Seranne] WHERE [Code] IN (";
                OleDbConnection conn = new OleDbConnection(connectionString);
                conn.Open();
                if (int.TryParse(this.textBoxCodeContainer[0].Text, out codeValue))
                {
                    query = query + codeValue.ToString();
                }
                for (int i = 1; i < 17; i++)
                {
                    if (int.TryParse(this.textBoxCodeContainer[i].Text, out codeValue))
                    {
                        query = query + "," + codeValue.ToString();
                    }
                }
                query = query + ")";
                OleDbCommand cmd = new OleDbCommand(query, conn);
                cmd.Parameters.Add("Code", System.Data.OleDb.OleDbType.Integer);
                cmd.Parameters.Add("Quantity", System.Data.OleDb.OleDbType.Integer);
                OleDbDataReader dReader;
                dReader = cmd.ExecuteReader();
                while (dReader.Read())
                {
                    if (textBoxCodeContainer[index].TextLength != 0)
                    {
                        this.textBoxQuantityContainer[index].Maximum = Convert.ToDecimal(dReader["Quantity"].ToString());
                        this.textBoxDescContainer[index].Text = dReader["Description"].ToString();
                        this.textBoxSubTotalContainer[index].Text = dReader["Price"].ToString();
                    }
                    index += 1;
                }
                conn.Close();
                dReader.Close();

谢谢大家!

查询表达式';中存在语法错误(缺少运算符);[代码]IN(';

好的,只需保留您的代码并更改它:

for (int i = 1; i < 17; i++) {
   if (int.TryParse(this.textBoxCodeContainer[i].Text, out codeValue))
   {
      query = query + codeValue + ",";
   }
}
query = query.TrimEnd(',') + ")";

在继续之前,最好用textBoxCodeContainer值创建整数列表,称为整数

然后

string command = "UPDATE [Seranne] SET [Quantity] =? WHERE [Code] IN(" + string.Join(", " , integers) +")";

生成如下的整数列表

List<int> integers = new List<int>();
foreach (var tb in textBoxCodeContainer)
{
    int codeValue;
    if (int.TryParse(tb.Text, out codeValue))
    {
        integers.Add(codeValue);
    }
}