C#異步綁定命據完成辦法。本站提示廣大學習愛好者:(C#異步綁定命據完成辦法)文章只能為提供參考,不一定能成為您想要的結果。以下是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#法式設計有所贊助。