C#异步绑定数据实现方法
来源:本站原创|时间:2022-11-25|栏目:C#教程|
本文实例讲述了C#异步绑定数据实现方法。分享给大家供大家参考。具体如下:
using System; using System.Collections.Generic; using System.Text; using System.Data.SqlClient; using System.Data; using System.Windows.Forms; namespace WindowsApplication2 { public class AsyncCallBackOpeartion { private static DataGridView dataGridView; public static void AsyncCallBack(string connectionString, string sql, DataGridView dgv) { dataGridView = dgv; connectionString += ";Asynchronous Processing=true"; SqlConnection conn = new SqlConnection(connectionString); SqlCommand command = new SqlCommand(sql, conn); conn.Open(); command.BeginExecuteReader(new AsyncCallback(AsyncCallBack), command); } static void AsyncCallBack(IAsyncResult ar) { if (ar.IsCompleted) { SqlCommand com = (SqlCommand)ar.AsyncState; SqlDataReader dr = com.EndExecuteReader(ar); DataTable dt = new DataTable(); dt.Load(dr); dr.Close(); if (dataGridView.InvokeRequired) { updateDG ur = new updateDG(dataBin); dataGridView.Invoke(ur, dt); } } } delegate void updateDG(DataTable dt); public static void dataBin(DataTable dt) { dataGridView.DataSource = dt; } } }
希望本文所述对大家的C#程序设计有所帮助。
您可能感兴趣的文章
- 01-10Extjs4如何处理后台json数据中日期和时间
- 01-10C#实现绑定Combobox的方法
- 01-10C#动态创建Access数据库及密码的方法
- 01-10C#使用ADO.Net部件来访问Access数据库的方法
- 01-10C#实现ComboBox控件显示出多个数据源属性的方法
- 01-10C#将图片存放到SQL SERVER数据库中的方法
- 01-10同步调用和异步调用WebService
- 01-10C#编程自学之数据类型和变量二
- 01-10C#编程自学之数据类型和变量三
- 01-10C#操作数据库中存取图片文件的方法