【问题标题】:Updating Value In Database更新数据库中的值
【发布时间】:2016-01-18 01:19:14
【问题描述】:

我有一个名为 Product 的数据库表。里面有 ID、ProductName、Brand、Description、Price、Cost 和 Count。我想使用文本框更新 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();
    }

【问题讨论】:

  • 有效吗?如果不是,有什么问题?您收到任何错误代码吗?在任何特定的行上?
  • @Ian 它可以工作,但它只会加 1。我需要它来添加我在文本框中键入的值。但我不确定如何修改查询。
  • @GrantWinney addStocks 中的 strCommandText。它有效,但我需要它来添加我在文本框中键入的值,而不仅仅是 +1。

标签: c# asp.net sql-server database shopping-cart


【解决方案1】:

你的代码应该是:

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

【讨论】:

  • 不要只是向我们倾倒大量代码,让我们找出您所做的更改 - 请EXPLAIN您更改了什么 - 以及原因。
  • 首先,我从数据库中获取 ProductID。如果确实存在,我将通过输入的值更新 Count 值,否则不做任何事情。此外,我正在更新所需产品的价值,而不是全部,因为它是关于问题的
  • @marc_s 很抱歉这样的短语,但如果您需要,我可以在代码中的每一行注释,我会在其中进行编辑。
  • 没关系 - 只需告诉我们类似的内容:我在 UPDATE 语句中添加了 WHERE 子句来更新特定产品 - 不是全部 - 这已经足够了.没有它,我们只能将您的代码与原始代码进行比较,并自己弄清楚您更改了哪些内容(有时可能不会这么明显)。您的回复是当场的并解决了问题 - 只需告诉我们您做了什么以及背后的原因是什么 - 那么回复就真的很完美了!
【解决方案2】:

我想你想修改命令文本:

    string strCommandText = "UPDATE Product SET Count = Count + 1 ";

    string strCommandText = "UPDATE Product SET Count = Count + @count WHERE ProductName = @ProductName  ";

这应该都是从纯粹的功能角度来看的。

但为了使其更加防弹,您应该检查“数量”字符串以确保它确实是一个数字。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-06
    • 2019-09-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多