2013-03-30 25 views
1

這是我的代碼,我認爲我現在正在做的權利,但我真的不知道是什麼問題。 我正在用txtbox作爲用戶名和密碼的註冊表格,我用MD5加密密碼,我試圖刪除MD5加密思想,認爲它可能是prtoblem,但仍然當我刪除它時,問題仍然存在。我試圖用C#txtbox形式在mysql數據庫中插入數據,但是我總是得到一個錯誤?

ApareceCrudLib a = new ApareceCrudLib("localhost", "root", "", "cashieringdb"); 

      string query = "INSERT INTO register (username,password) " + 
          "VALUES(" + 
          "'" + txtUser.Text + "'," + 
          "MD5('" + txtPass.Text +"')"; 
      a.mysqlInsert(query); 
      MessageBox.Show("Account has been registered!"); 
      this.Close(); 

這是mysqlInsert

public void mysqlInsert(string query) 
     { 
      try 
      { 
       if (this.Open()) 
       { 
        MySqlCommand cmd = new MySqlCommand(query, conn); 
        cmd.ExecuteNonQuery(); 
        this.Close(); 
        System.Windows.Forms.MessageBox.Show("Record Inserted!"); 
       } 
      } 
      catch { this.Close(); System.Windows.Forms.MessageBox.Show("INSERT Record Error!"); } 
      return; 
     } 

,你可以看到我趕上與對話框中的錯誤所以基本上,如果它將無法插入或連接到數據庫的消息框我的課ApareceCrudLib代碼顯示「INSERT Record Error!」。順便說一句,只有在插入到數據庫時,Visual Studio沒有錯誤。

我認爲錯誤的地方在代碼中插入數據庫的查詢字符串=「INSERT

INTO register (username,password) " + 
           "VALUES(" + 
           "'" + txtUser.Text + "'," + 
           "MD5('" + txtPass.Text +"')"; 

也許逗號分號一段時間我無言以對。

嗨!rhughes這裏是錯誤的圖像!

enter image description here

+0

什麼是錯誤 –

+0

,你可以看到我趕上與對話框中的錯誤?所以基本上如果它將無法插入或連接到數據庫的消息框顯示「插入記錄錯誤!」順便說一句,沒有錯誤在Visual Studio中只插入到數據庫中 – JustNoobWillingToLearn

+0

答案是正確的,並且異常消息會告訴你:知道有錯誤是不夠的;你需要知道錯誤實際上是什麼 –

回答

1

SQL不正確。你有兩個開放「(」,只有一個閉合

+0

haha​​ha哦是的謝謝你是多麼愚蠢我是我搜索每個語法的錯誤,忘記注意關閉hahahhahahahaha – JustNoobWillingToLearn

3

您必須添加一個 「)」 你的查詢字符串。

string query = "INSERT INTO register (username,password) " + 
         "VALUES(" + 
         "'" + txtUser.Text + "'," + 
         "MD5('" + txtPass.Text +"'))"; 
               ^HERE 
+1

+1:你快了幾秒 – Pleun

1

爲了看到實際的錯誤,試試這個:

try 
{ 
    if (this.Open()) 
    { 
     MySqlCommand cmd = new MySqlCommand(query, conn); 
     cmd.ExecuteNonQuery(); 
     this.Close(); 
     System.Windows.Forms.MessageBox.Show("Record Inserted!"); 
    } 
} 
catch(Exception ex) 
{ 
    this.Close(); 
    System.Windows.Forms.MessageBox.Show(String.Format("INSERT Record Error! {0}", ex.Message)); 
} 
相關問題