c#
  • asp.net
  • 3-tier
  • 2013-04-09 73 views -3 likes 
    -3

    我有註冊頁面,我想檢查用戶名是否已經存在於數據庫或不在3層架構。我想檢查用戶名是否已經存在於我的數據庫表中?

    MyRegistration.cs:

    public static int checkusername(string user_txt) 
    { 
        int id2 = 0; 
        string selectstr = "select * from xyz where UserName = '" + user_txt + " ' "; 
        id2 = DataAccessLayer.ExecuteReader(selectstr); 
        return id2;  
    } 
    

    和後面的文本框的onclick事件的代碼:

    protected void txt_username_TextChanged(object sender, EventArgs e) 
    { 
        if (!string.IsNullOrEmpty(txt_username.Text)) 
        { 
        int id = xyz.checkusername(txt_username.Text.Trim()); 
        if (id > 0) 
        { 
         lblStatus.Text = "UserName Already Taken"; 
        } 
        else 
        { 
         lblStatus.Text = "UserName Available"; 
        } 
        } 
    
    } 
    

    DataAccessLayer:

    public static int ExecuteReader(string Query) 
    { 
        SqlConnection con = new SqlConnection(); 
        con.ConnectionString = GetConnectionString(); 
        con.Open(); 
        int id = 0;    
        SqlCommand cmd = new SqlCommand(); 
        cmd.CommandText = Query; 
        cmd.CommandType = System.Data.CommandType.Text; 
        cmd.Connection = con; 
        SqlDataReader reader = cmd.ExecuteReader(); 
        while (reader.Read()) 
        { 
        id++; 
        } 
        cmd = null; 
        reader.Close(); 
        con.Close(); 
        return id; 
    } 
    
    +1

    那麼這裏有什麼疑問? – Darshan 2013-04-09 09:58:37

    +8

    謹防SQL注入! – Keysharpener 2013-04-09 09:59:50

    +0

    你正在打開這樣的安全風險,你需要防止SQL注入。編輯:看起來像keysharpener打敗我! – 2013-04-09 10:00:43

    回答

    2

    我已經編輯了一些你的代碼的嘗試像下面......它會幫助你...

    文本改變事件:

    protected void txt_username_TextChanged(object sender, EventArgs e) 
        { 
         if (!string.IsNullOrEmpty(txt_username.Text)) 
         { 
          if (xyz.checkusername(txt_username.Text.Trim())) 
          { 
           lblStatus.Text = "UserName Already Taken"; 
          } 
          else 
          { 
           lblStatus.Text = "UserName Available"; 
          } 
         } 
    
        } 
    

    檢查用戶名:

    public bool CheckUsername(string user_txt) 
        { 
         bool Result; 
         Result = DataAccessLayer.ExecuteReader(user_txt); 
         return Result; 
        } 
    

    Excute Reader:

    public bool ExecuteReader(string user_txt) 
        { 
         SqlConnection con = new SqlConnection(); 
         con.ConnectionString = GetConnectionString(); 
         con.Open(); 
         SqlCommand cmd = new SqlCommand("select * from xyz where UserName = @UserID", con); 
         SqlParameter param = new SqlParameter(); 
         param.ParameterName = "@UserID"; 
         param.Value = user_txt; 
         cmd.Parameters.Add(param); 
         SqlDataReader reader = cmd.ExecuteReader(); 
         if (reader.HasRows) 
          return true; 
         else 
          return false; 
        } 
    
    0

    正如其他人所提到的,這種方法有潛在的嚴重安全問題。

    但是,您的問題可能在此處user_txt + " ' "。第二個'周圍的空間,特別是它之前的空間可能導致用戶名不符合預期。

    2

    通常,如果「選擇查詢」沒有找到具有參數user_txt的userName,那麼您的id2將以值-1結束。因此,相應的代碼是:

      if (id ==-1) 
           { 
            lblStatus.Text = "UserName Available"; 
           } 
          if (id>0) 
           { 
    
            lblStatus.Text = "UserName Already Taken"; 
           } 
    

    順便問一下,你的代碼是非常不安全的,你的數據庫可以使用SQL Injection很容易受到攻擊,我建議你要了解這個問題,並添加參數到查詢,以防止它。 C#有其方法來實現這一點。不要試圖修復對數據庫的訪問,只需從scrath開始記住SQL注入。

    +0

    不工作的兄弟 – yash 2013-04-09 10:14:00

    +0

    你需要改正「selectstr」作爲其他評論說 – 2013-04-09 10:16:45

    相關問題