vb.net你的sql语法有错误;

时间:2014-02-18 23:57:01

标签: mysql sql vb.net

我点击“保存”按钮后出现此错误,但按钮上的事件正常工作。我知道下面的一个查询中一定有错误,但我不确定哪一个....请查看我的代码,我希望你能尽快回答。感谢

错误

you have an error in your sql syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near " at line 1

这是我的代码。

Private Sub add_petugas()
    Try
        sql = "INSERT INTO petugas(kd_petugas, nm_petugas, j_kel, almt_petugas, tlp_petugas, user, password, hak_akses) VALUES('" & txtKode.Text & "', '" & txtNama.Text & "', '" & cmbSex.Text & "', '" & txtAlamat.Text & "', '" & txtTelepon.Text & "', '" & txtUsername.Text & "', '" & txtPassword.Text & "', '" & txtHak.Text & "')"
        openDB()
        cmd = New MySqlCommand(sql, conn)
        Dim i As Integer
        i = cmd.ExecuteNonQuery
        If i > 0 Then
            MessageBox.Show("Data petugas berhasil ditambahkan", "Daftar", MessageBoxButtons.OK, MessageBoxIcon.Information)
        Else
            MessageBox.Show("Data petugas gagal ditambahkan", "Daftar", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End If
    Catch ex As Exception
        MsgBox(ex.Message)
    Finally
        cmd.Dispose()
        conn.Close()
    End Try
End Sub

Private Sub update_petugas()
    Try
        sql = "UPDATE petugas SET kd_petugas ='" & txtKode.Text & "', nm_petugas = '" & txtNama.Text & "', j_kel = '" & cmbSex.Text & "', almt_petugas = '" & txtAlamat.Text & "', tlp_petugas = '" & txtTelepon.Text & "', user = '" & txtUsername.Text & "', password = '" & txtPassword.Text & "', hak_akses = '" & txtHak.Text & "' WHERE kd_petugas = " & txtKode.Text & ""
        openDB()
        cmd = New MySqlCommand(sql, conn)
        Dim i As Integer
        i = cmd.ExecuteNonQuery
        If i > 0 Then
            MessageBox.Show("Data petugas berhasil diupdate", "Update", MessageBoxButtons.OK, MessageBoxIcon.Information)
        Else
            MessageBox.Show("Data petugas gagal diupdate", "Update", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End If
    Catch ex As Exception
        MsgBox(ex.Message)
    Finally
        cmd.Dispose()
        conn.Close()
    End Try
End Sub

2 个答案:

答案 0 :(得分:0)

看起来有些报价在结束时被遗漏了。

WHERE kd_petugas = " & txtKode.Text & ""

WHERE kd_petugas = '" & txtKode.Text & "'"

答案 1 :(得分:0)

Private Sub TextBox1_KeyPress(ByVal sender As Object, 
    ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
    If e.KeyChar = Chr(13) Then
        CMD = New OdbcCommand(String.Format("select * from petugas where nama='" & TextBox1.Text & "', and kode_petugas='" & MenuUtama.Panel1.Text & "'"), CONN)
        RD = CMD.ExecuteReader
        RD.Read()
        If RD.HasRows Then
            TextBox2.Text = True
            TextBox2.Focus()
        Else
            MsgBox("Anda tidak berhak mengganti password disini")
            TextBox1.Focus()
        End If
    End If