0

更新数据时出现错误,错误提示:“查询表达式'[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();

非常感谢!

4

2 回答 2

2

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

for (int i = 1; i < 17; i++) {
   if (int.TryParse(this.textBoxCodeContainer[i].Text, out codeValue))
   {
      query = query + codeValue + ",";
   }
}
query = query.TrimEnd(',') + ")";
于 2013-08-28T03:12:16.550 回答
1

在继续之前,您最好从 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);
    }
}
于 2013-08-28T03:16:51.553 回答