开发者

SQLException: incorrect syntax near '2'

开发者 https://www.devze.com 2023-01-31 07:59 出处:网络
whenever I call the \"ExecuteNonQuery\" command on the following CommandText, I get the above SQLException

whenever I call the "ExecuteNonQuery" command on the following CommandText, I get the above 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 &a开发者_StackOverflow社区mp; ",'" & winner & "'," & fs_A & "," & fs_B & "," & hts_A & "," & hts_B & "," & ets_A & "," & ets_B & "," & ps_A & "," & ps_B & "," & last_updated & ")"

But whenever, i remove the last table item - "last_updated", the error disappears. Please help me resolve this issue. Is there any special treatment to be given to datetime fields???

Thanks for your help


If you take a look at the value of myCommand.CommandText in the debugger after the strings have been concatenated I think it will be easy to see the problem. My guess is that you probably need quotes around the value of your datetime:

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

You may also need to specify the format used to convert the DateTime to a string, for example last_updated.ToString("yyyy-MM-dd HH:mm:ss").

However it would be better to use parameterized queries, as pointed out in the comments. Then things will just work.


I see a lot of posts on the internet about similar problem. I tried using a stored procedure and it worked like magic. for the benefit of all who may share a similar fate, Try stored procedure. Here's my new code.

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

Thanks all


You need to use parameters.


Almost certainly the value contained in last_updated has to be delimited in the resulting INSERT statement (the "2" in the exception is probably from 2010-etc).

Please don't post the expression you use to calculate the INSERT statement but rather the resulting statement itself (the contents of CommandText). That's where the error is.

0

精彩评论

暂无评论...
验证码 换一张
取 消