Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用SQL Server作为数据库在Visual Studio C#中创建销售按钮代码_C#_Sql Server_Visual Studio 2012 - Fatal编程技术网

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

如何使用SQL Server作为数据库在Visual Studio C#中创建销售按钮代码,c#,sql-server,visual-studio-2012,C#,Sql Server,Visual Studio 2012,我使用的是Visual Studio C#,在如何创建“销售”按钮以及如何获取此处已销售的产品总量方面存在冲突。这是我的代码 try { if (txtID.Text != "" && txtCategory.Text != "" && txtName.Text != "" && txtQty.Text != "" && txtQty.Text != "" && Price

我使用的是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);
}

第一个SqlCommand的语法错误。
INSERT INTO不需要WHERE子句,并且查询文本中缺少一个参数占位符

因此,第一个修复是围绕您的命令文本

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);
现在计算代码中@TotalAmount参数的值,如下

decimal total = Convert.ToDecimal(txtQty.Text) * 
                Convert.ToDecimal(PriceText.Text);
cmd.Parameters.AddWithValue("@TotalAmount", total);
最后,递减数量的命令应该标识您所销售产品的准确记录。这里WHERE子句是基本的

我假设您的产品表中有一条您所销售产品的记录,该记录由ProductID主键之类的东西标识。 (还需要在生成命令时添加连接)

您的代码还需要考虑其他因素。首先,忘记AddWithValue方法。这是一个简单的快捷方式,但如果将字符串作为参数值传递,并且接收值的列不是文本字段,则可能会产生很多问题(从字符串到十进制的自动转换可能会失败,因为客户端代码的本地化与数据库引擎使用的规则不匹配)

最好始终使用Add方法指定参数的数据类型。
例如,Price列可能需要十进制而不是字符串,因此

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

当然,这同样适用于所有其他值。不要让AddWithValue理解参数的含义。要明确。

INSERT INTO不需要WHERE条件,而是一个不带WHERE更新的更新,这是一个非常奇怪的WHERE子句。你应该想更新一个特定的项目对不起,我在编码方面是新手,你能解释给我听吗?当我试着按Seld时,请检查我的数据库数量是否保持不变,你用MySql标记它,然后你使用Sql Server专用的SqlClient库中的类。您使用的是哪个数据库?MySQL管理研究如果我的表中没有主键,而它们大多是独立的表,该怎么办?您如何识别销售的产品?我想你们有不止一种产品,对吗?您是否仅使用产品名称?(错误)不管怎样,如果您执行更新时没有指定名称,那么您将更新该表中的每个记录,以及存储不同产品数量的记录……哦,是的,很抱歉,我忘记了按产品名称标识它,以及如果您需要重命名产品会发生什么情况(根据我的经验,这不是不可能的事情)?然而,WHERE子句可以很容易地更改为“…WHERE ProductName=@ProductName”我对我拥有的每个产品都有一个固定的名称,而且我也尝试了你的代码我似乎无法使数量正常工作可能正是我放它的地方
decimal price = Convert.ToDecimal(PriceText.Text);
cmd.Parameters.Add("@Price", SqlDbType.Decimal).Value = price;