异步查询大结果集

 

partial class Form1 : Form
    {
        private ArrayList dbRecordsHolder;
        public Form1()
        {
            InitializeComponent();
            dbRecordsHolder = new ArrayList();
        }

        private void btnPopulate_Click(object sender, EventArgs e)
        {
            string connectionString = "Server=(local);Database=Test;Integrated Security=SSPI;Asynchronous Processing=true";
            SqlConnection testConnection = new SqlConnection(connectionString);
           
            SqlCommand testCommand =
                new SqlCommand("Select * from Demo", testConnection);
            testConnection.Open();

            AsyncCallback callback = new AsyncCallback(DataReaderIsReady);
            IAsyncResult asyncresult = testCommand.BeginExecuteReader(callback, testCommand);
        }

        private void DataReaderIsReady(IAsyncResult result)
        {
            MessageBox.Show("Results Load Complete","I'm Done");
            SqlCommand testCommand = (SqlCommand)result.AsyncState;

            SqlDataReader sqlDr = testCommand.EndExecuteReader(result);
            if (sqlDr.HasRows)
            {
                foreach (DbDataRecord rec in sqlDr)
                {
                    dbRecordsHolder.Add(rec);
                }
            }
            sqlDr.Close();
            testCommand.Connection.Dispose();
        }

        private void btnDataBind_Click(object sender, EventArgs e)
        {
            myDataGrid.DataSource = dbRecordsHolder;
        }
    }

 

你可能感兴趣的:(查询)