MySqlDataReader没有读

时间:2016-09-29 18:56:02

标签: c#

所以,我正在为我的公司制作一个小应用程序,让用户登录并访问特定数据,但是,在服务器端查询数据库,MySqlDataReader没有读取,正如我在下面指出的那样代码:

public string HandleLogin(string uname, string pwd)
    {
        MySqlDataReader READER;
        int count = 0;
        int clearance = 0;
        if (uname == "" || pwd == "")
            return "";
        CONN = new MySqlConnection();
        CONN.ConnectionString = Config.CONNSTRING;
        string query = "select * from members where username='" + uname + "' and password='" + pwd + "'";
        try
        {
            CONN.Open();
            COMMAND = new MySqlCommand(query, CONN);
            READER = COMMAND.ExecuteReader();
            while(READER.Read())
            {
                count = count + 1; //<-- this should happen, but it doesnt
                if (count == 1)
                    clearance = READER.GetInt32("clearance");
                else
                    return "";
            }
            if (count == 1)
                return Convert.ToString(clearance);
            else
                return "";
        }
        catch(Exception ex)
        {
            Console.WriteLine(ex.Message);
            return "";
        }
        finally
        {
            CONN.Dispose();
        }
    }

我确定它应该读取1行,询问它找到的行数,它表示0,但我检查了所有值,并且它们完全匹配数据库值,但它没有为查询读取行。这可能是一个非常愚蠢的错误,但我无法找到问题。

1 个答案:

答案 0 :(得分:0)

所以,我再次放置一个断点并重新检查变量,变量很好用Console.WriteLine,但是,在查询中它看起来像这样:

"select * from members where username='matthias' and password='M20UjefsltnFCTDsCL1temvFOtBcnN8gNtGbQevVTkkG8jCXZlbxQ3ykOTx0fV4Ls2VnAzOzx2F5CTY+jEDSzg==\0\0\0\0'"

但它应该是: "select * from members where username='matthias' and password='M20UjefsltnFCTDsCL1temvFOtBcnN8gNtGbQevVTkkG8jCXZlbxQ3ykOTx0fV4Ls2VnAzOzx2F5CTY+jEDSzg=='"

由于客户端和服务器之间的数据加密以及填充它最后添加了\ 0 \ 0 \ 0 \ 0,我应该早点看到这一点,感谢那些想要帮助我的人,我解决了它在查询之前添加此函数:

pwd = pwd.Replace("\0", "");