gpt4 book ai didi

c# - 更新表格

转载 作者:行者123 更新时间:2023-11-29 19:16:16 27 4
gpt4 key购买 nike

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

这就是我想要发生的事情:In = (Quantity*Unit_Value)and 应添加到当前值。 Unit_Value = textBox3.Text(Unit_Value是组合框中数据库中的固定值,我通过组合框中所选项目的名称获取它)。

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

最佳答案

这样做:

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

关于c# - 更新表格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42684438/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com