2013-02-27 125 views
0

我正在檢查數據庫表「用戶」以查看「用戶名」是否存在,以便無法再次創建相同的用戶名。我希望這是一個驗證器,所以如果用戶名存在,消息框會顯示它存在。檢查數據庫中是否已存在用戶名

請指導我這個問題,我有以下代碼到目前爲止後面的按鈕來添加,並檢查用戶名是否存在:

private void btnSignupNew_Click(object sender, EventArgs e) 
     { 

      if (txtUsername.Text == "") 
      { 
       errorUsername.SetError(txtUsername, "Enter A Username"); 
      } 

      else if (txtPassword.Text == "") 
      { 
       errorPassword.SetError(txtPassword, "Enter A Valid Password"); 
      } 

       //so if there isnt no error in the fields itll go on and add the data in to the database. 
      else{ 

      //instance of sqlConnection 
      SqlConnection con = new SqlConnection("Data Source=etc"); 

      //instance of sqlCommand 
      SqlCommand cmd = new SqlCommand("INSERT INTO [User] values ('" + txtForename.Text + "', '" + txtSurname.Text + "', '" + txtUsername.Text + "', '" + txtPassword.Text + "')", con); 
      con.Open(); 
      cmd.ExecuteNonQuery(); 

      //query executed correcty or not 
      con.Close(); 
+6

您有SQL注入漏洞。 – SLaks 2013-02-27 17:24:01

+6

**不要以純文本**存儲密碼。 – SLaks 2013-02-27 17:24:47

+3

使用[參數化查詢](http://www.codinghorror.com/blog/2005/04/give-me-parameterized-sql-or-give-me-death.html)來防止_SQL Injection_攻擊。 – 2013-02-27 17:24:58

回答

7

作爲一個良好的初步實踐,嘗試使用Parameters避免SQL,讓您持久注射。

嘗試一些liek這樣的:

private void btnSignupNew_Click(object sender, EventArgs e) 
{ 

    if (txtUsername.Text == "") 
    { 
     errorUsername.SetError(txtUsername, "Enter A Username"); 
    } 
    else if (txtPassword.Text == "") 
    { 
     errorPassword.SetError(txtPassword, "Enter A Valid Password"); 
    } 
    else 
    { 
     using (SqlConnection con = new SqlConnection("Data Source=etc")) 
     { 
      con.Open(); 

      bool exists = false; 

      // create a command to check if the username exists 
      using (SqlCommand cmd = new SqlCommand("select count(*) from [User] where UserName = @UserName", con)) 
      { 
       cmd.Parameters.AddWithValue("UserName", txtUsername.Text); 
       exists = (int)cmd.ExecuteScalar() > 0; 
      } 

      // if exists, show a message error 
      if (exists) 
       errorPassword.SetError(txtUsername, "This username has been using by another user."); 
      else 
      { 
          // does not exists, so, persist the user 
       using (SqlCommand cmd = new SqlCommand("INSERT INTO [User] values (@Forname, @Surname, @Username, @Password)", con)) 
       { 
        cmd.Parameters.AddWithValue("Forname", txtForname.Text); 
        cmd.Parameters.AddWithValue("Surname", txtSurname.Text); 
        cmd.Parameters.AddWithValue("UserName", txtUsername.Text); 
        cmd.Parameters.AddWithValue("Password", txtPassword.Text); 

        cmd.ExecuteNonQuery(); 
       }    
      } 

      con.Close(); 
     } 
    } 
} 
+0

這個作品謝謝!但我有一個小問題,如果用戶名存在我得到錯誤信息,但因爲我點擊了「註冊」按鈕,它會帶我回到登錄頁面。任何想法如何瀰漫?謝謝。順便說一下,我在底部的代碼是:MessageBox.Show(「成功註冊」); Form1 signin = new Form1(); signin.Show(); this.Close(); } } – bandaa 2013-02-27 17:57:34

+0

這只是關於他驗證用戶。你的問題聽起來像是一個身份驗證問題,它可能在另一個線程中,以避免延長這一問題。如果你正在改變一個已存在的用戶,你可以改變你的查詢到'... Where UserName = @ UserName和Id <> @Id「'來避免檢查一個存在的用戶。 – 2013-02-27 18:30:13

相關問題