查询表达式'[Code] IN(''中的语法错误(缺少运算符)

时间:2013-08-28 03:10:18

标签: c# database winforms ms-access sql-update

我在更新数据时遇到错误,错误说:“查询表达式中的语法错误(缺少运算符)'[代码] 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();

非常感谢!

2 个答案:

答案 0 :(得分:2)

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

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

答案 1 :(得分: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);
    }
}