簡體   English   中英

從程序到數據庫的負數據

[英]Minus data from the program to the database

我如何減去程序中的數據到數據庫?

我的數據庫中有數量和描述。 數量為100,其描述為A。

當我這樣輸入程序時(運行該程序后):數量為50。它的描述為A。

數據庫:數量為50(因為減去數據庫和我的程序“ 100-50 = 50”,並且描述仍然與A相同。

我怎么做?

提前致謝!

編輯:

我已經這樣做了:

if (textBoxCodeContainer[index].TextLength != 0)
                    {
                        this.textBoxQuantityContainer[index].Value = Convert.ToDecimal(dReader["Quantity"].ToString());
                        this.textBoxDescContainer[index].Text = dReader["Description"].ToString();
                        this.textBoxSubTotalContainer[index].Text = dReader["Price"].ToString();
                    }

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

但是當我運行程序時,加載的數量與數據庫中的數量完全相同,但是無法更改,當我鍵入數量為50時,程序將自動返回100(與數據庫中的數量完全相同)

為什么會這樣呢?

注意:對於文本框數量,我使用數字上向下。

編輯:完整的代碼在下面:

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

            if (firstForm.textBox1.Text == "Seranne")
            {
                string query = "SELECT [Quantity], [Description], [Price] FROM [Seranne] WHERE [Code] IN (";

                OleDbDataReader dReader;
                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);

                dReader = cmd.ExecuteReader();

                while (dReader.Read())
                {
                    if (textBoxCodeContainer[index].TextLength != 0)
                    {
                        this.textBoxQuantityContainer[index].Value = Convert.ToDecimal(dReader["Quantity"].ToString());
                        this.textBoxDescContainer[index].Text = dReader["Description"].ToString();
                        this.textBoxSubTotalContainer[index].Text = dReader["Price"].ToString();
                    }

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

                    index += 1;
                }

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

        private void UpdatePrice()
        {
            int totalPrice = 0;
            int quantity = 0;
            int price = 0;

            for (int i = 0; i < 17; i++)
            {
                if (textBoxQuantityContainer[i].Value > 0)
                {
                    quantity = (int)textBoxQuantityContainer[i].Value;
                    price = Convert.ToInt32(textBoxSubTotalContainer[i].Text);
                    textBoxTotalContainer[i].Text = (quantity * price).ToString();
                }

                else
                {
                    textBoxSubTotalContainer[i].Text = "";
                    textBoxTotalContainer[i].Text = "";
                }
            }

            for (int i = 0; i < 17; i++)
            {
                if (textBoxTotalContainer[i].TextLength != 0)
                {
                    totalPrice += Convert.ToInt32(textBoxTotalContainer[i].Text);
                }
            }

            textBoxAllTotalContainer.Text = totalPrice.ToString("n2");
        }

        private void textBox_TextChanged(object sender, EventArgs e)
        {
            UpdateDatas();
            UpdatePrice();
        }

    }
}

這是屏幕截圖: 在此處輸入圖片說明在此處輸入圖片說明

上面的屏幕快照中顯示的“代碼”指的是數據庫,也指“數量”,每當我鍵入數據庫中已有的代碼時,其余的框就會被填滿。 但是,當我將“數量”從100更改為50時,它就像自動刷新程序,再次回到100。

 cmd = new oledbcommand("select qty from tablename where pid=103");
qtyval = convert.ToInt32(cmd.ExecuteScalar());
if (qtyval != 0 )
{
if (qtyval >= convert.toint32(txtqty.text))
{
newval = qtyval - convert.ToInt32(txtqty.text);
cmd = new oledbcommand("update tablename set qty="+newval+" where pid=103");
}
}
cmd = new OleDbCommand("UPDATE [Seranne] SET [Quantity] ='" + newVal + "' WHERE [Code] IN (");

[這不是您的實際代碼,因為它缺少右引號和括號。 請始終只復制並粘貼您的實際代碼。]

WHERE [Code] IN (")

您正在嘗試更新Code為空字符串的地方嗎? 它更有可能為NULL,因此這是它不會更新的原因之一。 但是,您尚未執行此cmd,因此不會更改任何內容。

[您是否真的打算更新所有沒有Code記錄?]

您還應該打印出cmd,並嘗試在Access中從中執行它,作為調試步驟的一部分。

最后,為什么要在“ Quantity加上撇號? 大概是一個數字字段。

暫無
暫無

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

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