我有一个名为Product的数据库表。在里面,有ID,产品名称,品牌,描述,价格,成本和数量。我想使用文本框更新Count值。
例如,如果我在文本框中输入一个值,并且Count下已经有10个,则更新后的值应该是文本框中的值+10。
以下是我的代码:
protected void btnAdd_Click(object sender, EventArgs e)
{
string productName = ddlName.Text;
string quantity = tbQuantity.Text;
string strConnectionString = ConfigurationManager.ConnectionStrings["TrimberlandConnectionString"].ConnectionString;
SqlConnection myConnect = new SqlConnection(strConnectionString);
string strCommandText = "Select ProductName FROM Product WHERE ProductName = @ProductName";
SqlCommand cmd = new SqlCommand(strCommandText, myConnect);
cmd.Parameters.AddWithValue("@ProductName", productName);
myConnect.Open();
SqlDataReader reader = cmd.ExecuteReader();
addStocks(productName, quantity);
reader.Close();
myConnect.Close();
}
private void addStocks(string productName, string quantity)
{
string strConnectionString = ConfigurationManager.ConnectionStrings["TrimberlandConnectionString"].ConnectionString;
SqlConnection myConnect = new SqlConnection(strConnectionString);
string strCommandText = "UPDATE Product SET Count = Count + 1 ";
SqlCommand cmd = new SqlCommand(strCommandText, myConnect);
cmd.Parameters.AddWithValue("@ProductName", productName);
cmd.Parameters.AddWithValue("@Count", quantity);
myConnect.Open();
int result = cmd.ExecuteNonQuery();
if (result > 0)
{
MessageBox.Show("Stocks have successfully been added.");
}
myConnect.Close();
}
答案 0 :(得分:2)
您的代码应为:
protected void btnAdd_Click(object sender, EventArgs e)
{
string productName = ddlName.Text;
string quantity = tbQuantity.Text;
string product_id = "";
string strConnectionString = ConfigurationManager.ConnectionStrings["TrimberlandConnectionString"].ConnectionString;
SqlConnection myConnect = new SqlConnection(strConnectionString);
string strCommandText = "Select ID FROM Product WHERE lower(ProductName) = lower(@ProductName)";
SqlCommand cmd = new SqlCommand(strCommandText, myConnect);
cmd.Parameters.AddWithValue("@ProductName", productName);
myConnect.Open();
product_id = cmd.ExecuteScalar() == null ? "" : cmd.ExecuteScalar().ToString();
myConnect.Close();
if (!String.IsNullOrEmpty(product_id))
addStocks(product_id, quantity);
}
private void addStocks(string productID, string quantity)
{
string strConnectionString = ConfigurationManager.ConnectionStrings["TrimberlandConnectionString"].ConnectionString;
SqlConnection myConnect = new SqlConnection(strConnectionString);
string strCommandText = "UPDATE Product SET Count = Count + @val where ID = @PID ";
SqlCommand cmd = new SqlCommand(strCommandText, myConnect);
cmd.Parameters.AddWithValue("@PID", productID);
cmd.Parameters.AddWithValue("@val", quantity);
try{
myConnect.Open();
cmd.ExecuteNonQuery();
MessageBox.Show("Stocks have successfully been added.");
}
catch(Exception ex){
//work with exception
}
finally{
myConnect.Close();
}
}
答案 1 :(得分:0)
我认为您要修改命令文本:
string strCommandText = "UPDATE Product SET Count = Count + 1 ";
到
string strCommandText = "UPDATE Product SET Count = Count + @count WHERE ProductName = @ProductName ";
这应该完全来自纯粹的功能性。
但为了使它更具弹性,你应该检查'数量'字符串,以确保它确实是一个数字。