买一本书,做一个减法

本文关键字:一个 一本 | 更新日期: 2023-09-27 18:33:17

我使用以下代码购买书籍和.....在TextBox中,每次我输入已售出书籍的数量时,它都会更新数据库中的新TextBox值。每次书籍销售时,它都必须做减法。但是,它会更新新值。

private void textBox1_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Return)
        {
            sql = new SqlConnection(@"Data Source=PC-PC'PC;Initial Catalog=Anbar;Integrated Security=True");
            adapter = new SqlDataAdapter("select * from Goods", sql);
            cmd = new SqlCommand();
            cmd.Connection = sql;
            cmd.CommandText = ("Update Goods set Buy =@Buy, Remain =@Remain where GoodsNumber =@GoodsNumber");
            cmd.Parameters.AddWithValue("@Buy", Convert.ToInt32(textBox1.Text));
            cmd.Parameters.AddWithValue("@GoodsNumber", Convert.ToInt32(comboBox1.Text));
            cmd.Parameters.AddWithValue("@Remain", Convert.ToInt32(comboBox3.Text) - Convert.ToInt32(textBox1.Text));
            sql.Open();
            cmd.ExecuteNonQuery();
            sql.Close();
            fill();
        }
    }

它应该显示总书籍的剩余部分。

买一本书,做一个减法

我认为问题是您在更新数据库中的剩余计数后不更新comboBox3

顺便说一句,为什么你为此使用组合框?数据库中还有非常混乱的列名:购买、商品编号和剩余。真的很难猜测那里存储了哪些数据。考虑诸如产品 ID、名称和数量等名称。

如果我要实现这样的东西,我从将产品加载到 combobox 开始:

private void Form_Load(object sender, EventArgs e)
{
    productsComboBox.DataSource = GetAllProducts();
    productsComboBox.DisplayMember = "Name";
    productsComboBox.ValueMember = "Id";
}
private IList<Product> GetAllProducts()
{
    List<Product> products = new List<Product>();
    // I use ConfigurationManager from System.Configuration.dll 
    // to read connection strings from App.config
    string connectionString = ConfigurationManager.ConnectionStrings["anbar"].ConnectionString;
    using (SqlConnection conn = new SqlConnection(connectionString))
    {
        string query = "SELECT * FROM Products";
        SqlCommand cmd = new SqlCommand(query, conn);
        conn.Open();
        SqlDataReader reader = cmd.ExecuteReader();
        while (reader.Read())
        {
            products.Add(new Product() { Id = (int)reader["Id"],
                                         Name = (string)reader["Name"],
                                         Quantity = (int)reader["Quantity"] });
        }
    }
    return products;
}

产品简单类:

public class Product
{
    public int Id { get; set; }
    public string Name { get; set; }
    public int Quantity { get; set; }
}

当所选产品发生更改时,显示其详细信息:

private void ProductsComboBox_SelectedIndexChanged(object sender, EventArgs e)
{
    Product product = productsComboBox.SelectedItem as Product;
    qtyTextBox.Text = product.Quantity.ToString();
    // I use NumericUpDown control to input numbers
    // Minimum is set to 1
    qtyToSellNumericUpDown.Maximum = product.Quantity;
}

最后 - 当我输入要出售的数量(1..当前数量)时,我执行以下操作:

private void ButtonSell_Click(object sender, EventArgs e)
{
    Product product = productsComboBox.SelectedItem as Product;
    int qtyToSell = (int)qtyToSellNumericUpDown.Value;
    SellProduct(product, qtyToSell);
    product.Quantity -= qtyToSell; // update product
    qtyTextBox.Text = product.Quantity.ToString(); // update current quantity 
}
private void SellProduct(Product product, int qtyToSell)
{
    string connectionString = ConfigurationManager.ConnectionStrings["anbar"].ConnectionString;
    using (SqlConnection conn = new SqlConnection(connectionString))
    {
        string query = "UPDATE Products SET Quantity = @Quantity WHERE Id = @Id";
        SqlCommand cmd = new SqlCommand(query, conn);
        cmd.Parameters.AddWithValue("@Id", product.Id);
        cmd.Parameters.AddWithValue("@Quantity", product.Quantity - qtyToSell);
        conn.Open();
        cmd.ExecuteNonQuery();
    }
}

就是这样。是的,有很多地方需要改进,但这个小示例有效。

更新:如何将连接字符串添加到应用程序配置

<configuration>
  <connectionStrings>
    <add name="anbar" 
         connectionString="Data Source=PC-PC'PC;Initial Catalog=Anbar;Integrated Security=True"                 
         providerName="System.Data.SqlClient"/>
  </connectionStrings>
</configuration>