MySql Connection方法是否支持异步编程? [英] Does MySql Connection methods support async programming?

查看:105
本文介绍了MySql Connection方法是否支持异步编程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用一种简单的方法来连接到MySql数据库,但是连接到该数据库需要一段时间&这会使应用程序处于无响应"模式.现在,我可以使用异步解决此问题吗?

I'm using a simple method for connecting to a MySql database but connecting to this database takes a while & this causes the app to be in "not responding" mode. now, can I use async for solving this?

脚本为:

Private void button_clicked()
{
MysqlConnection connection = new MysqlConnection(constring);
connection.open();
}

推荐答案

最后,我根据@MikaalAnwar的答案找到了确切的答案!

At the end, I found the exact answer according to @MikaalAnwar's answer!

我们不需要在连接字符串中添加任何新选项(例如 Asynchronous Processing = true );这是用于SQL连接&不适用于MySql.

We don't need to add any new options (like Asynchronous Processing=true) to connection string; That's for SQL connections & doesn't work for MySql.

那么,我们现在该怎么办?

So, what should we do now?

我们将任何被视为具有异步选项 async 的空缺.然后,我们添加一个等待任务&运行它( Task.Run ).在该任务中,我们将通过连接来做我们想做的一切.

we make any void that is respected to have async option, async. Then we add an await task & run it (Task.Run). inside that task, we do what ever we want with our connection.

例如:(我们不想使用任何数据集)

for example: (We don't want to use any datasets)

private async void DBConnect(String connectionString)
{
    await Task.Run(() =>
        { 
            MySqlConnection dbConnection = new MySqlConnection(connectionString);
            dbConnection.Open();
        }
    );

}

&我们不使用 DBConnection.OpenAsync(),因为void是async&我们已经使用了等待任务.

& We don't use DBConnection.OpenAsync()because the void is async & we've used await for the task.

完成:)

这篇关于MySql Connection方法是否支持异步编程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆