我有一個用戶名數據庫表,我試圖用它來比較用戶名/密碼。在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";
}
}
}
你做了什麼錯誤? – anishMarokey 2010-09-22 11:20:17
當你說它不工作,以什麼方式?你有錯誤嗎?作爲一個側面的問題(但是很重要),通過動態構建SQL來打開自己的SQL注入。您應該使用參數化的SQL。 – AdaTheDev 2010-09-22 11:22:11
當我試圖對數據表中的行進行計數時,我得到一個空引用異常。調試器說:對象引用未設置爲對象的實例。 – 2010-09-22 18:52:23