更新数据库中的值
Updating Value In Database
我有一个名为 Product 的数据库 table。里面有 ID、ProductName、Brand、Description、Price、Cost 和 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();
}
我想你想修改命令文本:
string strCommandText = "UPDATE Product SET Count = Count + 1 ";
至
string strCommandText = "UPDATE Product SET Count = Count + @count WHERE ProductName = @ProductName ";
从纯功能的角度来看应该就这些了。
但为了使其更加防弹,您应该检查 'quantity' 字符串以确保它确实是一个数字。
您的代码应该是:
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();
}
}
我有一个名为 Product 的数据库 table。里面有 ID、ProductName、Brand、Description、Price、Cost 和 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();
}
我想你想修改命令文本:
string strCommandText = "UPDATE Product SET Count = Count + 1 ";
至
string strCommandText = "UPDATE Product SET Count = Count + @count WHERE ProductName = @ProductName ";
从纯功能的角度来看应该就这些了。
但为了使其更加防弹,您应该检查 'quantity' 字符串以确保它确实是一个数字。
您的代码应该是:
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();
}
}