C#異步綁定數據實現(xiàn)方法
更新時間:2015年09月04日 16:54:42 作者:我心依舊
這篇文章主要介紹了C#異步綁定數據實現(xiàn)方法,實例分析了C#操作數據庫及異步綁定的相關實現(xiàn)技巧,具有一定參考借鑒價值,需要的朋友可以參考下
本文實例講述了C#異步綁定數據實現(xiàn)方法。分享給大家供大家參考。具體如下:
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#程序設計有所幫助。
相關文章
基于C#?實現(xiàn)?OPC?DA?Server的問題小結
這篇文章主要介紹了基于C#?實現(xiàn)?OPC?DA?Server的相關知識,關于C#怎么編寫一個進程外的DCOM組件,這里先不做介紹了,這里主要介紹下OPC?DA?Server?的第一個接口,感興趣的朋友跟隨小編一起看看吧2024-04-04

