更新表格

时间:2017-03-09 00:18:32

标签: c# mysql

嗨,我只是这个语言C#和MySQL的新手......我不知道为什么InUnit_Value的值仍为0
我想更新列,每当我添加一个Item时,该项的数量应该添加到当前值。

这就是我想要发生的事情: In =(Quantity * Unit_Value)并且应该添加到当前值。 Unit_Value = textBox3.Text(Unit_Value是组合框中数据库的固定值,我通过组合框中的selecteditem名称得到它。)

Screenshot of UI

Table data

这是我的代码:

string constring1 = "datasource=localhost;port=;username=;password=;database=;";
MySqlConnection conDataBase1 = new MySqlConnection(constring1);
MySqlCommand mycommand = new MySqlCommand("select `Unit_Value` from `tbl_inventory_item_unit` where `Unit_Name`='"+metroComboBox2.Text+"';",conDataBase1);
MySqlCommand myincommand = new MySqlCommand (
    "UPDATE `tbl_inventory_in` SET `In`= `In` + '" + metroLabel12.Text + 
    "',`In_Quantity`='" + textBox3.Text + 
    "',`Unit_Value`='" + metroLabel11.Text + 
    "',`Unit_Name`='"+ metroComboBox2.Text + 
    "',`In_Date`='"+ metroLabel8.Text + 
    "'where `Item_ID`='" + textBox4.Text + 
    "';",conDataBase1);

string b;
int c;
int a;

try {
    conDataBase1.Open();
    MySqlDataReader myReader1 = mycommand.ExecuteReader();
    while (myReader1.Read()) {
        b = myReader1.GetValue(0).ToString();
        // c = Int32.Parse(b);
        // c = Convert.ToInt32(c);
        c = Int32.Parse(b);
        a = Int32.Parse(textBox3.Text);
        metroLabel11.Text = c.ToString();
        metroLabel12.Text = (a * c).ToString();
    }
    myReader1.Close();
    MySqlDataReader myinreader = myincommand.ExecuteReader();
    while (myinreader.Read()) {
    }
    MessageBox.Show("Stocks Added");
    conDataBase1.Close();
}

1 个答案:

答案 0 :(得分:0)

这样做:

String connectionString = "This is your connection string";  
public static int updateTable(string query)
        {

            using (MySqlConnection con = new MySqlConnection(connectionString))
            {
                con.Open();
                using (MySqlCommand cmd = con.CreateCommand())
                {
                   cmd.CommandText = query;
                    cmd.CommandType = CommandType.Text;
                    int result = cmd.ExecuteNonQuery();
                    return result;
                }
            }
        }

To use this:
 updateTable("update yourtable set column1=value1, column2 = value2 where primarycolumn = primaryKeyValue");