我嘗試通過我的代碼幾個小時試圖看看我錯了什麼地方,谷歌似乎也沒有答案。 基本上我運行此代碼:SqlDataReader每隔一行讀取一次?
public bool LoginRequest(string ReceivedUsername, string ReceivedPassword)
{
bool ValidLogin = false;
try
{
using (SqlConnection myConnection = new SqlConnection(ConnectString))
{
myConnection.Open();
Log.Debug("Succesful sql connection");
SqlCommand userSELECTcom = new SqlCommand("SELECT username,password FROM users;", myConnection);
SqlDataReader reader = userSELECTcom.ExecuteReader();
//verify login
while (reader.Read())
{
CompareUsername = reader["username"].ToString();
ComparePassword = reader["password"].ToString();
Log.Debug(ReceivedUsername + " against " + CompareUsername);
Log.Debug(ReceivedPassword + " against " + ComparePassword);
if (CompareUsername == ReceivedUsername && ComparePassword == ReceivedPassword)
{
ValidLogin = true;
Log.Debug(ReceivedUsername + " has logged in successfully!!!");
myConnection.Close();//close sql conn
reader.Close();//close sqldatareader
return ValidLogin;
}
else if (CompareUsername != ReceivedUsername || ComparePassword != ReceivedPassword)
{
if (!reader.Read())
{
Log.Debug(ReceivedUsername + " has not logged in successfully with password: " + ReceivedPassword);
myConnection.Close();//close sql conn
reader.Close();//close sql data reader
return ValidLogin;
}
}
}
//end of verify sequence
}
}
//logging any login request issues
catch (Exception e)
{
Log.Debug(e);
}
return ValidLogin;
}
我已經設置了一個記錄程序,告訴的代碼被執行我的一切多數民衆贊成在發生。這些行:「Log.Debug(ReceivedUsername +」against「+ CompareUsername); Log.Debug(ReceivedPassword +」against「+ ComparePassword);」
幫助我查看讀者正在檢查哪一行。我嘗試了六行,每行都有唯一的用戶名和密碼,結果基本上表明只有第1,3,5行被讀者檢查,以防止用戶輸入。因此,如果我嘗試使用第2,4或6行中的用戶名和密碼登錄客戶端,我收到一條錯誤信息,說我的登錄失敗。誰能解釋爲什麼會發生這種情況?
只是一個方面說明。如果您在連接周圍使用「使用」關鍵字,則不需要您撥打電話。關閉它。它繼承了IDisposable,它使用呼叫給你。 – 2014-08-28 04:05:43
通常,直接從數據庫查詢而不是從表中檢索所有數據是一種好的做法。建議使用where子句。 – Hassan 2014-08-28 04:06:16
絕對要考慮到Cubicle.Jockey。你能解釋一下哈桑嗎? – Jaja 2014-08-28 04:11:13