简体   繁体   English

如何存储mysql查询的结果

[英]How to store result from mysql query

How do I store the results from a mysql query for use in other classes most efficiently?如何最有效地存储来自 mysql 查询的结果以在其他类中使用?

I've tried the following code, which executes properly and stores all data in reader as it should.我已经尝试了以下代码,它可以正确执行并将所有数据存储在阅读器中。 Reading the DataReader here works fine if I want to!如果我愿意,在这里阅读 DataReader 可以正常工作!

public class DatabaseHandler
{
    public void MySqlGetUserByName(string input_username, MySqlDataReader reader)
    {
        try
        {
            _database.Open();
            string query = "SELECT * FROM users WHERE username = '@input'";
            MySqlParameter param = new MySqlParameter(); param.ParameterName = "@input"; param.Value = input_username;
            MySqlCommand command = new MySqlCommand(query, _database);
            command.Parameters.Add(param);
            reader = command.ExecuteReader();
            _database.Close();
        }
        catch(Exception ex)
        {
            Console.WriteLine(ex.ToString());
        }
    }
}

But when I try to read the same DataReader here, it is null and throws an exception (right after Debug6).但是当我尝试在这里读取相同的DataReader 时,它为 null 并引发异常(在 Debug6 之后)。

public class LoginHandler
{
    public static void UserAuth(Client user, string input_username, string input_password)
    {
        DatabaseHandler dataBase = new DatabaseHandler();
        MySqlDataReader dataReader = null; 
        dataBase.MySqlGetUserByName(input_username, dataReader);

        Console.WriteLine("Debug6");
        if (!dataReader.HasRows)
        {
            user.SendChatMessage("No match found.");
            return;
        }

        while (dataReader.Read())
        {
            user.SetData("ID", (int)dataReader[0]);
            user.SetData("username", (string)dataReader[1]);
            user.SetData("email", (string)dataReader[2]);
            user.SetData("password", (string)dataReader[3]);
        }
        dataReader.Close();
    }
}

Please let me know how to make this work, or if there is a more efficient way of doing this without limiting the function of MySqlGetUserByName.请让我知道如何进行这项工作,或者是否有更有效的方法可以在不限制 MySqlGetUserByName 的功能的情况下执行此操作。 The purpose of it is to input a name and a place to store all info from the match in the database.它的目的是输入一个名字和一个地方来存储数据库中匹配的所有信息。

Also, feel free to drop in any other suggestions that could make the code more efficient.此外,请随意提出任何其他可以提高代码效率的建议。

You could change your MySqlGetUserByName to return a User instance if all goes well, otherwise you return a null instance to the caller (Or you can thrown an exception, or you can set a global error flag in the DatabaseHandler class..., but to keep things simple I choose to return a null)如果一切顺利,您可以更改 MySqlGetUserByName 以返回一个 User 实例,否则您将向调用方返回一个空实例(或者您可以抛出异常,或者您可以在 DatabaseHandler 类中设置一个全局错误标志...,但要保持简单,我选择返回空值)

public class DatabaseHandler
{
    public User MySqlGetUserByName(string input_username)
    {
        User result = null;
        try
        {
            string query = "SELECT * FROM users WHERE username = @input";
            using(MySqlConnection cnn = new MySqlConnection(......))
            using(MySqlCommand command = new MySqlCommand(query, cnn))
            {
                cnn.Open();
                command.Parameters.AddWithValue("@input", input_username);
                using(MySqlDataReader dataReader = command.ExecuteReader())
                {
                   if (dataReader.Read())
                   {
                       result = new User();
                       result.ID = Convert.ToInt32(dataReader[0]);
                       ..... and so on with the other user properties ....
                   }
                }
            }
        }
        catch(Exception ex)
        {
            Console.WriteLine(ex.ToString());
        }
        // Return the user to the caller. If we have not found the user we return null
        return result;
    }
}

In the same way the caller handles the situation以同样的方式调用者处理情况

public class LoginHandler
{
    public static void UserAuth(string input_username, string input_password)
    {
        DatabaseHandler dataBase = new DatabaseHandler();
        User result = dataBase.MySqlGetUserByName(input_username);

        // If we have not found the user we have a null in the variable 
        if(result == null)
        {
            // Send your message using a static method in the user class
            // User.SendMessage("User with username {input_username} not found!");
        }
        else
        {
            // User ok. return it? or do something with its data?
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM