欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C#异步绑定数据实现方法

程序员文章站 2023-11-22 14:09:10
本文实例讲述了c#异步绑定数据实现方法。分享给大家供大家参考。具体如下: using system; using system.collections.gene...

本文实例讲述了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#程序设计有所帮助。