SqlDataReader读取每隔一行?

时间:2014-08-28 04:00:52

标签: c# sql sql-server-2012-express

我尝试查看我的代码几个小时试图查看我哪里出错了,谷歌似乎也找不到答案。 基本上我运行的是这段代码:

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 +"反对" + ComparePassword); "

帮助我查看读者正在检查哪一行。我尝试了六行,每行有唯一的用户名和密码,结果基本上显示读者只检查第1,3和5行对用户的输入。因此,如果我尝试使用第2,4或6行的用户名和密码登录我的客户端,则会收到错误消息,说我的登录失败。任何人都可以解释为什么会这样吗?

4 个答案:

答案 0 :(得分:2)

在你没有找到登录时间的条件下,你有一个额外的Reader.Read()电话。这是跳到下一条记录,然后你的主循环Reader.Read()转到下一条记录。

但是,您不需要像这样循环。构建一个按用户名查找记录的查询。如果没有记录,则登录失败。如果有,请检查密码。

答案 1 :(得分:1)

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

答案 2 :(得分:0)

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;
}
}

答案 3 :(得分:0)

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

下面是检查db:

中是否存在收到的用户名和密码的示例代码
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; 
}