2015-01-27 30 views
0

這是如何工作的。我從列表視圖中選擇一行,然後點擊「編輯」按鈕,所選項目的值也將顯示在註冊表單中。 「註冊」按鈕現在將更改爲「更新」。我在更改我的註冊表格上的文本框輸入後更新我的客戶表,但是我的數據庫沒有任何更改。從鏈接到ListView中選定項目的文本框更改值後,數據庫中的表未更新

我沒有收到任何錯誤,但我可能在這裏錯過了一些東西。

這是我的代碼在這裏:

private void btnRfrsh_Click(object sender, EventArgs e) 
    { 
     try 
     { 
      con = "datasource=localhost; port=3306; database=cam_air_db; uid=root;"; 
      connect = new MySqlConnection(con); 
      connect.Open(); 

      string query = "SELECT Cust_Lname, Cust_Fname, Cust_MI, Birthdate, Age, Sex, Passport_ID, Address, Contact_Num, Nationality from customers where removed = 0"; 
      MySqlCommand select = new MySqlCommand(query, connect); 
      MySqlDataReader refresh = select.ExecuteReader(); 

      while (refresh.Read()) 
      { 
       ListViewItem item; 
       item = new ListViewItem(refresh.GetString(0)); 
       item.SubItems.Add(refresh.GetString(1)); 
       item.SubItems.Add(refresh.GetString(2)); 
       item.SubItems.Add(refresh.GetString(3)); 
       item.SubItems.Add(refresh.GetString(4)); 
       item.SubItems.Add(refresh.GetString(5)); 
       item.SubItems.Add(refresh.GetString(6)); 
       item.SubItems.Add(refresh.GetString(7)); 
       item.SubItems.Add(refresh.GetString(8)); 
       item.SubItems.Add(refresh.GetString(9)); 
       lviewCust.Items.Add(item); 
      } 

      if (refresh.Read()) 
      { 
       connect.Close(); 

      } 
      else 
      { 
       connect.Close(); 

      } 
     } 

     catch (Exception error) 
     { 
      MessageBox.Show(error.Message); 
     } 
    } 

    private void btnEdit_Click(object sender, EventArgs e) 
    { 
     if (lviewCust.SelectedItems.Count > 0) 
     { 
      ListViewItem item = lviewCust.SelectedItems[0]; 
      cust_fname.Text = item.SubItems[0].Text; 
      cust_lname.Text = item.SubItems[1].Text; 
      cust_mi.Text = item.SubItems[2].Text; 
      //DateTime bdate = Convert.ToDateTime(item.SubItems[3].Text); 
      String bdate_string = item.SubItems[3].Text; 
      DateTime bdate = DateTime.ParseExact(bdate_string, "dd-MM-yyyy", null); 
      cust_bdate.Value = bdate; 
      cust_age.Text = item.SubItems[4].Text; 
      cust_sex.Text = item.SubItems[5].Text; 
      cust_passid.Text = item.SubItems[6].Text; 
      cust_nation.Text = item.SubItems[9].Text; 
      cust_add.Text = item.SubItems[7].Text; 
      cust_contact.Text = item.SubItems[8].Text; 
     } 
     cust_fname.ReadOnly = true; 
     cust_lname.ReadOnly = true; 
     cust_mi.ReadOnly = true; 
     cust_passid.ReadOnly = true; 
     btnReg.Text = "Update"; 
     btnReg.Name = "btnUpdate"; 
     btnReg.Click -= this.btnReg_Click; 
     btnReg.Click += this.btnUpdate_Click; 
    } 

    private void btnUpdate_Click(object sender, EventArgs e) 
    { 
     try 
     { 
      con = "datasource=localhost; port=3306; database=cam_air_db; uid=root;"; 
      connect = new MySqlConnection(con); 
      connect.Open(); 

      string query = "UPDATE customers SET Age = '" + this.cust_age.Text + "', Nationality = '" + this.cust_nation.Text + "', Address = '" + this.cust_add.Text + "', Contact_Num = '" + this.cust_contact.Text + "' WHERE Cust_Fname = '" + this.cust_fname.Text + "' and Cust_Lname = '" + this.cust_lname.Text + "'"; 
      MySqlCommand update = new MySqlCommand(query, connect); 
      MySqlDataReader updte = update.ExecuteReader(); 
      MessageBox.Show("Customer Info Updated Successfully"); 


      if (updte.Read()) 
      { 
       connect.Close(); 

      } 
      else 
      { 
       connect.Close(); 

      } 
     } 

     catch (Exception error) 
     { 
      MessageBox.Show(error.Message); 
     } 
     cust_fname.Clear(); 
     cust_lname.Clear(); 
     cust_mi.Clear(); 
     cust_bdate.Value = DateTime.Now; 
     cust_age.Clear(); 
     cust_passid.Clear(); 
     cust_add.Clear(); 
     cust_contact.Clear(); 
     cust_nation.Clear(); 
     cust_fname.ReadOnly = false; 
     cust_lname.ReadOnly = false; 
     cust_mi.ReadOnly = false; 
     cust_passid.ReadOnly = false; 

     btnReg.Text = "Register"; 
     btnReg.Name = "btnReg"; 
     btnReg.Click -= this.btnUpdate_Click; 
     btnReg.Click += this.btnReg_Click; 
    } 




} 

}

回答

0
  • 首先,我認爲你應該使用的ExecuteNonQuery()代替的ExecuteReader()。
    當您執行返回某些內容的sql命令(如SELECT)時,可以調用ExecuteReader()。
    當您調用不返回任何內容的命令時(例如INSERT,UPDATE,DELETE等),您應該調用ExecuteNonQuery()。
    看到詳細信息here
  • 其次,我認爲你應該在提醒「成功」之前檢查結果。 ExecuteNonQuery()返回受影響的行數,你可以檢查這個以確定是否成功。
+0

謝謝。解決了我的問題。對不起,延遲迴復。在外面忙碌。 – user3488282 2015-02-12 08:23:54

相關問題