我在點擊「保存」按鈕後出現此錯誤,但按鈕上的事件正常工作。我知道在下面的一個查詢中必須有錯誤,但我不確定哪一個....請檢查我的代碼,我希望你能儘快回答。由於vb.net你在你的sql語法中有錯誤;
錯誤
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
你錯過了周圍的變量單引號您正在使用的'WHERE'你'UPDATE'查詢的WHERE子句。 – 2014-02-19 00:01:20
@MikeW我已經試着用單引號解決這個問題,但是錯誤仍然存在.. – Dika