如何使oledb查询异步

时间:2019-07-02 17:21:16

标签: c#-5.0

此查询存在问题,运行时间长且超时。我想尝试使其异步,但是找不到与我尝试做的事情接近的好例子。

以下是我尝试过的内容,它可以启动,但是什么也不做。这是在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();
        }
    }
}

理想情况下,我想从查询中获取数据并将其加载到datagridview表中。

1 个答案:

答案 0 :(得分:1)

您应该更改一些内容:

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窗体应用程序,所以您可以使用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();
}