Syntax error when executing INSERT INTO statement

前端 未结 4 1258
灰色年华
灰色年华 2021-01-23 08:52

I input the Right dataSource but it didnt i cant fixed the problem cmd.ExecuteNonQuery() saying:

Syntax error in INSERT INTO

4条回答
  •  说谎
    说谎 (楼主)
    2021-01-23 09:05

    Use a parameterized query, like this:

    cmd.CommandText = "INSERT INTO Printlist1(StickerCode, Description, Company, Department, Location, User, SerialNumber, DatePurchased, Tagable, Quantity, Brand, Model)" & _
                            " VALUES(@StickerCode, @Description, @Company, @Department, @Location, @User, @SerialNumber, @DatePurchased, @Tagable, @Quantity, @Brand, @Model)"
    
    cmd.Parameters.AddWithValue("@StickerCode", Me.Text1.Text)
    cmd.Parameters.AddWithValue("@Description", Me.Text2.Text)
    cmd.Parameters.AddWithValue("@Company", Me.Text3.Text)
    cmd.Parameters.AddWithValue("@Department", Me.Text4.Text)
    cmd.Parameters.AddWithValue("@Location", Me.Text5.Text)
    cmd.Parameters.AddWithValue("@User", Me.Text6.Text)
    cmd.Parameters.AddWithValue("@SerialNumber", Me.Text7.Text)
    cmd.Parameters.AddWithValue("@DatePurchased", Me.Text8.Text)
    cmd.Parameters.AddWithValue("@Tagable", Me.Text9.Text)
    cmd.Parameters.AddWithValue("@Quantity", Me.Text10.Text)
    cmd.Parameters.AddWithValue("@Brand", Me.Text11.Text)
    cmd.Parameters.AddWithValue("@Model", Me.Text12.Text)
    

    Note: It is best to keep the order of the parameters in line with the query, as databases like Microsoft Access will not execute the query correctly if the order is altered.

提交回复
热议问题