2017-01-07 63 views
1

嗨,我需要在這一個幫助。我想在這裏做的是檢查我的MySQL數據庫,如果一個成員使用基於我的WinForm數據輸入該成員的名稱已經存在。重複錄入工作驗證,顯示在MessageBox完美的方式我想,但它不執行插入查詢我的情況下,毫不現有成員中找到。C#檢查,然後運行插入查詢,如果沒有重複

我是不是做正確嗎?還是有另一種方式讓它按我想要的方式工作。

這裏是我的代碼:

private void metroButton1_Click(object sender, EventArgs e) { 
    using (con = new MySqlConnection(constring)) {  
     string selectquery = "SELECT * FROM sbis.sb_members WHERE lname ='" + this.lnametxtbox.Text + "' AND fname = '" + this.fnametxtbox.Text + "' AND mname ='" + this.mnametxtbox.Text + "' "; 
     MySqlCommand command2 = new MySqlCommand(selectquery, con); 

     string insertquery = "INSERT INTO sbis.sb_members (lname, fname, mname, position, appointment, address, contactnum, birthdate, civilstatus, educattainment, eligibility, terms_idterms, polparties_id, sex) VALUES (@lname, @fname, @mname, @position, @appointment, @address, @contactnum, @birthdate, @civilstatus, @educattainment, @eligibility, @terms_idterms, @polparties_id, @sex)"; 
     MySqlCommand cmd = new MySqlCommand(insertquery, con); 
     string lname, fname, mname, address, contactnum, educattainment; 

     lname = lnametxtbox.Text; 
     fname = fnametxtbox.Text; 
     mname = mnametxtbox.Text; 
     address = addresstxtbox.Text; 
     contactnum = contacttxtbox.Text; 
     educattainment = eductxtbox.Text; 
     var birthdate = birthdatedtp.Value.Date; 

     cmd.Parameters.AddWithValue("@lname", lname); 
     cmd.Parameters.AddWithValue("@fname", fname); 
     cmd.Parameters.AddWithValue("@mname", mname); 
     cmd.Parameters.AddWithValue("@address", address); 
     cmd.Parameters.AddWithValue("@contactnum", contactnum); 
     cmd.Parameters.AddWithValue("@position", position); 
     cmd.Parameters.AddWithValue("@appointment", appointment); 
     cmd.Parameters.AddWithValue("@birthdate", birthdate); 
     cmd.Parameters.AddWithValue("@sex", sex); 
     cmd.Parameters.AddWithValue("@eligibility", eligibility); 
     cmd.Parameters.AddWithValue("@civilstatus", civilstatus); 
     cmd.Parameters.AddWithValue("@terms_idterms", terms); 
     cmd.Parameters.AddWithValue("@polparties_id", polparties); 
     cmd.Parameters.AddWithValue("@educattainment", educattainment); 

     try { 
      con.Open(); 
      MySqlDataReader cr = command2.ExecuteReader(); 
      while (cr.Read()) { 
       if (cr.HasRows == true) { 
        MessageBox.Show("Member already exists.", "Duplicate Entry", MessageBoxButtons.OK, MessageBoxIcon.Warning); 
       } else if (cr.HasRows == false) { 
        if (cmd.ExecuteNonQuery() > 0) { 
         MessageBox.Show("Saved", "Save", MessageBoxButtons.OK, MessageBoxIcon.Information); 
        } 
       } 
      } 
      cr.Close();  
     } catch (Exception ex) { 
      MessageBox.Show(ex.Message); 
     } 
    } 
} 

回答

2

你似乎知道如何使用準備好的語句。所以這是一個謎,爲什麼在使用字符串連接你的SELECT

string selectquery = "SELECT * FROM sbis.sb_members WHERE lname ='" + this.lnametxtbox.Text + "' AND fname = '" + this.fnametxtbox.Text + "' AND mname ='" + this.mnametxtbox.Text + "' "; 

但好消息是,你不需要這個選擇的。你可以扔掉它。並利用mysql中的IGNORE特性。或者如果你想顯示那個成員已經存在的消息,就趕上例外!

using (con = new MySqlConnection(constring)) 
    { 

     string insertquery = "INSERT INTO sbis.sb_members (lname, fname, mname, position, appointment, address, contactnum, birthdate, civilstatus, educattainment, eligibility, terms_idterms, polparties_id, sex) VALUES (@lname, @fname, @mname, @position, @appointment, @address, @contactnum, @birthdate, @civilstatus, @educattainment, @eligibility, @terms_idterms, @polparties_id, @sex)"; 
     MySqlCommand cmd = new MySqlCommand(insertquery, con); 
     string lname, fname, mname, address, contactnum, educattainment; 


     lname = lnametxtbox.Text; 
     fname = fnametxtbox.Text; 
     mname = mnametxtbox.Text; 
     address = addresstxtbox.Text; 
     contactnum = contacttxtbox.Text; 
     educattainment = eductxtbox.Text; 
     var birthdate = birthdatedtp.Value.Date; 

     cmd.Parameters.AddWithValue("@lname", lname); 
     cmd.Parameters.AddWithValue("@fname", fname); 
     cmd.Parameters.AddWithValue("@mname", mname); 
     cmd.Parameters.AddWithValue("@address", address); 
     cmd.Parameters.AddWithValue("@contactnum", contactnum); 
     cmd.Parameters.AddWithValue("@position", position); 
     cmd.Parameters.AddWithValue("@appointment", appointment); 
     cmd.Parameters.AddWithValue("@birthdate", birthdate); 
     cmd.Parameters.AddWithValue("@sex", sex); 
     cmd.Parameters.AddWithValue("@eligibility", eligibility); 
     cmd.Parameters.AddWithValue("@civilstatus", civilstatus); 
     cmd.Parameters.AddWithValue("@terms_idterms", terms); 
     cmd.Parameters.AddWithValue("@polparties_id", polparties); 
     cmd.Parameters.AddWithValue("@educattainment", educattainment); 

     try 
     { 
      if (cmd.ExecuteNonQuery() > 0) 
      { 
        MessageBox.Show("Saved", "Save", MessageBoxButtons.OK, MessageBoxIcon.Information); 
      } 
     } 
     catch (Exception ex) 
     { 
      if (ex.Message.ToLower().Contains("duplicate key")) 
      { 
        MessageBox.Show("Member already exists.", "Duplicate Entry", MessageBoxButtons.OK, MessageBoxIcon.Warning); 
      } 
      else { 
        MessageBox.Show(ex.Message); 
      } 
     } 
    } 
} 

上面的例子捕獲重複鍵錯誤,以顯示該用戶是否存在,而,忽略錯誤的消息。

當然,這一切都假定您已經在不希望重複的列上創建了一個UNIQUEPRIMARY KEY約束。這是數據庫設計的基本部分。標準做法是依靠數據庫來執行唯一性和其他約束,而不是在應用程序級別執行它

+0

感謝您的回覆,我嘗試了INSERT IGNORE,但每次保存時我都會得到重複的條目。 – nolimits

+0

非常感謝您的幫助,非常感謝。 – nolimits

+0

高興已經幫助所有最好與你的項目 – e4c5

0

while (cr.Read())是問題,如果(讀)表爲空(不重複),則循環從不運行。

而是因爲你有競爭條件的做法是不好的。如果有人在你插入行後檢查了沒有?要真正實施約束,只需添加

ALTER TABLE sbis.sb_members ADD UNIQUE (lname, fname, mname) 

現在您可以避免重複。

+0

感謝您的想法,真的很感激。 – nolimits