【问题标题】:SQLException: incorrect syntax near '2'SQLException: '2' 附近的语法不正确
【发布时间】:2011-05-28 14:30:24
【问题描述】:

每当我在下面的CommandText 上调用“ExecuteNonQuery”命令时,我都会得到上面的 SQLException

myCommand.CommandText = "INSERT INTO fixtures (round_id, matchcode, date_utc, time_utc, date_london, time_london, team_A_id, team_A, team_A_country, team_B_id, team_B, team_B_country, status, gameweek, winner, fs_A, fs_B, hts_A, hts_B, ets_A, ets_B, ps_A, ps_B, last_updated) VALUES (" _
            & round_id & "," & match_id & "," & date_utc & ",'" & time_utc & "'," & date_london & ",'" & time_london & "'," & team_A_id & ",'" & team_A_name & "','" & team_A_country & "'," & team_B_id & ",'" & team_B_name & "','" & _
            team_B_country & "','" & status & "'," & gameweek & ",'" & winner & "'," & fs_A & "," & fs_B & "," & hts_A & "," & hts_B & "," & ets_A & "," & ets_B & "," & ps_A & "," & ps_B & "," & last_updated & ")"

但每当我删除最后一个表项 - “last_updated”时,错误就会消失。请帮我解决这个问题。 datetime字段有什么特殊处理吗???

感谢您的帮助

【问题讨论】:

  • 为什么哦为什么人们仍然不使用参数化查询。你让你的生活变得非常困难,更不用说让自己面临 sql 注入攻击了。
  • 请向我们展示所有文本的连接结果。答案可能很明显。
  • 我知道 SQL 注入攻击以及何时接触到它们。没有条目不是​​来自表单上的文本框,它们都来自订阅的足球比赛的 xml 提要。所以没有注射的风险。无论如何,我使用了存储过程并且它有效。

标签: asp.net sql exception


【解决方案1】:

你需要使用参数。

【讨论】:

  • 没错。我使用了一个将条目作为参数的存储过程,而不是代码中的 SQL 查询,它就像魔术一样工作。
【解决方案2】:

如果你在字符串连接后在调试器中查看myCommand.CommandText 的值,我认为很容易看出问题所在。我的猜测是,您可能需要在日期时间的值周围加上引号:

... & ",'" & last_updated & "')"

您可能还需要指定用于将 DateTime 转换为字符串的格式,例如 last_updated.ToString("yyyy-MM-dd HH:mm:ss")

但是,正如 cmets 中所指出的,使用参数化查询会更好。然后一切都会奏效。

【讨论】:

    【解决方案3】:

    几乎可以肯定,last_updated 中包含的值必须在生成的 INSERT 语句中进行分隔(异常中的“2”可能来自 2010 年等)。

    请不要发布用于计算 INSERT 语句的表达式,而是发布结果语句本身(CommandText 的内容)。这就是错误所在。

    【讨论】:

      【解决方案4】:

      我在网上看到很多关于类似问题的帖子。我尝试使用存储过程,它就像魔术一样工作。为了所有可能有类似命运的人的利益,请尝试存储过程。这是我的新代码。

      Dim myConnection As New SqlConnection(strConnection)
          Dim myCommand As New SqlCommand("TCreateMatch", myConnection)
      
          With myCommand
              .CommandType = CommandType.StoredProcedure
              With .Parameters
                  .AddWithValue("@round_id", round_id)
                  .AddWithValue("@matchcode", match_id)
                  .AddWithValue("@date_utc", date_utc)
                  .AddWithValue("@time_utc", time_utc)
                  .AddWithValue("@date_london", date_london)
                  .AddWithValue("@time_london", time_london)
                  .AddWithValue("@team_A_id", team_A_id)
                  .AddWithValue("@team_A", team_A_name)
                  .AddWithValue("@team_A_country", team_A_country)
                  .AddWithValue("@team_B_id", team_B_id)
                  .AddWithValue("@team_B", team_B_name)
                  .AddWithValue("@team_B_country", team_B_country)
                  .AddWithValue("@status", status)
                  .AddWithValue("@gameweek", gameweek)
                  .AddWithValue("@winner", winner)
                  .AddWithValue("@fs_A", fs_A)
                  .AddWithValue("@fs_B", fs_B)
                  .AddWithValue("@hts_A", hts_A)
                  .AddWithValue("@hts_B", hts_B)
                  .AddWithValue("@ets_A", ets_A)
                  .AddWithValue("@ets_B", ets_B)
                  .AddWithValue("@ps_A", ps_A)
                  .AddWithValue("@ps_B", ps_B)
                  .AddWithValue("@last_updated", last_updated)
              End With
      
              Try
                  myConnection.Open()
                  result = .ExecuteNonQuery()
              Catch ex As Exception
      
              Finally
                  myConnection.Close()
              End Try
          End With
      

      谢谢大家

      【讨论】:

      • 您也可以只使用普通查询的参数。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-11-09
      • 2021-01-23
      • 1970-01-01
      • 2020-11-03
      • 2019-04-13
      • 1970-01-01
      • 2010-11-27
      相关资源
      最近更新 更多