簡體   English   中英

將“數量”值從程序更新到數據庫

[英]Updating “Quantity” value from program to the database

我在這里有問題。

這是我的情況:

我想根據程序給定的值更新程序中的“ Quantity值,並將其減去到數據庫中。 例如:我的數據庫中的Quantity100 ,一旦運行程序並將“ Quantity更新為50 ,數據庫中的“ Quantity ”應為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");
                }

但是,當我檢查數據庫時,無論我在程序中給定的值是什么,並且newVal保持為0Quantity值都更改為0 因此,我認為因為newVal被保持為0 ,然后數據庫識別出了它,並根據newVal更新了它,因此這段代碼似乎不起作用:

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

在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 ,並在以后進行了更新。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM