【问题标题】:How to update stock quantity in the database? [closed]如何更新数据库中的库存数量? [关闭]
【发布时间】:2013-03-01 07:49:36
【问题描述】:

我有 Windows 应用程序来添加购买信息。在将购买添加到数据库之前,用户从组合框中选择项目并添加到列表框中。当用户点击添加购买时,数据库中将有一个或多个项目。

那里的项目名称与数据库中的产品名称完全匹配。

我想获取列表框中每个项目的字符串,然后编写减少数据库中项目数量的 sql 查询。总量存储在 Quantity 列下的 Product 表中。

有没有人有想法来完成这个任务?

我正在尝试将列表框项的字符串一个一个传递给这个方法

 public string Update(string product)
        {
            // Create connection object
            int ix = 0;
            string rTurn = "";
            OleDbConnection oleConn = new OleDbConnection(connString);
            try
            {
                oleConn.Open();
                string sql = "UPDATE [Product] SET [Quantity]=[Quantity] - 1 " + " WHERE [Product Name]= " + product;
                OleDbCommand oleComm = new OleDbCommand(sql, oleConn);

                oleComm.Parameters.Add("@product", OleDbType.Char).Value = product;

                ix = oleComm.ExecuteNonQuery();
                if (ix > 0)
                    rTurn = "Stock Updated";
                else
                    rTurn = "Update Failed";
            }
            catch (Exception ex)
            {

            }
            finally
            {
                oleConn.Close();
            }
            return rTurn;
        }

以上方法将从以下方法中获取产品名称:

 public string updateStock()
        {
            string listItem = string.Empty;
            foreach (var listBoxItem in listBox1.Items)
            {



                if (listBox1.Items.IndexOf(listBoxItem) < listBox1.Items.Count - 1)
                {
                    listItem = listBox1.Items.ToString();
                }
            }

            return listItem;

        }

我将从按钮事件处理程序中调用此代码

Update(updateStock());

【问题讨论】:

  • 你能提供表的结构吗,也许有一个[SQL Fiddle](http//sqlfiddle.com)的例子?
  • 您是在寻求有关 SQL 查询、如何从列表框中获取名称或介于两者之间的所有内容的帮助吗?恐怕这个问题非常广泛......也许你需要把它分成不同的问题,并展示你对每个问题的尝试。
  • 我只是想获取列表框中每个项目的字符串并将该字符串传递给更新方法..让我编辑问题..
  • 我正在更新答案
  • @peterm 问题已更新

标签: c# sql winforms


【解决方案1】:

你可以试试这样的:

public void UpdateStock()
{
    foreach (var listBoxItem in listBox1.Items)
    {
        string result = Update(listBoxItem.ToString());
    }
}

public string Update(string product)
{
    // Create connection object
    string rTurn = "";
    OleDbConnection oleConn = new OleDbConnection(connString);
    try
    {
        oleConn.Open();
        string sql = "UPDATE [Product] SET [Quantity]=[Quantity] - 1 WHERE [Product Name] = @product";
        OleDbCommand oleComm = new OleDbCommand(sql, oleConn);

        oleComm.Parameters.Add("@product", OleDbType.VarChar, 50).Value = product;

        oleComm.ExecuteNonQuery();

        rTurn = "Stock Updated";    
    }
    catch (Exception ex)
    {
        rTurn = "Update Failed";
    }
    finally
    {
        oleConn.Close();
    }
    return rTurn;
}

编辑:您可以在添加参数时轻松指定大小 - 如上所示。 50只是任意的。您应确保大小与目标 [产品名称] 列的大小相同。

【讨论】:

  • 它适用于像“笔记本电脑”这样的项目,但如果你有像“三星 Galaxy S3”这样的项目,它会抛出错误。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-28
  • 2013-08-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多