当前位置:编程学习 > C#/ASP.NET >>

C# 异步读取数据库里面的数据与绑定UI的解决办法

异步读取数据库,在数据绑定的时候会出现点问题,就是窗体界面会无法关闭,要结束任务才能结束进程。例如下面代码

首先按习惯的方法,设定线程更新UI

a2.CheckForIllegalCrossThreadCalls = false;   //a2为窗体名称

下面的代码就是从数据库里取得数据并绑定

private void button1_Click(object sender, EventArgs e)
        {
            SqlConnection con;
            SqlCommand com;
            try
            {
                con = new SqlConnection("UID=sa;Password=123;Initial Catalog=AD;Data Source=192.168.1.1;Asynchronous Processing=true");
                con.Open();
                com = new SqlCommand("select top 100 * from tb_user", con);
                com.BeginExecuteReader(new AsyncCallback(delDataBin), com);
            }
            catch (Exception ex)
            {
                MessageBox.Show("程序发生错误,信息: " + ex.Message);
            }

        }

        private void delDataBin(IAsyncResult ar)
        {
            if (ar.IsCompleted)
            {
                SqlCommand com = (SqlCommand)ar.AsyncState;
                SqlDataReader dr = com.EndExecuteReader(ar);
                DataTable dt = new DataTable();
                dt.Load(dr);
                dr.Close();

                this.dataGridView1.DataSource = dt;   //绑定数据            

            }
        }

到这里完成的绑定的工作,运行查看一下效果,其实这样是会出现窗体假死的现象。

下面通过Invoke 来实现

首先声明委托  public delegate void updateDG(DataTable dt);

然后通过dataBin来绑定DataGridView

        public void dataBin(DataTable dt)
        {
            dataGridView1.DataSource = dt;
            return;
        } 

在线程里面调用下面方法

//绑定数据
                if (this.InvokeRequired)
                {
                    updateDG ur = new updateDG(dataBin);
                    this.Invoke(ur, dt);
                }

完整的代码如下:

 

        private void button1_Click(object sender, EventArgs e)
        {
            SqlConnection con;
            SqlCommand com;
            try
            {
                con = new SqlConnection("UID=sa;Password=123;Initial Catalog=AD;Data Source=192.168.1.1;Asynchronous Processing=true");
                con.Open();
                com = new SqlCommand("select top 100 * from tb_user", con);
                com.BeginExecuteReader(new AsyncCallback(delDataBin), com);
            }
            catch (Exception ex)
            {
                MessageBox.Show("程序发生错误,信息: " + ex.Message);
            }

        }

        private void delDataBin(IAsyncResult ar)
        {
            if (ar.IsCompleted)
            {
                SqlCommand com = (SqlCommand)ar.AsyncState;
                SqlDataReader dr = com.EndExecuteReader(ar);
                DataTable dt = new DataTable();
                dt.Load(dr);
                dr.Close();

                //this.dataGridView1.DataSource = dt;//绑定数据

                if (this.InvokeRequired)
                {
                    updateDG ur = new updateDG(dataBin);
                    this.Invoke(ur, dt);
                }

补充:软件开发 , C# ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,