<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs"
Inherits="Default" %>
Non-blocking Commands
File: Default.aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
using System.Web;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Default : System.Web.UI.Page
{
protected void Button1_Click(object sender, EventArgs e)
{
string connString = "Async=true;SERVER=(local);DATABASE=northwind;Trusted_Connection=yes;";
using (SqlConnection conn = new SqlConnection(connString))
{
SqlCommand cmd = new SqlCommand("SELECT lastname,firstname FROM employees", conn);
conn.Open();
IAsyncResult iar = cmd.BeginExecuteReader();
SqlDataReader reader = (SqlDataReader) cmd.EndExecuteReader(iar);
ProcessData(reader);
}
}
protected void ProcessData(SqlDataReader reader)
{
StringBuilder sb = new StringBuilder("");
while (reader.Read())
sb.AppendFormat("{0}, {1}
", reader[0], reader[1]);
reader.Close();
Results.Text = sb.ToString();
}
}