【问题标题】:Incorrect syntax near '8' error when using update query使用更新查询时,“8”附近的语法错误
【发布时间】:2011-05-18 10:26:29
【问题描述】:
 static public void updateSelectedCaravan(string make, string model, string birth, string year, string Int, string ext, string width, string Unladen, string mtplm, string warranty, string freeText, string price, string location, string Tel, string Email, int makeID, string description)
{
    SqlConnection conn = new SqlConnection(ConnectionString);
    conn.Open();
    SqlCommand updateNews = new SqlCommand("Update [productDetail] SET [make] =@make , [model] = @model , [Berth] = @birth , [Year] =  @year , [InternalLength] = @Int , [ExternalLength] = @ext, [Width] = @width , [UnladenWeight] = @Unladen , [MTPLM] = @mtplm , [Warranty] = @warranty , [FreeTextDetails] = @freeText , [Price] = @price , [Location] = @location , [Tel] = @Tel , [Email] = @Email , [description] = @description where [makeID] = @makeID", conn);
    updateNews.Parameters.AddWithValue("@make", make);
    updateNews.Parameters.AddWithValue("@model", model);
    updateNews.Parameters.AddWithValue("@birth", birth );
    updateNews.Parameters.AddWithValue("@year", year);
    updateNews.Parameters.AddWithValue("@Int", Int);
    updateNews.Parameters.AddWithValue("@ext", ext);
    updateNews.Parameters.AddWithValue("@width", width);
    updateNews.Parameters.AddWithValue("@Unladen", Unladen);
    updateNews.Parameters.AddWithValue("@mtplm", mtplm);
    updateNews.Parameters.AddWithValue("@warranty",warranty);
    updateNews.Parameters.AddWithValue("@freeText", freeText);
    updateNews.Parameters.AddWithValue("@price", price);
    updateNews.Parameters.AddWithValue("@location", location);
    updateNews.Parameters.AddWithValue("@Tel", Tel);
    updateNews.Parameters.AddWithValue("@Email",Email );
    updateNews.Parameters.AddWithValue("@description",description );
    updateNews.Parameters.AddWithValue("@makeID", makeID);
    updateNews.ExecuteNonQuery();
    conn.Close();
}

上面的查询不起作用,它给出了错误“8”附近的语法不正确,它之前工作正常,但我不知道它停止工作的原因,我已经调试了 d 查询并传递了所有必需的值。

【问题讨论】:

  • 你能提供 updateNews.CommandText 属性的值吗??

标签: asp.net sql database sql-update


【解决方案1】:

您的一个数据项包含一个单引号,可能在数字 8 旁边。这会破坏您的 SQL,并且是意外 SQL Injection 的示例,您应该注意这一点。

您应该使用参数化查询。这样单引号就会自动为你转义,这个问题就会消失。

【讨论】:

  • 很遗憾,我缺乏远距离直接读取计算机内存的能力。
  • 我现在刚改成参数
【解决方案2】:

停止创建您自己的 SQL 查询。它容易受到 SQL 注入攻击。改为使用参数化表达式,您将不必再处理这些参数错误。

 SqlCommand cmd = new SqlCommand("UPDATE productDetail SET make = @make WHERE id = @id");
 cmd.Parameters.AddWithValue("@make", "someValue");
 cmd.Parameters.AddWithValue("@id", 1234);

 // execute

【讨论】:

  • 是的,我会在在线上传之前这样做.. 只是检查一下,我已经习惯了,因为 uni ..
【解决方案3】:

首先,您应该对查询进行参数化,因为如果有人恶意使用您的系统进行 SQL 注入,您将面临巨大的安全问题。

其次,字段有哪些类型?如果它们是 int,很多都应该是,你不需要这样做:

SET year = '2011'

你会这样做:

SET year = 2011

这不会引发错误,但建议这样做。

调试查询的最佳方法是在执行之前打印查询,以查看实际构建的查询是什么。正如其他人所提到的,这可能是导致问题的单引号。

如果您将查询参数化,它将起作用,并且会更安全。

【讨论】:

  • 我已将所有字段设置为字符串
  • @Muhammad 只是为了让您知道您可能希望将其中一些设置为不同的数据类型,以后会有所收获。
  • 有什么办法可以在visual studio中调试sql查询
【解决方案4】:

除了上面的答案,我建议你尝试在你的函数中发送类对象。

这意味着您应该有一个对象模型项目。这包含一个用于您在函数中传递的所有参数的类。

将此项目的引用添加到您的数据层/演示文稿,并从演示文稿发送填充了数据成员值的此类的对象以访问成员数据。这样,万一明天你必须增加/减少参数,那么你不需要改变函数签名。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-05-26
    • 1970-01-01
    • 1970-01-01
    • 2016-02-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-15
    相关资源
    最近更新 更多