2014-02-18 35 views
-1

我在點擊「保存」按鈕後出現此錯誤,但按鈕上的事件正常工作。我知道在下面的一個查詢中必須有錯誤,但我不確定哪一個....請檢查我的代碼,我希望你能儘快回答。由於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 
+0

你錯過了周圍的變量單引號您正在使用的'WHERE'你'UPDATE'查詢的WHERE子句。 – 2014-02-19 00:01:20

+0

@MikeW我已經試着用單引號解決這個問題,但是錯誤仍然存​​在.. – Dika

回答

0

看起來像是一些引號在結尾處被遺漏了。

WHERE kd_petugas = " & txtKode.Text & "" 

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

這可能是這種情況,但是如果你使用參數,那麼你不必擔心單引號或其他分隔符。添加這些單引號可能會使此代碼正常工作,但「正確的」解決方案是使用參數。 – jmcilhinney

+0

@jmcilhinney - 我同意100%! – Drewness

+0

@jmcilhinney我已經試過這個,但錯誤仍在。我仍然不明白「參數」,因爲這是我的學校考試,我在vb.net仍然是新的。如果你不介意,你可以用我的代碼解釋「參數」嗎? – Dika

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