MySQL Async - 我如何使用它?

时间:2017-04-09 12:07:38

标签: c# mysql asynchronous

我尝试运行自己的TCP应用程序,现在我将检查用户是否退出。

我的问题是:我如何使用mySQL Async?

        public static bool CheckUserLogin(string Username,string Password){
        try{
            if(string.IsNullOrEmpty(Username) || string.IsNullOrEmpty(Password)){ return false; }
            if (!System.Text.RegularExpressions.Regex.IsMatch(Username, "^[a-zA-Z0-9\x20]+$")) { return false; }
            if (!System.Text.RegularExpressions.Regex.IsMatch(Password, "^[a-zA-Z0-9\x20]+$")) { return false; }
            bool UserExit = false;
            DataBase_Connection.DB_Conn.OpenAsync();
            try{
                string c_query = "SELECT COUNT(*) FROM `KoN_Account` WHERE BINARY `AccountName` = @U_Name AND `HashPass` = @P_Pass LIMIT 1;";
                using (MySqlCommand CU_cmd = new MySqlCommand(c_query, db_Conn)){
                    CU_cmd.Parameters.AddWithValue("@U_Name", Username);
                    CU_cmd.Parameters.AddWithValue("@P_Pass", Password);
                    var count = CU_cmd.ExecuteScalar();
                    UserExit = Convert.ToBoolean(count);
                }
            }
            catch{
            }
            DataBase_Connection.DB_Conn.CloseAsync();
            return UserExit;
        }
        catch (Exception ex){
            LoginConsole.WriteConsoleLoginParams(System.ConsoleColor.Red, "ERROR CheckUserLogin", ex.ToString() + "\nFailed to connect to the database (server shutdown...)");
        }
        return false;
    }

我的问题是我做的是什么?使用 OpenAsync CloseAsync

另外我如何使用:Async方式的ExecuteScalar?

感谢您的帮助

1 个答案:

答案 0 :(得分:0)

要使用async版本,您需要将方法从返回bool更改为返回Task<bool>并添加async修饰符,然后您将想要使用Async后缀方法和await它们。

我也会在DB_Conn.CloseAsync()块中执行finally,并删除空的catch,因为您已经有了某种形式的基本错误处理。

请注意,在使用C#6及更高版本时,您只能使用awaitcatch中的finally

所以你的例子看起来像:

public static async Task<bool> CheckUserLogin(string Username, string Password)
{
    try
    {
        if (string.IsNullOrEmpty(Username) || string.IsNullOrEmpty(Password)) { return false; }
        if (!System.Text.RegularExpressions.Regex.IsMatch(Username, "^[a-zA-Z0-9\x20]+$")) { return false; }
        if (!System.Text.RegularExpressions.Regex.IsMatch(Password, "^[a-zA-Z0-9\x20]+$")) { return false; }
        bool UserExit = false;
        await DataBase_Connection.DB_Conn.OpenAsync();
        try
        {
            string c_query = "SELECT COUNT(*) FROM `KoN_Account` WHERE BINARY `AccountName` = @U_Name AND `HashPass` = @P_Pass LIMIT 1;";
            using (MySqlCommand CU_cmd = new MySqlCommand(c_query, db_Conn))
            {
                CU_cmd.Parameters.AddWithValue("@U_Name", Username);
                CU_cmd.Parameters.AddWithValue("@P_Pass", Password);
                var count = await CU_cmd.ExecuteScalarAsync();
                UserExit = Convert.ToBoolean(count);
            }
        }
        finally
        {
            await DataBase_Connection.DB_Conn.CloseAsync();
        }
        return UserExit;
    }
    catch (Exception ex)
    {
        LoginConsole.WriteConsoleLoginParams(System.ConsoleColor.Red, "ERROR CheckUserLogin", ex.ToString() + "\nFailed to connect to the database (server shutdown...)");
    }
    return false;
}