0

我这里有问题。

这是我的情况:

我想Quantity根据程序给出的值更新程序中的值并将其减去到数据库中。例如:我100Quantity数据库中,一旦我运行程序并更新Quantity50Quantity数据库中的应该是50.

这是我的问题:

我已经可以将Quantity值从程序更新到数据库,但是无论我Quantity在程序中赋予的值是什么Quantity,数据库中的值总是更新为 0。

这是代码:

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

            List<int> integers = new List<int>();

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

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

            OleDbConnection conn = new OleDbConnection(connectionString);

            OleDbDataReader dReader;

            OleDbCommand cmd = new OleDbCommand(command, conn);

            conn.Open();

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

            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;
                    cmd.ExecuteNonQuery();
                }

                index += 1;
            }

            if (newVal == 0)
            {
                System.Media.SoundPlayer sounds = new System.Media.SoundPlayer(@"C:\Windows\Media\Windows Notify.wav");
                sounds.Play();
                MessageBox.Show("Cannot Update", "Error");
            }

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

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

newVal值保持出现0。因为newVal保持 0,所以当我单击程序中的“更新”按钮时,程序会显示这个:

if (newVal == 0)
                {
                    System.Media.SoundPlayer sounds = new System.Media.SoundPlayer(@"C:\Windows\Media\Windows Notify.wav");
                    sounds.Play();
                    MessageBox.Show("Cannot Update", "Error");
                }

但是当我签入数据库时​​,无论我在程序中给出的值是什么,都会将Quantity值更改为keep 。所以,我认为是因为一直出现,然后数据库识别它并根据它更新它,这段代码似乎不起作用:0newVal0newVal0newVal

if (textBoxQuantityContainer[index].Value != 0 && textBoxQuantityContainer[index].Value >= Convert.ToDecimal(dReader["Quantity"].ToString()))
                    {
                        newVal = Convert.ToDecimal(dReader["Quantity"].ToString()) - textBoxQuantityContainer[index].Value;
                        cmd.ExecuteNonQuery();
                    }

你们能帮帮我吗?提前致谢!

编辑代码:

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

        List<int> integers = new List<int>();

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

        OleDbConnection conn = new OleDbConnection(connectionString);

        OleDbDataReader dReader;

        OleDbCommand cmd = new OleDbCommand(command, conn); // error: use of unassigned local variable, if i put "string command = '' ", the program will run, but the command not recognized by system, and the error will be: Command text was not set for the command object.

        conn.Open();

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

        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;
                command = "UPDATE [Seranne] SET [Quantity]= " + newVal + " WHERE [Code] IN(" + string.Join(", ", integers) + ")";
                cmd.ExecuteNonQuery();
            }

            index += 1;
        }

        if (newVal == 0)
        {
            System.Media.SoundPlayer sounds = new System.Media.SoundPlayer(@"C:\Windows\Media\Windows Notify.wav");
            sounds.Play();
            MessageBox.Show("Cannot Update", "Error");
        }

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

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

1 回答 1

1

在while循环中像这样更改以下内容,

      newVal = Convert.ToDecimal(dReader["Quantity"].ToString()) - textBoxQuantityContainer[index].Value;                
      string command = "UPDATE [Seranne] SET [Quantity]= " + newVal + " WHERE [Code] IN(" + string.Join(", ", integers) + ")";
      cmd.ExecuteNonQuery();

在您的 while 循环之后,因为“newVal”是0在查询中使用并稍后更新的。

于 2013-08-29T07:31:54.713 回答