如何使oledb查询异步

How to make oledb query asychronous

我在查询时遇到问题,运行 需要很长时间,而且正在超时。我想尝试使其异步,但我找不到一个接近我正在尝试做的事情的好例子。

下面是我试过的方法,它启动了但什么也没做。这是在 WinForm 项目中。

namespace access_db_csharp
{
    public partial class Form1 : Form
    {
        string SQL = "";
        public Form1()
        {
            InitializeComponent();
            textBox1.Text = "Select Project, Request, Release, TestName, AssignmentNumber, Formulation, Container, Closure FROM tblWtLossBottDropResults";
            textBox2.Text = "Where Container LIKE '%2700-305%'";
            dataGridView1.Focus();
        }
        OleDbConnection con=new OleDbConnection ();
        OleDbCommand cmd=new OleDbCommand ();
        string connectionstring = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=K:\R&D Dept\Development Lab\DLab Databases and Add Ins\DLab Results Database\DLab Results Individual Tables.accdb;Jet OLEDB:Database Password='roscoe'";

        private async void button1_Click(object sender, EventArgs e)
        {
            Task<DataTable> task = new Task<DataTable>(RunQuery);
            task.Start();

        }

        private DataTable RunQuery()
        {
            if (checkBox1.Checked == true)
            {
                SQL = textBox1.Text + " " + textBox2.Text;
            }
            else
            {
                SQL = textBox1.Text;
            }
            OleDbConnection con = new OleDbConnection(connectionstring);
            OleDbCommand cmd = new OleDbCommand(SQL, con);
            con.Open();
            cmd.CommandType = CommandType.Text;
            OleDbDataAdapter da = new OleDbDataAdapter(cmd);
            DataTable tblWtLossBottDropResults = new DataTable();
            da.Fill(tblWtLossBottDropResults);
            dataGridView1.DataSource = tblWtLossBottDropResults;

            //dataGridView1.ColumnCount = 17;
            dataGridView1.Columns[0].Name = "Project";
            dataGridView1.Columns[1].Name = "Request";
            dataGridView1.Columns[2].Name = "Release";
            dataGridView1.Columns[3].Name = "TestName";
            dataGridView1.Columns[4].Name = "AssignmentNumber";
            dataGridView1.Columns[5].Name = "Formulation";
            dataGridView1.Columns[6].Name = "Container";
            dataGridView1.Columns[7].Name = "Closure";

            MessageBox.Show("Done");
            int numRows = dataGridView1.RowCount;
            label4.Text = "Rows Returned: " + numRows.ToString();

            return tblWtLossBottDropResults;
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            con.ConnectionString = connectionstring;
            con.Open();
        }
    }
}

理想情况下,我想从查询中获取数据并将其加载到数据网格视图中table。

您应该更改一些内容:

1. You should not mix UI and non-UI code.
2. You should keep your connection open for the shortest amount of time.

因为 OleDb 提供程序没有异步 API,您的数据库代码将是同步的:

private DataTable RunQuery(string commandText)
{
    using (var connection = new OleDbConnection(connectionstring))
    {
        using (var command = connection.CreateCommand())
        {
            command.CommandType = CommandType.Text;
            command.CommandText = commandText;

            using (var dataAdapter = new OleDbDataAdapter(command))
            {
                connection.Open();

                var dataTable = new DataTable();
                dataAdapter.Fill(dataTable);

                return dataTable;
            }
        }
    }
}

因为它是一个 Windows Forms 应用程序,您可以使用 Task.Run 将工作卸载到线程池并释放 UI 线程:

private async void button1_Click(object sender, EventArgs e)
{
    var commandText = checkBox1.Checked
        ? textBox1.Text + " " + textBox2.Text
        : textBox1.Text;

    var dataTable = await Task.Run(() => RunQuery(commandText));

    /* This should be set on the designer or on the constructor
    //dataGridView1.ColumnCount = 17;
    dataGridView1.Columns[0].Name = "Project";
    dataGridView1.Columns[1].Name = "Request";
    dataGridView1.Columns[2].Name = "Release";
    dataGridView1.Columns[3].Name = "TestName";
    dataGridView1.Columns[4].Name = "AssignmentNumber";
    dataGridView1.Columns[5].Name = "Formulation";
    dataGridView1.Columns[6].Name = "Container";
    dataGridView1.Columns[7].Name = "Closure";
    */

    dataGridView1.DataSource = dataTable;
    MessageBox.Show("Done");
    int numRows = dataGridView1.RowCount;
    label4.Text = "Rows Returned: " + numRows.ToString();
}