C#MySQL executenonqueryasync不是异步的 [英] C# Mysql executenonqueryasync is not asynchronous

查看:654
本文介绍了C#MySQL executenonqueryasync不是异步的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获取正在教授这种特定类型的老师的名单:

public static async Task<DataTable> getTeacherSHS() 
{
    DataTable dt = new DataTable();
    string query = @"some long query dont mid this";
    using (MySqlConnection conn = new MySqlConnection(cs))
    {
        try
        {
            await conn.OpenAsync();
            using( MySqlCommand cmd = new MySqlCommand(query,conn))
            {
                cmd.Parameters.AddWithValue("@shs", "%SHS%");
                cmd.Parameters.AddWithValue("@term", term);

                await cmd.ExecuteNonQueryAsync();
                using (MySqlDataAdapter da = new MySqlDataAdapter(cmd)) 
                {
                    await da.FillAsync(dt);
                    MySqlCommandBuilder cb = new MySqlCommandBuilder(da);
                }
            }
        }
        catch (MySqlException e)
        {
            Console.Write(e.Message);
        }
        if (conn != null)
            await conn.CloseAsync();
    }

    return dt;
}

现在,我的获取方法是通过单击按钮,它将为我的datagridview返回一个数据表

private  async void button1_Click_1(object sender, EventArgs e)
{
    dataGridView1.DataSource = await ConnectionAsync.getTeacherSHS(); 
    dataGridView1.Columns[0].AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells;
    dataGridView1.Columns[1].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
}

我的问题是,单击按钮后,该呼叫阻止了另一个I/O,我似乎无法在另一个文本框中键入内容,因为它正在等待任务完成.我以为是异步的,有人可以解释吗?

解决方案

之所以会发生这种情况,是因为MySql.Data连接器中的Async方法实际上并不是异步的.它们在网络I/O上阻塞,仅在DB操作完成后才返回. (有关详细说明,请参见此问题及其最佳答案.) MySQL错误#70111 报告了MySQL连接器中的此问题.

要获得真正的异步DB操作,您必须等到该错误修复后,或切换到其他连接器.

我一直在开发新的完全异步连接器(在NuGet上 MySqlConnector ; GitHub上的源).由于版本0.33.0 ,它支持MySqlDataAdapter. /p>

I want to get the list of the teachers who are teaching this specific type:

public static async Task<DataTable> getTeacherSHS() 
{
    DataTable dt = new DataTable();
    string query = @"some long query dont mid this";
    using (MySqlConnection conn = new MySqlConnection(cs))
    {
        try
        {
            await conn.OpenAsync();
            using( MySqlCommand cmd = new MySqlCommand(query,conn))
            {
                cmd.Parameters.AddWithValue("@shs", "%SHS%");
                cmd.Parameters.AddWithValue("@term", term);

                await cmd.ExecuteNonQueryAsync();
                using (MySqlDataAdapter da = new MySqlDataAdapter(cmd)) 
                {
                    await da.FillAsync(dt);
                    MySqlCommandBuilder cb = new MySqlCommandBuilder(da);
                }
            }
        }
        catch (MySqlException e)
        {
            Console.Write(e.Message);
        }
        if (conn != null)
            await conn.CloseAsync();
    }

    return dt;
}

Now my method for getting this is via a button click and it will return a datatable for my datagridview

private  async void button1_Click_1(object sender, EventArgs e)
{
    dataGridView1.DataSource = await ConnectionAsync.getTeacherSHS(); 
    dataGridView1.Columns[0].AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells;
    dataGridView1.Columns[1].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
}

My problem is that after clicking the button the call is blocking the other I/O i cant seem to type in the other textbox because it is waiting for the task to be done. I thought it was asynchronous, can somebody explain?

解决方案

This happens because the Async methods in the MySql.Data connector aren't actually asynchronous. They block on network I/O and only return when the DB operation is complete. (For a much more detailed description, see this question and its top answer.) MySQL bug #70111 reports this problem in the MySQL connector.

To get truly asynchronous DB operations, you'll have to wait until that bug is fixed, or switch to a different connector.

I've been developing a new, fully async connector (MySqlConnector on NuGet; source on GitHub). It supports MySqlDataAdapter since version 0.33.0.

这篇关于C#MySQL executenonqueryasync不是异步的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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