【问题标题】:IN SQL Query Error ""Incorrect syntax near '0)'." in c#IN SQL查询错误“'0)'附近的语法不正确。”在c#中
【发布时间】:2018-12-29 13:23:34
【问题描述】:
SqlConnection con = new SqlConnection(@"Data Source=HAMMAD2-PC\SQLEXPRESS;Initial Catalog=StockManagement;Integrated Security=True");
con.Open();

SqlCommand cmd = new SqlCommand(@"INSERT INTO [StockManagement].[dbo].[Product] ([ProductID], [ProductName], [SalePrice], [PurchasePrice], [Status])
 VALUES ('" + pcodetxt.Text + "','" + pnametxt.Text + "','" + rtlpricetxt + "','" + purpricetxt.Text + "','" + statuscbox.SelectedIndex+")'",con);

cmd.ExecuteNonQuery();
con.Close();

此代码导致错误

'0)' 附近的语法不正确

解决办法是什么?

我正在使用 Visual Studio 2012 和 SQL Server

【问题讨论】:

  • 学习使用参数!这样的问题就会消失。
  • 你错过了结束引号试试这个 ('"+pcodetxt.Text+"','"+pnametxt.Text+"','"+rtlpricetxt+"','"+purpricetxt.Text+"', '"+statuscbox.SelectedIndex+"')",con);
  • 通过将代码放在stored proc中来使用参数化查询
  • 简单调试 101:将字符串复制到变量中,查看生成的字符串。粘贴到 SSMS (SQL Server Managemen Studio)。这根本与 C# 无关,除了“你错误地将字符串放在一起”。

标签: c# sql-server database


【解决方案1】:

如果你使用了参数就不会出现这样的错误,而且你会受到“SQL注入攻击”的保护。即:

using (SqlConnection con = new SqlConnection(@"server=.\SQLEXPRESS;Initial Catalog=StockManagement;Integrated Security=True"))
using (SqlCommand cmd = new SqlCommand(@"INSERT INTO [StockManagement].[dbo].[Product]
   ([ProductID]
   ,[ProductName]
   ,[SalePrice]
   ,[PurchasePrice]
   ,[Status])
VALUES
   (@pid, @pname, @salePrice, @purPrice, @status)", con))
{
    cmd.Parameters.Add("@pid", SqlDbType.Int).Value = int.Parse(pcodetxt.Text);
    cmd.Parameters.Add("@pname", SqlDbType.VarChar).Value = pnametxt.Text;
    cmd.Parameters.Add("@salePrice", SqlDbType.Money).Value = decimal.Parse(rtlpricetxt);
    cmd.Parameters.Add("@purPrice", SqlDbType.Money).Value = decimal.Parse(purpricetxt.Text);
    cmd.Parameters.Add("@status", SqlDbType.Int).Value = statuscbox.SelectedIndex;

    con.Open();
    cmd.ExecuteNonQuery();
    con.Close(); // This is not needed: it is done by the implicit Dispose when exiting the using block
}

【讨论】:

  • 您也应该为SqlCommand 使用using (....) { .... } 块!
  • 并且不需要关闭连接,这是在退出 using 块时通过隐式 Dispose 完成的。您可能可以通过坚持使用他们的连接字符串来帮助 OP,以避免他们认为它有一些微妙的问题。
  • @Richardissimo,它有一些微妙的问题。如果你用机器名写它并不总是有效的。点有效。
  • @CetinBasoz 感谢您的解释……我以前没听说过。也许考虑在你的答案中解释这一点;但这不是该用户遇到的问题。
【解决方案2】:

该错误是因为您在 sql 语句中缺少右引号,但无论如何您都不应该使用字符串操作手动创建语句 - 这很容易出错,而且非常不安全!

改用声明的参数。 见What's the best method to pass parameters to SQLCommand?

【讨论】:

    【解决方案3】:

    X 附近的语法不正确,试图告诉您在 X 之前或之后有一些错误。

    在您的查询中,您将' 放置在错误的位置

    所以改写如下:

    SqlCommand cmd = new SqlCommand(@"INSERT INTO [StockManagement].[dbo].[Product] ([ProductID], [ProductName], [SalePrice], [PurchasePrice], [Status])
     VALUES ('" + pcodetxt.Text + "','" + pnametxt.Text + "','" + rtlpricetxt + "','" + purpricetxt.Text + "','" + statuscbox.SelectedIndex+"')",con);
    

    注意: 使用以下代码会将自己置于 SQL 注入漏洞的范围内,因此您应始终尝试将代码编写为 @CetinBasoz 张贴或其他类似的方法来确保您的安全针对类似的漏洞。

    【讨论】:

    • @CetinBasoz 问题不在于编码风格。如果他/她询问正确或安全的代码,我们可以关注您的评论,否则您的评论超出了问题的范围
    • @CetinBasoz 你是 100% 正确的,我同意你的看法。但我想告诉你,解决上述问题有不同的选择。一个是你的,另一个是我的等等。你的代码是安全的等等......我的代码是问题的形式,我只是试图不更改代码。我认为没有必要投反对票!
    • @CetinBasoz 好的,您可以将其作为对给定代码的注释提及。祝你好运;-)
    • @CetinBasoz 我已经更新了我的答案并将你的笔记放在里面
    猜你喜欢
    • 2013-12-15
    • 2017-02-26
    • 1970-01-01
    • 2014-03-15
    • 1970-01-01
    • 1970-01-01
    • 2012-01-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多