C#和SQL Server错误 [英] C# and sql server error

查看:99
本文介绍了C#和SQL Server错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试使用此代码时,vs向我显示此错误:
<字符串"@bc"后的右引号.
``@bc''附近的语法不正确.>
有人知道哪里出问题了吗?

when im trying to use this code, vs shows me this error:
< Unclosed quotation mark after the character string ''@bc''.
Incorrect syntax near ''@bc''.>
does anybody have idea where is problem?

internal static void EditProduct(string txtBarCode, string txtArtNumber, string txtProductName, string txtPrice, string txtComment, decimal NumericUpDown1, string PriceWithOutAWD, string txtSelfPrice)
{
    SqlConnection con = new SqlConnection(ConnectionString);
    SqlCommand cmd = new SqlCommand("UPDATE Products SET BarCode = @BarCode, ArtNumber = @ArtNumber, ProductName = @ProductName, Price = @Price, SelfPrice = @SelfPrice, PriceWithOutAWD = @PriceWithOutAWD, UnitsInStock = @UnitsInStock, Comment = @Comment WHERE BarCode = ''@bc", con);


    cmd.Parameters.Add(new SqlParameter("@BarCode", txtBarCode));
    cmd.Parameters.Add(new SqlParameter("@ArtNumber", txtArtNumber));
    cmd.Parameters.Add(new SqlParameter("@ProductName", txtProductName));
    cmd.Parameters.Add(new SqlParameter("@Price", txtPrice));
    cmd.Parameters.Add(new SqlParameter("@SelfPrice", txtSelfPrice));
    cmd.Parameters.Add(new SqlParameter("@PriceWithOutAWD", PriceWithOutAWD));
    cmd.Parameters.Add(new SqlParameter("@UnitsInStock", NumericUpDown1));
    cmd.Parameters.Add(new SqlParameter("@Comment", txtComment));
    cmd.Parameters.Add(new SqlParameter("@bc", txtBarCode));
    //cmd.Parameters.Add(new SqlParameter("@an", txtArtNumber));
    try
    {
        con.Open();
        cmd.ExecuteNonQuery();
    }
    finally
    {
        con.Close();
    }
}

推荐答案

是,

就在这里;
yep,

right here;
BarCode = ''@bc", con
          ^


看,您打开了一个报价,但没有关闭它,尽管我认为您根本不想在查询的其余部分中添加一个报价,但这只是一个错字.


Look, you opened a quote, but didn''t close it, although i don''t think you meant to put one in at all looking at the rest of your query, and it is just a typo.


SqlCommand cmd = new SqlCommand("UPDATE Products SET BarCode = @BarCode, ArtNumber = @ArtNumber, ProductName = @ProductName, Price = @Price, SelfPrice = @SelfPrice, PriceWithOutAWD = @PriceWithOutAWD, UnitsInStock = @UnitsInStock, Comment = @Comment WHERE BarCode = ''@bc", con);



无需在更新语句中再次更新条形码,您在以下条件下使用此代码...



No Need update the Barcode again in your update statement, you are using for this in where condition...


所有参数的类型均为字符串,因此应在所有输入中添加引号,参数.
the type all of parameters are string,so you should add quote around all in-paramter.


这篇关于C#和SQL Server错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆