2010-09-22 72 views
0

我有一個用戶名數據庫表,我試圖用它來比較用戶名/密碼。在ASP.NET中使用SQL適配器填充數據表問題

這是我的代碼,它不工作,我做錯了什麼?

DataTable dt = null; 

protected void btn_Click_Login(object sender, EventArgs e) 
{ 
    string query = string.Format("SELECT * FROM Users WHERE Username='{0}' AND Password='{1}'", txtUsername.Text, txtPassword.Text); 

    using (SqlConnection c = new SqlConnection(ConfigurationManager.ConnectionStrings["RBConnectionString"].ConnectionString)) 
    { 
     c.Open(); 

     using (SqlDataAdapter a = new SqlDataAdapter(query, c)) 
     { 
      DataTable t = new DataTable(); 
      a.Fill(t); 
     } 
    } 
    if (dt.Rows.Count > 0) 
    { 
     Session["Username"] = txtUsername.Text; 
     Session["Password"] = txtPassword.Text; 
     Response.Redirect("main.aspx"); 
     lblError.Text = "success"; 
    } 
    else 
    { 
     lblError.Text = "Wrong Username/Password combination"; 
    } 
} 

}

+0

你做了什麼錯誤? – anishMarokey 2010-09-22 11:20:17

+1

當你說它不工作,以什麼方式?你有錯誤嗎?作爲一個側面的問題(但是很重要),通過動態構建SQL來打開自己的SQL注入。您應該使用參數化的SQL。 – AdaTheDev 2010-09-22 11:22:11

+0

當我試圖對數據表中的行進行計數時,我得到一個空引用異常。調試器說:對象引用未設置爲對象的實例。 – 2010-09-22 18:52:23

回答

1

,我決定嘗試數據讀取器和得到它的工作:

protected void btn_Click_Login(object sender, EventArgs e) 
{ 

    SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["RbConnectionString"].ConnectionString); 
    conn.Open(); 
    string queryString = "SELECT * FROM [Users] WHERE [email protected] AND Password= @password"; 
    SqlCommand command = new SqlCommand(queryString, conn); 
    command.Parameters.AddWithValue("@username", txtUsername.Text); 
    command.Parameters.AddWithValue("@password", txtPassword.Text); 

    SqlDataReader reader = null; 
    reader = command.ExecuteReader(); 

    if (reader.Read()) 
    { 
     Session["Username"] = txtUsername.Text; 
     Session["Password"] = txtPassword.Text; 
     Response.Redirect("main.aspx"); 
    } 
    else 
    { 
     lblError.Visible = true; 
     lblError.Text = "Incorrect Username/Password Combination"; 
    } 
    conn.Close(); 

} 
+0

這是防止SQL注入的正確方法嗎? – 2010-09-23 15:36:30

+0

是的,這是避免SQL注入的正確方法。 – 2010-09-23 15:51:40

+0

這對N層應用程序沒有好處......您應該使用表格適配器和代碼分離。 – PriceCheaperton 2015-03-21 13:09:30

1

嘗試創建一個SqlCommand握住你的查詢。

SqlCommand cmd = new SqlCommand(query, c); 

using (SqlDataAdapter a = new SqlDataAdapter(cmd)) 
{ 
    DataTable t = new DataTable(); 
    a.Fill(t); 
} 

我不是100%肯定這是你的問題,但早在時,我曾經使用ADO.NET天(L2SQL/EF之前,黑暗的日子確實),我似乎記得DataTable的問題和SqlDataAdapter。

從我記得 - 你不能用基於原始查詢字符串的SqlDataAdapter填充DataTable - 你需要使用SqlCommand。但我相信這可以用DataSet來完成。

因此,要麼更改爲SqlCommand,要麼更改爲DataSet。

1

很可能您正在使用錯誤的數據表來檢查返回的行數。

檢查數據表的t和dt實例。

1

您填寫t

DataTable t = new DataTable(); 
a.Fill(t); 

但讀dt

if (dt.Rows.Count > 0) 
+0

哈哈,甚至沒有注意到!你也許是對的。讓我猜猜,你真的很擅長那些「哪裏有書」? – RPM1984 2010-09-22 11:29:46

+0

嘗試對數據表中的行進行計數時,我收到空引用異常。調試器說:對象引用未設置爲對象的實例。 – 2010-09-22 18:51:34

+1

@nick - 你提到了變化@abatishchev嗎? – RPM1984 2010-09-22 22:10:43

0

什麼錯誤,你越來越不清晰。但我覺得你的聯繫是開放的,永遠不會關閉。嘗試

c.Close();