這是我的代碼:數據讀取器和無效的嘗試時不存在數據讀取
protected void logujButton_Click(object sender, EventArgs e)
{
string user = "data source=myHostServer; database = myDataBase; user id=myLogin; password=myPassword";
SqlConnection con2 = new SqlConnection(user);
con2.Open();
string loguj = "select count(*) from uzytkownik where Login = '"+ logujTextBox.Text +"'";
SqlCommand command = new SqlCommand(loguj, con2);
int wartosc = Convert.ToInt32(command.ExecuteScalar().ToString());
con2.Close();
if (wartosc == 1)
{
con2.Open();
SqlCommand pobierzHaslo = new SqlCommand("select Haslo from uzytkownik where Login = '" + logujTextBox.Text + "'", con2);
SqlDataReader rdr = pobierzHaslo.ExecuteReader();
string haslo = rdr["Haslo"].ToString();
if (haslo == hasloTextBox.Text)
{
errorLabel.Text = "Prawidlowe Haslo !";
}
else
{
errorLabel.Text = "Zle haslo !";
}
}
else
{
errorLabel.Text = "Taki uzytkownik nie istnieje !";
}
}
當我按下按鈕,這個錯誤是出現:「無效的嘗試時,不存在數據讀」。你能告訴我,我犯了什麼錯誤嗎?感謝您的建議!
調試您的代碼並告訴我們發生了哪一行。 –
這行有一個問題「string haslo = rdr [」Haslo「]。ToString();」 – Shagohad
請閱讀[參數化查詢](http://en.wikipedia.org/wiki/Prepared_statement)。您當前的方法很容易受到SQL注入攻擊。 –