2014-10-09 66 views
-3

我正在做一個登錄和註冊表格,當我嘗試註冊它把用戶名和密碼在SQL數據庫中,但它這樣做:
enter image description here
我的項目看起來是這樣的:Parameters.Add在SQL項目C#

 static public void Insert(string _userName) 
    { 
     try 
     { 
      connection.Open(); 
      SqlCeCommand commandInsert = new SqlCeCommand("INSERT INTO [Table](username) VALUES(@userName)", connection); 
      commandInsert.Parameters.Add("@userName", _userName); 
      commandInsert.ExecuteNonQuery(); 
     } 
     catch (SqlCeException expection) 
     { 
      MessageBox.Show(expection.ToString()); 
     } 
     finally 
     { 
      connection.Close(); 
     } 
    } 



    static public void Insertt(string _password) 
    { 
     try 
     { 
      connection.Open(); 
      SqlCeCommand commandInsert = new SqlCeCommand("INSERT INTO [Table](password) VALUES(@Password)", connection); ; 



      commandInsert.Parameters.Add("@password", _password); 

      commandInsert.ExecuteNonQuery(); 
     } 
     catch (SqlCeException expection) 
     { 
      MessageBox.Show(expection.ToString()); 
     } 
     finally 
     { 
      connection.Close(); 
     } 
    } 

和按鈕註冊看起來像這樣:

 private void button1_Click(object sender, EventArgs e) 
      { 
       if (insertBox.Text != "" || deleteBox.Text != "") 
       { 
        SQLFunctions.Insert(insertBox.Text); 
        SQLFunctions.Insertt(deleteBox.Text); 
        SQLFunctions.Refresh(this.dataGridView1); 

       } 
       else 
       { 
        MessageBox.Show("login failed"); 
       } 
      } 

感謝您的幫助

+4

請說明問題,不只是把我們的程序,並且希望我們的調試/修復它爲您 – Steve 2014-10-09 17:20:46

+1

你爲什麼將在單獨的記錄的用戶名和密碼?爲什麼有兩個功能呢?是什麼讓'Insertt'與'Insert'不同?另外,請不要以純文本存儲用戶密碼。如果用戶使用他們的密碼信任您,請妥善散列這些密碼,以免他們被讀取。 – David 2014-10-09 17:24:13

回答

3

你插入記錄:

這些功能
SQLFunctions.Insert(insertBox.Text); 
SQLFunctions.Insertt(deleteBox.Text); 

每插入一條記錄表。所以你最終得到兩個記錄應該不足爲奇。我想這會更有意義,只是插入一個記錄:

SQLFunctions.Insert(insertBox.Text, deleteBox.Text); 

而且在功能:

static public void Insert(string _userName, string _password) 
{ 
    // ... 
    SqlCeCommand commandInsert = new SqlCeCommand("INSERT INTO [Table](username, password) VALUES(@userName, @password)", connection); 
    commandInsert.Parameters.Add("@userName", _userName); 
    commandInsert.Parameters.Add("@password", _password); 
    // ... 
} 

每個值並不需要自己單獨的數據庫查詢,整個記錄可在單個查詢中插入/更新。


其他一些注意事項...

  • 謝謝使用參數化查詢。你會驚訝有多少人不會:)
  • 你正在用純文本存儲用戶密碼。請千萬不要這樣做。用戶密碼應該被散列,並且在存儲之後不應該被讀取。 (理想情況下,他們應該被散列馬上當您的代碼收到時,你應該只使用散列值的任何東西,這樣他們不會意外地結束在日誌轉儲)
  • 你可能想要考慮變量/函數/等的更有意義/正確的名稱。它將大大幫助您的調試工作,特別是在您創建更復雜的事情時。例如,InsertInsertt並不真正告訴你他們在做什麼,或者他們有什麼不同。另外,爲什麼insertBox有一個用戶名和deleteBox有一個密碼?這只是誤導。不要低估好命名的重要性。
0

我可以在這裏看到兩個可能導致您的問題的錯誤。

第一 - 在您的button1_Click情況下,您有:

if (insertBox.Text != "" || deleteBox.Text != "")

導致的第一個問題 - 在你的數據庫空列。相反,檢查是這樣的:

if (insertBox.Text != "" && deleteBox.Text != "")

其實更好用string.IsNullOrEmpty如此檢查:

if (!string.IsNullOrEmpty(insertBox.Text) && !string.IsNullOrEmpty(deleteBox.Text))

其次,你不需要和不能使用兩個查詢插入此。所以有兩個參數,使其一個查詢:

SqlCeCommand commandInsert = new SqlCeCommand("INSERT INTO [Table](username, password) VALUES(@username, @password)", connection); 

commandInsert.Parameters.Add("@username", _username); 
commandInsert.Parameters.Add("@password", _password);