语法错误-c#

语法错误-c#,c#,sql,sql-server,C#,Sql,Sql Server,我犯了一个错误 “(”附近的语法不正确 输入特定产品代码时,我正在从数据库更新产品 如何做到这一点 // Update product with supplier code entered DialogResult dr = MessageBox.Show("Are you sure you want to update this product?", "Update Product Details", MessageBoxButtons.YesNo); if (dr == DialogRes

我犯了一个错误

“(”附近的语法不正确

输入特定产品代码时,我正在从数据库更新产品

如何做到这一点

// Update product with supplier code entered
DialogResult dr = MessageBox.Show("Are you sure you want to update this product?", "Update Product Details", MessageBoxButtons.YesNo);

if (dr == DialogResult.Yes)
{
    try
    {
         using (SqlConnection SQLcon = new SqlConnection("Data Source = .\\SqlExpress;" + "Initial Catalog=NCAShop;" + "Integrated Security=True;"))
         {
             SQLcon.Open();

             using (SqlCommand addProduct = new SqlCommand("UPDATE dbo.[NCAProduct] (ProductName, SupplierCode, Cost, RetailPrice, Quantity, BestBefore) VALUES ('" + txtUPProductName.Text + "', " + txtUPSupplierCode.Text + ", " + txtUPCost.Text + ", " + txtUPRetail.Text + ", " + txtUPQuantity.Text + ", @date) WHERE ProductCode = " + txtUPProdCode.Text, SQLcon))
             {
                 addProduct.Parameters.Add("@date", SqlDbType.DateTime).Value = bestBeforeDTP.Value.Date;
                 addProduct.ExecuteNonQuery();
             }
         }

         MessageBox.Show("This product has been successfully added to the database!");
    }
    catch (Exception error2)
    {
        MessageBox.Show(error2.ToString());
    }
}
else if (dr == DialogResult.No)
{
    // Program will continue if user selects 'No'
}

您需要使用来更正SQL更新的语法。语法为:

UPDATE table SET column = value, ... WHERE ...
您将它与
INSERT
语法混合在一起,即

INSERT INTO Table (Column, ...) VALUES (Value, ...)
我想您实际上还是想执行
INSERT
操作。在这种情况下,请将查询中的
UPDATE
替换为
INSERT-INTO
,这样就可以了


PS:Oh and please-对所有值使用参数,而不仅仅是日期。

使用正确的查询格式有时“[]”这些括号在数据库中执行查询时会产生错误,请对特定的数据库类型使用正确的转换。为防止对每个用户提供的输入使用参数。您可以使用下面给出的查询:

string query = @"UPDATE dbo.[NCAProduct] (
                       [ProductName], 
                       [SupplierCode], 
                       [Cost], 
                       [RetailPrice], 
                       [Quantity], 
                       [BestBefore]) VALUES 
                       (@txtUPProductName,
                        @txtUPSupplierCode,
                        @txtUPCost,
                        @txtUPRetail,
                        @txtUPQuantity, 
                        @date) 
                        WHERE [ProductCode] = @txtUPProdCode";
        using (SqlCommand addProduct = new SqlCommand(query, SQLcon))
        {
            addProduct.Parameters.AddWithValue("@date", SqlDbType.DateTime).Value = bestBeforeDTP.Value.Date;
            addProduct.Parameters.AddWithValue("@txtUPProductName", txtUPProductName.Text);
            addProduct.Parameters.AddWithValue("@txtUPSupplierCode", Convert.ToInt32(txtUPSupplierCode.Text));
            addProduct.Parameters.AddWithValue("@txtUPCost", Convert.ToInt32(txtUPCost.Text));
            addProduct.Parameters.AddWithValue("@txtUPRetail", Convert.ToInt32(txtUPRetail.Text));
            addProduct.Parameters.AddWithValue("@txtUPQuantity", Convert.ToInt32(txtUPQuantity.Text));
            addProduct.Parameters.AddWithValue("@txtUPProdCode", Convert.ToInt32(txtUPProdCode.Text));
            addProduct.ExecuteNonQuery();
        }

如果您在数据库中使用bigint数据类型,请将文本转换为int64或double。

您的
UPDATE
语法为false。是否插入数据或更新数据?+1000000,感谢您提到此查询对SQL注入非常开放的事实。谢谢-我认为值得注意的是,OP知道参数化查询,但只使用我想值得注意的是,OP知道参数化查询,但只在一个参数上使用它们。