0

我有问题。我想将数据更新到数据库,但数据库不会更新。

这是代码:

else if (firstForm.textBox1.Text == "Seranne")
            {
                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();
                    }

                    if (textBoxQuantityContainer[index].Value != 0 && textBoxQuantityContainer[index].Value >= Convert.ToDecimal(dReader["Quantity"].ToString()))
                    {
                        newVal = textBoxQuantityContainer[index].Value - Convert.ToDecimal(dReader["Quantity"].ToString());
                        cmd = new OleDbCommand("UPDATE [Seranne] SET [Quantity] ='" + newVal + "' WHERE [Code] IN ('");
                    }

                    index += 1;
                }

                conn.Close();
                dReader.Close();
            }
        }

        private void UpdateQuantity()
        {
            System.Media.SoundPlayer sound = new System.Media.SoundPlayer(@"C:\Windows\Media\Windows Notify.wav");
            sound.Play();
            MessageBox.Show("Updated Successfully", "Success");
        }

        private void button1_Click(object sender, EventArgs e)
        {
            UpdateQuantity();
        }

编辑:(下面的函数 UpdateQuantity 是用户单击更新按钮时),当我单击更新按钮时出现错误,这是错误:Syntax error (missing operator) in query expression '[Code] IN ('.

private void UpdateQuantity()
        {
            int index = 0;
            int codeValue = 0;

            string query = "SELECT [Quantity], [Description], [Price] FROM [Seranne] WHERE [Code] IN (";

            OleDbConnection conn = new OleDbConnection(connectionString);

            OleDbDataReader dReader;

            OleDbCommand cmd = new OleDbCommand(query, conn);

            conn.Open();

            cmd.Parameters.Add("Quantity", System.Data.OleDb.OleDbType.Integer);

            dReader = cmd.ExecuteReader();

            if (textBoxQuantityContainer[index].Value != 0 && textBoxQuantityContainer[index].Value >= Convert.ToDecimal(dReader["Quantity"].ToString()))
            {
                cmd = new OleDbCommand("UPDATE [Seranne] SET [Quantity] ='" + newVal + "' WHERE [Code] IN (", conn);

                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 + ")";

                newVal = textBoxQuantityContainer[index].Value - Convert.ToDecimal(dReader["Quantity"].ToString());
                cmd.ExecuteNonQuery();
                System.Media.SoundPlayer sound = new System.Media.SoundPlayer(@"C:\Windows\Media\Windows Notify.wav");
                sound.Play();
                MessageBox.Show("Updated Successfully", "Success");
            }

            index += 1;

            dReader.Close();
            conn.Close();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            UpdateQuantity();
        }

上面的代码都有效,除了将数量更新到数据库。我的意思是,我将数据库中的数量设置为 100,当我在程序中将数量设置为 10 并更新它时,数据库应该将数量更新为 90(因为 100 - 10),但它仍然是 100。

我会在某个地方出错吗?

这是截图的链接:(ScreenShot 1)https://www.dropbox.com/s/rph5iuh371rc9ny/Untitled.png

(屏幕截图 2)https://www.dropbox.com/s/5q8pyztqy7ejupy/Capture.PNG

在屏幕截图 1 中,我已经将数量设置为 10,并且消息框显示数据已成功更新,并且数据库中的数据应该是 90(因为 100-10)。但是,在数据库所在的屏幕截图 2 中,数量仍为 100。

提前致谢!

4

1 回答 1

0

您的更新查询

 cmd = new OleDbCommand("UPDATE [Seranne] SET [Quantity] ='" + newVal + "' WHERE [Code] IN ('");

您发布的代码中似乎没有完成。我的猜测是你得到了一个语法错误,你的代码没有捕获和显示。

于 2013-08-28T00:42:45.950 回答