2016-01-07 84 views
0

我想要做的是,首先檢查ID號碼是否存在,如果存在,則執行更新過程,但問題是,它不會更新。問題是什麼 ?無法更新mySQL數據庫中的數據

sqlconn = New MySqlConnection 
sqlconn.ConnectionString = "server=localhost;userid=root;password='';database=innovative" 
Try 
    sqlconn.Open() 
    query = "SELECT Full_Name FROM employee WHERE ID='" & txt_id_number.Text & "'" 
    cmd = New MySqlCommand(query, sqlconn) 
    reader = cmd.ExecuteReader 
    If reader.HasRows = False Then 
     MsgBox("Invalid ID number please secure that the ID number is already Exist" & vbNewLine & "TAKE NOTE:" & vbNewLine & "You cannot update or change the existing ID number for it is the primary Key for the Employee, If you want to Change it, its better to delete the Employee then add it again." & vbNewLine & "Other than that you can change the Full name, age, contact and etc.", vbCritical) 

    Else 
     reader.Close() 
     sqlconn.Open() 
     query1 = "UPDATE employee SET Full_Name ='" & txt_fullname.Text & "', Employee_Type='" & txt_employee_type.Text & "', Age='" & txt_age.Text & "',Sex='" & cb_sex.Text & "', Status='" & txt_status.Text & "', Contact ='" & txt_contact.Text & "',E_mail='" & txt_email.Text & "' WHERE ID = '" & txt_id_number.Text & "'" 
     cmd = New MySqlCommand(query1, sqlconn) 
     reader1 = cmd.ExecuteReader 
     MsgBox(txt_fullname.Text & " was successfully updated", vbInformation) 
     txt_age.Text = "" 
     txt_contact.Text = "" 
     txt_email.Text = "" 
     txt_employee_type.Text = "" 
     txt_fullname.Text = "" 
     txt_id_number.Text = "" 
     txt_status.Text = "" 
     cb_sex.Text = "" 
     add_employee() 
    End If 
    sqlconn.Close() 
Catch ex As Exception 
Finally 
    sqlconn.Dispose() 
End Try 
+0

讀者對象用於查詢數據庫,用於更新,插入和刪除您想要的ExecuteNonQuery語句。 –

+1

您還可以擺脫查詢查找有效ID的查詢,並返回執行更新語句時更新的行數。如果返回0,那麼WHERE條件未找到要更新的行。任何大於0的值表示符合WHERE條件。此外,您應該停止將值連接到您的語句中,並使用參數來保護您的sql語句,並讓您在知道如何根據類型來包裝字段時更輕鬆。 –

+0

此代碼很瘋狂 - 容易受到SQL注入攻擊。 –

回答

0
Imports MySql.Data.MySqlClient 

Public Class Form1 
    Private sqlconn As MySqlConnection 
    Private query, query1 As String 
    Private cmd As MySqlCommand 
    Private reader As MySqlDataReader 

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click 
     sqlconn = New MySqlConnection 
     sqlconn.ConnectionString = "server=localhost;userid=root;password='';database=innovative" 
     Try 
      sqlconn.Open() 
      query = "SELECT Full_Name FROM employee WHERE ID='" & txt_id_number.Text & "'" 
      cmd = New MySqlCommand(query, sqlconn) 
      reader = cmd.ExecuteReader 
      If reader.HasRows = False Then 
       MsgBox("Invalid ID number please secure that the ID number is already Exist" & vbNewLine & "TAKE NOTE:" & vbNewLine & "You cannot update or change the existing ID number for it is the primary Key for the Employee, If you want to Change it, its better to delete the Employee then add it again." & vbNewLine & "Other than that you can change the Full name, age, contact and etc.", vbCritical) 
      Else 
       query1 = "UPDATE employee SET Full_Name = @txt_fullname, Employee_Type=txt_employee_type, [email protected]_age'" 
     cmd = New MySqlCommand(query1, sqlconn) 
     cmd.CommandType = CommandType.Text 
     cmd.Parameters.Add("@txt_fullname", SqlDbType.VarChar, 255).Value = txt_fullname.Text 
     cmd.Parameters.Add("@txt_employee_type", SqlDbType.VarChar, 255).Value = txt_employee_type.Text 
     cmd.Parameters.Add("@txt_age", SqlDbType.VarChar, 255).Value = txt_age.Text 
     cmd.Parameters.Add("") 
     cmd.ExecuteNonQuery() 
       MsgBox(txt_fullname.Text & " was successfully updated", vbInformation) 
       txt_age.Text = "" 
       txt_contact.Text = "" 
       txt_email.Text = "" 
       txt_employee_type.Text = "" 
       txt_fullname.Text = "" 
       txt_id_number.Text = "" 
       txt_status.Text = "" 
       cb_sex.Text = "" 
       add_employee() 
      End If 
      sqlconn.Close() 
      reader.Close() 
     Catch ex As Exception 
     Finally 
      sqlconn.Dispose() 
     End Try 
    End Sub 
End Class 
+0

是正確的....你不能使用cmd.ExecuteReader執行非查詢事務。您只在使用Reader時拉取數據,而不是在插入,更新或刪除時。 – DiscipleMichael

+0

是的,這個代碼不安全,這是安全的。 – Mysta

+0

請擴大**爲什麼**您的答案有效。 – cybermonkey

0

三件事改變。

  1. 使用cmd。用於插入或更新查詢的ExecuteNonQuery
  2. 不要使用康恩。當它未關閉時,再次使用Open;它返回'連接已經打開'錯誤,並且執行將終止以捕獲塊。 這就是您的代碼無法工作的原因。
  3. Parameterize查詢安全和類型轉換。

快樂編碼!

+0

要添加到第2項):如果在你的代碼的任何一點,你不能確定的連接的狀態(開/關),你可以檢查'如果sqlconn.State = ConnectionState.Open'試圖打開之前,它再次。 – DiscipleMichael