SQLException:'2'附近的语法不正确

时间:2010-12-21 02:28:29

标签: asp.net sql exception

每当我在下面的ExecuteNonQuery上调用“CommandText”命令时,我得到上面的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字段???

感谢您的帮助

4 个答案:

答案 0 :(得分:3)

如果在串联字符串后查看调试器中myCommand.CommandText的值,我认为很容易看到问题。我的猜测是,您可能需要围绕日期时间值的报价:

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

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

然而,如评论中所指出的,使用参数化查询会更好。事情就会奏效。

答案 1 :(得分: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

全部谢谢

答案 2 :(得分:0)

您需要使用参数。

答案 3 :(得分:0)

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

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