2010-12-21 54 views
0

每当我呼吁以下CommandText的“ExecuteNonQuery”命令,我得到上面的SQLException的SQLException:不正确的语法附近“2”

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字段给予特殊待遇?

感谢您的帮助

+3

为什么啊,为什么人们仍然没有使用参数化查询。你让你的生活变得非常困难,更不要说打开自己的SQL注入攻击。 – RPM1984 2010-12-21 02:32:04

+1

请向我们展示所有文本串联的结果。答案可能很明显。 – 2010-12-21 02:47:14

+0

我知道有关SQL注入攻击以及什么时候暴露给他们。非条目不是来自表单上的文本框,它们全部来自订阅的足球比赛的XML提要。所以没有注射风险。无论如何,我已经使用存储过程,它的工作。 – 2010-12-22 06:52:42

回答

0

您需要使用参数。

+0

没错。我用一个存储过程作为参数,而不是代码中的SQL查询,它的工作原理像魔术一样。 – 2010-12-22 06:45:29

3

如果您在调试器中查看myCommand.CommandText的值,并将字符串连接起来后,我认为很容易发现问题。我的猜测是,你可能需要在你的日期时间价值报价:

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

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

但是,如评论中指出的那样,使用参数化查询会更好。然后事情就会起作用。

0

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

请不要发表您使用的表达式来计算INSERT语句,而是生成的语句本身(CommandText的内容)。这是错误的地方。

1

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

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 

感谢所有

+0

您也可以只使用正常查询的参数。 – SLaks 2010-12-22 13:53:12