SqlDataReader每隔一行读取一次

本文关键字:一次 读取 一行 SqlDataReader | 更新日期: 2023-09-27 18:19:37

我试着浏览了几个小时的代码,试图找出哪里出了问题,但谷歌似乎也没有答案。基本上我正在运行这个代码:

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+"对照"+CompareUsername";Log.Debug(ReceivedPassword+"against"+ComparePassword);"

帮助我查看阅读器正在检查哪一行。我尝试了六行,每行都有唯一的用户名和密码,结果基本上显示,只有第1、3和5行由读取器根据用户的输入进行检查。因此,如果我试图使用第2、4或6行的用户名和密码登录我的客户端,我会收到一个错误,说我的登录失败。有人能解释为什么会发生这种情况吗?

SqlDataReader每隔一行读取一次

在您当时没有找到登录名的情况下,您有一个额外的Reader.Read()调用。这是跳到下一个记录,然后主循环的Reader.Read()转到下一个。

不过,你不需要像这样循环。生成一个按用户名查找记录的查询。如果没有记录,则登录失败。如果有,请检查密码。

while块内的if语句上有第二个reader.Read()。这导致您的代码跳过记录。

为了简单起见,您可以直接从数据库中查询。

以下是用于检查数据库中是否存在收到的用户名和密码的示例代码:

string sql = @"SELECT username,password FROM users 
             WHERE username=@username and password = @password";
SqlCommand userSELECTcom = new SqlCommand(sql, myConnection);
userSELECTcom.Parameters.AddWithValue(@username, ReceivedUsername);
userSELECTcom.Parameters.AddWithValue(@password, ReceivedPassword);
using(SqlDataReader reader = userSELECTcom.ExecuteReader())
{
   ValidLogin = reader.HasRows; 
}
else if (CompareUsername != ReceivedUsername || ComparePassword != ReceivedPassword)
{
if (!reader.Read())  //remove this condition it will skip the current loop                             
{
Log.Debug(ReceivedUsername + " has not logged in successfully with password: " + ReceivedPassword);
myConnection.Close();//close sql conn
reader.Close();//close sql data reader
return ValidLogin;
}
}