简体   繁体   English

如何使用 SQL Server 作为数据库在 Visual Studio C# 中创建销售按钮代码

[英]How to Create a Sell Button Code In Visual Studio C# with SQL Server as database

I'm using Visual Studio C# and I'm having a conflict on how to create a sell button as well how can I get the total amount of product that had been sold here is my code我正在使用 Visual Studio C# 并且我在如何创建销售按钮以及如何获得已售出的产品总量方面存在冲突是我的代码

try
{
    if (txtID.Text != "" && txtCategory.Text != "" && 
        txtName.Text != "" && txtQty.Text != "" && 
        txtQty.Text != "" && PriceText.Text != "" && 
        SuppNameText.Text != "")
    {
        con.Open();
        SqlCommand cmd = new SqlCommand(@"INSERT INTO Sold_Inventory 
            (ProductName, Description, Category, Quantity_Sold, Price,
             TotalAmount, SuppliersName) 
             VALUES(@ProductName, @Description, @Category, @Quantity, @Price,
             @Supplier)
             WHERE TotalAmount = @Quantity * @Price", con);

        cmd.Parameters.AddWithValue("@ProductName", txtID.Text);
        cmd.Parameters.AddWithValue("@Description", txtName.Text);
        cmd.Parameters.AddWithValue("@Category", txtCategory.Text);
        cmd.Parameters.AddWithValue("@Quantity", txtQty.Text);
        cmd.Parameters.AddWithValue("@Price", PriceText.Text);
        cmd.Parameters.AddWithValue("@Supplier", SuppNameText.Text);

        cmd.ExecuteNonQuery();

        SqlCommand cmd1 = new SqlCommand(@"UPDATE Quantity 
               FROM Inventory SET Quantity = Quantity - @Quantity");
        cmd1.Parameters.AddWithValue("@Quantity", txtQty.Text);

        cmd1.ExecuteNonQuery();
        con.Close();

        ClearTextbox();

        btnSearch.Enabled = true;
        txtSearch.Enabled = true;
        groupBox4.Enabled = true;
        btnSubmit.Enabled = false;
        btnCancel.Enabled = false;
        ClearTextbox();
        DisabledTextbox();
        btnAdd.Enabled = true;

        RefreshDGV(this.dataGridView1);
    }
    else
    {
        MessageBox.Show("You left an empty field!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
}
catch (Exception ex)
{
    MessageBox.Show(ex.Message);
}

The syntax of the first SqlCommand is wrong.第一个 SqlCommand 的语法是错误的。
INSERT INTO doesn't require a WHERE clause and you are missing one parameter placeholder in your query text. INSERT INTO 不需要 WHERE 子句,并且您的查询文本中缺少一个参数占位符。

So the first fix is around your command text所以第一个修复是围绕你的命令文本

SqlCommand cmd = new SqlCommand(@"INSERT INTO Sold_Inventory 
     (ProductName,Description,Category,Quantity_Sold,Price,
      TotalAmount,SuppliersName) 
      VALUES(@ProductName,@Description,@Category,@Quantity,@Price,
      @TotalAmount, @Supplier)", con);

Now calculate the value for the @TotalAmount parameter in code like this....现在在这样的代码中计算@TotalAmount 参数的值......

decimal total = Convert.ToDecimal(txtQty.Text) * 
                Convert.ToDecimal(PriceText.Text);
cmd.Parameters.AddWithValue("@TotalAmount", total);

Finally, the command that decrements the quantity should identify the exact record for the product that you have sold.最后,减少数量的命令应该标识您所售产品的准确记录。 Here the WHERE clause is fundamental.这里 WHERE 子句是基本的。

I suppose that your Product table has a record for the product that you have sold identified by something like a ProductID primary key.我想你的 Product 表有一个你所销售的产品的记录,由 ProductID 主键之类的东西标识。 (Also you need to add the connection when you build the command) (另外你需要在构建命令时添加连接)

SqlCommand cmd1 = new SqlCommand(@"Update Inventory 
           SET Quantity = Quantity - @Quantity
           WHERE ProductID = @productID", con);

There are other considerations to your code.您的代码还有其他注意事项。 First, forget the AddWithValue method.首先,忘记 AddWithValue 方法。 It is an easy shortcut, but it can create a lot of problems if you pass a string as parameter value and the column that receives the value is not a text field (the automatic conversion from string to decimal could fail because the localization of your client code doesn't match the rules used by the database engine)这是一个简单的快捷方式,但是如果您将字符串作为参数值传递并且接收该值的列不是文本字段(从字符串到十进制的自动转换可能会失败,因为您的客户端的本地化),它会产生很多问题代码与数据库引擎使用的规则不匹配)

Better use always the Add method specifying the datatype of the parameter.最好始终使用指定参数数据类型的 Add 方法。
For example, the Price column probably wants a decimal not a string, so例如,价格列可能需要小数而不是字符串,所以

decimal price = Convert.ToDecimal(PriceText.Text);
cmd.Parameters.Add("@Price", SqlDbType.Decimal).Value = price;

Of course the same applies to all other values.当然,这同样适用于所有其他值。 Don't let AddWithValue figure out what do you mean with your parameter.不要让 AddWithValue 弄清楚你的参数是什么意思。 Be explicit.明确一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Visual Studio 2015中使用C#创建到SQL Server数据库的连接字符串 - How to create a connection string using C# to SQL Server database in Visual Studio 2015 如何在Visual Studio 2017中从C#代码制作SQL Server 2017本地数据库? - How to make a SQL Server 2017 local database from C# code in Visual Studio 2017? 如何将在 Visual Studio C# 中创建的数据库迁移到 SQL 服务器? - How to migrate a DataBase created in Visual Studio C# to a SQL Server? 在 Visual Studio 2015 中使用 SQL 服务器数据库从 C# 项目创建安装文件 - Create setup file from C# project with SQL server database in Visual Studio 2015 C#Visual Studio违反PK for SQL Server数据库 - C# Visual Studio violation of PK for SQL Server database 通过 Visual Studio 上 C# 中的 datagridview 更新 SQL 服务器数据库 - Update SQL Server database via a datagridview in C# on Visual Studio 使用C#连接到SQL Server 2012数据库(Visual Studio 2012) - Connect to SQL Server 2012 Database with C# (Visual Studio 2012) Microsoft SQL Server 数据库/Visual Studio C#:日期处理 - Microsoft SQL Server database / Visual Studio C# : date handling 在Visual Studio 2012 C#中连接到SQL Server数据库 - Connection to SQL Server Database inside Visual Studio 2012 C# Visual Studio 中的数据库问题(c# 和 SQL 服务器) - Database problem in visual studio (c# and SQL Server)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM