2013-03-11 31 views
-4

經營業務接入層:獲取麻煩登錄頁面3 - 輪胎結構

public static int login(string userlogin, string pwdlogin) 
    { 
     SqlConnection con = new SqlConnection(); 
     con.ConnectionString = GetConnectionString(); 
     con.Open(); 
     int id = 0; 
     string selectstr = "SELECT UserName, Password FROM Registration WHERE UserName = '" + userlogin.Trim() + "' AND Password = '" + pwdlogin.Trim() + "'"; 
     SqlCommand cmd = new SqlCommand(); 
     cmd.CommandText = selectstr; 
     cmd.CommandType = System.Data.CommandType.Text; 
     cmd.Connection = con; 
     id = cmd.ExecuteNonQuery(); 
     cmd = null; 
     con.Close(); 
     return id; 
    } 

Login.cs

protected void Button1_Click(object sender, EventArgs e) 
    { 
     int id = BusinessAccessLayer.login(userlogin.Text.Trim(), pwdlogin.Text.Trim()); 
     if (id > 0) 
     { 
      message.Text = " valid"; 
     } 
     else 
     { 
      message.Text = "in valid"; 
     } 
    } 
+1

什麼是你的問題? – 2013-03-11 06:58:34

+0

你已經向我們展示了代碼,但沒有告訴我們任何有關錯誤的信息。請閱讀http://tinyurl.com/so-list。 (無可否認,我可以看到四個重要問題,只是瀏覽它,但我不知道這些問題是否與您所看到的有關...) – 2013-03-11 06:58:48

+0

每次顯示無效用戶 – yash 2013-03-11 06:59:34

回答

0

ExecuteNonQuery用於對於UPDATE,INSERT和DELETE語句。 對於SELECT語句,使用ExecuteReader

public static int login(string userlogin, string pwdlogin) 
{ 
     SqlConnection con = new SqlConnection(); 
     con.ConnectionString = GetConnectionString(); 
     con.Open(); 
     int id = 0; 
     string selectstr = "SELECT UserName, Password FROM Registration WHERE UserName = '" + userlogin.Trim() + "' AND Password = '" + pwdlogin.Trim() + "'"; 
     SqlCommand cmd = new SqlCommand(); 
     cmd.CommandText = selectstr; 
     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; 
} 
+0

這將返回「具有給定用戶名和密碼的用戶數量」,而不是正確用戶的* ID *。它也沒有解決代碼中其他任何可怕的問題。 – 2013-03-11 07:10:56

+0

完成它的工作感謝很多 – yash 2013-03-11 07:13:01

+0

@JonSkeet:由OP提供的if語句只檢查'id> 0'是否...如果'id> 0'那麼它是一個有效的,所以我簡單的代碼與'id ++ '。 – 2013-03-11 07:13:33

0

如果你想要一個結果,你不能使用.ExecuteNonQuery。使用.ExecuteReader。

public static int login(string userlogin, string pwdlogin) 
{ 
    SqlConnection con = new SqlConnection(); 
    con.ConnectionString = GetConnectionString(); 
    con.Open(); 
    int id = 0; 
    string selectstr = "SELECT UserId FROM Registration WHERE UserName = '" + userlogin.Trim() + "' AND Password = '" + pwdlogin.Trim() + "'"; 
    SqlCommand cmd = new SqlCommand(); 
    cmd.CommandText = selectstr; 
    cmd.CommandType = System.Data.CommandType.Text; 
    cmd.Connection = con; 

    SqlDataReader reader = cmd.ExecuteReader(); 
    reader.Read(); 
    id = reader.GetInt32("UserId"); 

    reader.Close(); 
    con.Close(); 

    return id; 
} 
+0

您應該在完成後關閉閱讀器。 – 2013-03-11 07:05:42

+0

哎呦。固定,thx – hcb 2013-03-11 07:06:27

6

好了,有什麼錯在這裏有許多東西:

1)您應該使用using語句,以確保您關閉連接和命令,即使引發異常

2)您應該使用參數化的SQL,而不是將值直接放入您的SQL語句中,以避免SQL注入攻擊

3)您似乎以純文本格式存儲密碼。 不要這樣做。使用鹽漬散列或類似的東西(理想情況下計算速度慢)。

4)您忽略了.NET命名約定;方法應該在PascalCase中

5)您的SQL從不查看任何似乎與用戶ID相關的字段。目前還不清楚你希望ExecuteNonQuery返回什麼,但是如果你想要實際的ID,你需要在SQL中引用它。 (即使最初你只是想知道用戶的密碼是否有效,我強烈懷疑在某些時候你會想要使用真實的用戶ID,所以你應該讓你的代碼返回它。如果你真的只是想知道的密碼是否是有效的,你應該

6)您使用ExecuteNonQuery當你的命令明確查詢更改方法的返回類型爲bool)。改爲使用ExecuteReaderExecuteScalar。 (ExecuteNonQuery是爲插入,刪除和更新語句,並返回你受命令行數。)

所以喜歡的東西:

public static int Login(string user, string password) 
{ 
    using (var conn = new SqlConnection(GetConnectionString())) 
    { 
     conn.Open(); 
     string sql = "select Id, PasswordHash from logins where [email protected]"; 
     using (var command = new SqlCommand(sql)) 
     { 
      command.Parameters.Add("@Username", SqlDbType.NVarChar).Value = user; 

      using (var reader = command.ExecuteRead()) 
      { 
       if (reader.Read()) 
       { 
        int id = reader.GetInt32(0); 
        string hash = reader.GetString(1); 
        // TODO: Hash provided password with the same salt and compare 
        // results 
        if (CheckPassword(password, hash)) 
        { 
         return id; 
        } 
       } 
       return 0; // Or use an int? return type and return null 
      } 
     } 
    } 
}