0

我想根据以前的库存状态更新我的库存数量。我需要根据向内或向外交易通过添加或减少它来更新以前的数据。我没有在 currentstock 表中更新库存。//这是问题部分的开始

        SqlCommand cmd2 = new SqlCommand("select stock from currentstock where itemname=@a", con);
        con.Open();
        int x=0;
        cmd2.Parameters.AddWithValue("@a",comboBox2.SelectedItem.ToString());
        SqlDataReader dr = cmd2.ExecuteReader();
        if (dr.Read())
        {
             x = dr.GetInt16(2);
        }
        if (radioButton1.Checked == true)
        {
            x = x + Convert.ToInt16(textBox1.Text);
        }
        else if (radioButton2.Checked == true)
        {
            x = x - Convert.ToInt16(textBox2.Text);
        }
            con.Close();



        SqlCommand cmd1 = new SqlCommand("update currentstock set stock=@a where itemname=@b",con);
        con.Open();
        cmd1.Parameters.AddWithValue("@a", x);
        cmd1.Parameters.AddWithValue("@b", comboBox2.SelectedItem.ToString());
        cmd1.ExecuteNonQuery();
        con.Close();

// 这是问题段的结尾

4

1 回答 1

0

首先,在与您的 sql 表交谈时,请不要直接使用用户输入的值(请参阅SQL 注入)。使用存储过程。

我希望您从组合框中返回的值不是表的 itemname 列中的有效值。

于 2014-04-26T14:46:23.843 回答