'='附近的UPDATE语法不正确

时间:2016-07-28 07:24:26

标签: sql vb.net

我在更新中遇到了“错误的语法'='”错误。

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    conn.Open()
    For i = 0 To Me.Dgv_sql.Rows.Count - 1
        Dim idx As String = Dgv_sql.Rows(i).Cells("ID").Value
        query = "UPDATE kkpsurabaya SET late = '" & Dgv_sql.Rows(i).Cells("late").Value & "' WHERE ID = " & idx

        If (UpdateData(query)) Then

        End If

    Next
    conn.Close()
    MessageBox.Show("Data Berhasil Di Update", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Information)
    'dgv_sql_isi()
End Sub

enter image description here

2 个答案:

答案 0 :(得分:0)

Idx是一个字符串,所以当你执行where ID = (and place string here)时,它会失败。

更改为:

query = "UPDATE kkpsurabaya SET late = '" & Dgv_sql.Rows(i).Cells("late").Value & "' WHERE ID = '" & idx &"'"

但更好的解决方案就是一起使用Parameterized Queries .. - 将帮助您避免SQL Injection

答案 1 :(得分:-1)

创建变量并首先将值赋给该变量。

vara=Dgv_sql.Rows(i).Cells("late").Value
query = "UPDATE kkpsurabaya SET late = '" & vara & "' WHERE ID = " & idx

希望它会有所帮助。