[英]How to update a quantity of an item if it already exists in database
我有一個productstbl
表,其中包含Name
、 Quantity
和其他屬性。
當我想購買productstbl
表中已經存在的新商品時,我需要更新Quantity
。
SqlCommand cmdCount = new SqlCommand("SELECT COUNT(*) FROM Productstbl WHERE Name = @name", con);
con.Open();
cmdCount.Parameters.AddWithValue("@name", NameBox.Text);
int count = (int)cmdCount.ExecuteScalar();
con.Close();
if (count > 0)
{
SqlCommand cmd = new SqlCommand("SELECT Qunatity FROM Productstbl WHERE Name = @name, Quantity = @quantity", con);
con.Open();
cmd.Parameters.AddWithValue("@name", NameBox.Text);
int ExistingQTY = (int)cmdCount.ExecuteScalar(); // I get error here
}
cmd = new SqlCommand("INSERT INTO Historytbl (Name, Date, Price, Quantity, Total_Price, Sup_Name, Process, Retail_Price) VALUES (@name, @date, @price, @quantity, @total_price, @sup_name, @process, @retail_price)", con);
con.Open();
cmd.Parameters.AddWithValue("@name", NameBox.Text);
cmd.Parameters.AddWithValue("@date", dateTimePicker1.Text);
cmd.Parameters.AddWithValue("@price", PriceBox.Text);
cmd.Parameters.AddWithValue("@quantity", ExistingQTY);
cmd.Parameters.AddWithValue("@total_price", TotalPriceBox.Text);
cmd.Parameters.AddWithValue("@sup_name", comboBox1.Text);
cmd.Parameters.AddWithValue("@process", "buy");
cmd.Parameters.AddWithValue("@retail_price", RetailPriceBox.Text);
cmd.ExecuteNonQuery();
con.Close();
MessageBox.Show("Record inserted successfully");
DisplayData();
ClearData();
如果要更新一個產品的數量,如果數據庫已經存在,最好從數據庫中獲取數量。
我做了一個代碼示例,你可以看看。
private void Form1_Load(object sender, EventArgs e)
{
comboBox1.Items.Add("Juice");
comboBox1.Items.Add("Apple");
comboBox1.Items.Add("Milk");
ShowData();
}
public void ShowData()
{
string connstr = "connstr";
SqlConnection connection = new SqlConnection(connstr);
connection.Open();
string cmd = "select * from Product";
DataSet set = new DataSet();
SqlDataAdapter adapter = new SqlDataAdapter(cmd, connection);
adapter.Fill(set);
dataGridView1.DataSource = set.Tables[0];
}
private void button1_Click(object sender, EventArgs e)
{
string connstr = "connstr";
SqlConnection connection = new SqlConnection(connstr);
connection.Open();
SqlCommand cmdCount = new SqlCommand("SELECT COUNT(*) FROM Product WHERE Name = @Name", connection);
cmdCount.Parameters.AddWithValue("@Name", comboBox1.SelectedItem.ToString());
int count = (int)cmdCount.ExecuteScalar();
int i = 0;
if(count>0)
{
SqlCommand command = new SqlCommand("SELECT Quantity FROM Product WHERE Name = @Name",connection);
command.Parameters.AddWithValue("@Name", comboBox1.SelectedItem.ToString());
using (SqlDataReader reader = command.ExecuteReader())
{
while (reader.Read())
{
i = Convert.ToInt32(reader["Quantity"]);
}
}
SqlCommand updatecmd = new SqlCommand("Update Product set Quantity=@Quantity, Date=@Date where Name=@Name",connection);
updatecmd.Parameters.AddWithValue("@Quantity", i + numericUpDown1.Value);
updatecmd.Parameters.AddWithValue("@Date", DateTime.Now);
updatecmd.Parameters.AddWithValue("@Name", comboBox1.SelectedItem.ToString());
updatecmd.ExecuteNonQuery();
dataGridView1.DataSource = null;
ShowData();
MessageBox.Show("Update successfully");
}
else
{
SqlCommand insertcmd = new SqlCommand("insert into Product(Name,Quantity,Date,Price)values(@Name,@Quantity,@Date,@Price)",connection);
insertcmd.Parameters.AddWithValue("Name", comboBox1.SelectedItem.ToString());
insertcmd.Parameters.AddWithValue("@Quantity",numericUpDown1.Value);
insertcmd.Parameters.AddWithValue("@Date",DateTime.Now);
insertcmd.Parameters.AddWithValue("Price",Convert.ToInt32(textBox1.Text));
insertcmd.ExecuteNonQuery();
dataGridView1.DataSource = null;
ShowData();
MessageBox.Show("Insert successfully");
}
connection.Close();
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if(comboBox1.SelectedItem.ToString()== "Juice")
{
textBox1.Text = "10";
}
if (comboBox1.SelectedItem.ToString() == "Apple")
{
textBox1.Text = "5";
}
if (comboBox1.SelectedItem.ToString() == "Milk")
{
textBox1.Text = "8";
}
}
測試結果:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.