首页 > 解决方案 > C# - SQL 中的 Reader 总是返回 false

问题描述

所以我试图在 C# 应用程序中创建一个登录系统,我使用 sql/mysql 作为我存储用户数据的数据库,当我执行阅读器时,我遇到了一个错误,然后我制作了 if 语句(如果读者阅读然后做某事)并且 if 语句总是返回 false,我不知道为什么,它没有给出任何异常,我已经将它放在 try 语句中但再次没有异常,所以我真的不知道出了什么问题。而且连接,查询和一切都很好(我已经仔细检查过)这是我的代码:

if (!OpenConnection(Connection)) return false;
                    using (var cmd =
                    new MySqlCommand(
                        "SELECT Login, Password, HWID, Premium, IP FROM Users WHERE Login = @Login AND Password = @Password" +
                        " AND HWID = @HWID AND Premium = @Premium AND IP = @IP",
                        Connection))
cmd.Parameters.AddWithValue("@Login", username);
                        cmd.Parameters.AddWithValue("@Password",
                        Encryption.Encrypt(password, Encryption.RandomBytes().ToString(), salt));
cmd.Parameters.AddWithValue("@HWID", hardware);
cmd.Parameters.AddWithValue("@Premium", 1);
cmd.Parameters.AddWithValue("@IP", Helpers.GetIpAddress());

var reader = cmd.ExecuteReader(); // < executing the reader everything is ok

if (reader.Read()) // < and then this is where it always returns false no matter what
    {
reader.Close();
Connection.Close();
return true
    }

reader.Close(); // this is where it goes when the reader is returning false
Connection.Close(); // this is where it goes when the reader is returning false
return false; // this is where it goes when the reader is returning false

标签: c#mysqlsql

解决方案


推荐阅读