简体   繁体   English

从程序到数据库的负数据

[英]Minus data from the program to the database

How do i minus data from the program to the database? 我如何减去程序中的数据到数据库?

I have quantity and description in my database. 我的数据库中有数量和描述。 The quantity will be 100, and the description of it will be A. 数量为100,其描述为A。

When i type in my program like this (after run the program): The quantity is 50. and the description of it is A. 当我这样输入程序时(运行该程序后):数量为50。它的描述为A。

The database: The quantity will be 50 (because minus with the database and my program "100 - 50 = 50", and the description still remains same as A. 数据库:数量为50(因为减去数据库和我的程序“ 100-50 = 50”,并且描述仍然与A相同。

How do i do that? 我怎么做?

Thanks in advance! 提前致谢!

Edit: 编辑:

I already did like this: 我已经这样做了:

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

But when i run the program, the quantity loaded exactly the same like in the database, but it can't change, when i type quantity is 50, the program will automatically return to 100 (which is same exactly the quantity in the database) 但是当我运行程序时,加载的数量与数据库中的数量完全相同,但是无法更改,当我键入数量为50时,程序将自动返回100(与数据库中的数量完全相同)

Why is it like that? 为什么会这样呢?

Note: for textbox quantity, i use the Numeric Up Down. 注意:对于文本框数量,我使用数字上向下。

Edit: The full code is on below: 编辑:完整的代码在下面:

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

    }
}

Here is the screenshot: 这是屏幕截图: 在此处输入图片说明在此处输入图片说明

"Code" displayed in the screenshot above is refer to the database, and also the "Quantity", whenever i type the code that already have in the database, the remaining box are filled up. 上面的屏幕快照中显示的“代码”指的是数据库,也指“数量”,每当我键入数据库中已有的代码时,其余的框就会被填满。 But, when i change the "Quantity" from 100 to 50, it is automatically like refresh the program to back to 100 again. 但是,当我将“数量”从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 (");

[This can't be your actual code because it is missing a closing quote and bracket. [这不是您的实际代码,因为它缺少右引号和括号。 Please always just copy and paste your actual code.] 请始终只复制并粘贴您的实际代码。]

WHERE [Code] IN (")

You are attempting to update where the Code is an empty string? 您正在尝试更新Code为空字符串的地方吗? It is far more likely to be NULL, so this is one reason why it won't update. 它更有可能为NULL,因此这是它不会更新的原因之一。 However, you haven't executed this cmd, so it won't change anything. 但是,您尚未执行此cmd,因此不会更改任何内容。

[Do you really intend to update ALL records which don't have a Code ?] [您是否真的打算更新所有没有Code记录?]

You should also print-out cmd and attempt to execute it from within Access as part of your debugging steps. 您还应该打印出cmd,并尝试在Access中从中执行它,作为调试步骤的一部分。

Lastly, why are you quoting the Quantity with apostrophes? 最后,为什么要在“ Quantity加上撇号? Presumably, it is a numeric field. 大概是一个数字字段。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM