Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/264.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 更新「;数量“;从程序到数据库的值_C#_Database_Winforms_Ms Access - Fatal编程技术网

C# 更新「;数量“;从程序到数据库的值

C# 更新「;数量“;从程序到数据库的值,c#,database,winforms,ms-access,C#,Database,Winforms,Ms Access,我这里有个问题 我的情况如下: 我想根据程序给出的值更新程序中的数量值,并将其减去数据库。例如:我在数据库中的Quantity中有100,一旦我运行程序并将Quantity更新为50,数据库中的Quantity应该是50 我的问题是: 我已经可以将Quantity值从程序更新到数据库,但无论我在程序中给Quantity的值是什么,数据库中的Quantity值始终更新为0。 代码如下: private void UpdateQuantity() { int

我这里有个问题

我的情况如下:

我想根据程序给出的值更新程序中的
数量
值,并将其减去数据库。例如:我在数据库中的
Quantity
中有
100
,一旦我运行程序并将
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();
        }
但是当我检查数据库时,
数量
值更改为
0
,无论程序中给我的值是什么,以及
newVal
保留
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();
    }
private void UpdateQuantity()
{
int指数=0;
int codeValue=0;
字符串命令;
列表整数=新列表();
foreach(textBoxCodeContainer中的变量tb)
{
if(int.TryParse(tb.Text,out code值))
{
整数。相加(codeValue);
}
}
OLEDB连接连接=新的OLEDB连接(连接字符串);
OLEDB阅读恐惧症;
OleDbCommand cmd=new-OleDbCommand(command,conn);//错误:使用未分配的局部变量,如果我输入“string command=”,程序将运行,但系统无法识别该命令,错误为:未为命令对象设置命令文本。
conn.Open();
cmd.Parameters.Add(“数量”,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+”,其中[Code]位于(“+string.Join”(“,”,integers)+”)中;
cmd.ExecuteNonQuery();
}
指数+=1;
}
如果(newVal==0)
{
System.Media.SoundPlayer sounds=新的System.Media.SoundPlayer(@“C:\Windows\Media\Windows Notify.wav”);
声音。播放();
MessageBox.Show(“无法更新”,“错误”);
}
其他的
{
System.Media.SoundPlayer sound=新的System.Media.SoundPlayer(@“C:\Windows\Media\Windows Notify.wav”);
声音。播放();
MessageBox.Show(“更新成功”、“成功”);
}
恐惧者。关闭();
康涅狄格州关闭();
}

在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
,并且在以后更新。

@Furqan:如果我这样做,它将被弄乱,因为
cmd.executeReader()
位于
string命令=..
的顶部或之前,如果我将此
OleDbCommand cmd=new OleDbCommand(命令,conn)
string command=..
之前,它给出了一个错误:
没有为命令对象设置命令文本。
请参阅问题编辑代码先生,thanks@FuhansPujiSaputra,我已经更新了答案,并在执行cmd之前移动了查询行,请现在检查。@Furqan:我不明白,如果我完全按照您在上面给我的建议进行操作,我会对这个命令做什么:
OleDbCommand cmd=new OleDbCommand()?因为那应该在
while循环之前
?如果我用
命令填充该命令,conn
,并生成一个变量
字符串命令应该是错误的,因为系统无法识别该命令(因为在while循环中,full命令在下方打开),如果我在顶部声明变量
OleDbCommand cmd
,并且full命令在while循环中,则
cmd.ExecuteReader
中的
cmd
无法识别。抱歉,arguingSir@Furqan,请再次参考问题编辑代码,我已经在那里更新了我的代码。非常感谢您,很抱歉与您争论。@FuhansPujiSaputra,它是否仍在更新0?
      newVal = Convert.ToDecimal(dReader["Quantity"].ToString()) - textBoxQuantityContainer[index].Value;                
      string command = "UPDATE [Seranne] SET [Quantity]= " + newVal + " WHERE [Code] IN(" + string.Join(", ", integers) + ")";
      cmd.ExecuteNonQuery();