ASP.NET Tutorial/ADO.net Database/IAsyncResult — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Версия 15:30, 26 мая 2010
Содержание
- 1 The Poll approach of working with asynchronous commands (C#)
- 2 The Poll approach of working with asynchronous commands (VB)
- 3 The wait approach of handling a single asynchronous process (C#)
- 4 The wait approach of handling a single asynchronous process (VB)
- 5 Use of the WaitAny method of processing multiple asynchronous processes (C#)
- 6 Use of the WaitAny method of processing multiple asynchronous processes (VB)
The Poll approach of working with asynchronous commands (C#)
<%@ Page Language="C#" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<%@ Import Namespace="System.Configuration" %>
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection DBCon;
SqlCommand Command = new SqlCommand();
SqlDataReader OrdersReader;
IAsyncResult ASyncResult;
DBCon = new SqlConnection();
DBCon.ConnectionString =
ConfigurationManager.ConnectionStrings["DSN_NorthWind"].ConnectionString;
Command.rumandText =
"SELECT TOP 5 Customers.rupanyName, Customers.ContactName, " +
" Orders.OrderID, Orders.OrderDate, " +
" Orders.RequiredDate, Orders.ShippedDate " +
" FROM Orders, Customers " +
" WHERE Orders.CustomerID = Customers.CustomerID " +
" ORDER BY Customers.rupanyName, Customers.ContactName "
Command.rumandType = CommandType.Text;
Command.Connection = DBCon;
DBCon.Open();
ASyncResult = Command.BeginExecuteReader();
while (!ASyncResult.IsCompleted)
{
System.Threading.Thread.Sleep(10);
}
OrdersReader = Command.EndExecuteReader(ASyncResult);
gvOrders.DataSource = OrdersReader;
gvOrders.DataBind();
DBCon.Close();
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>The Poll Approach</title>
</head>
<body>
<form id="form1" runat="server">
<div><br />
<asp:GridView ID="gvOrders" Runat="server"
AutoGenerateColumns="False" Width="100%">
<Columns>
<asp:BoundField HeaderText="Company Name"
DataField="CompanyName"></asp:BoundField>
<asp:BoundField HeaderText="Contact Name"
DataField="ContactName"></asp:BoundField>
<asp:BoundField HeaderText="Order Date"
DataField="orderdate" DataFormatString="{0:d}"></asp:BoundField>
<asp:BoundField HeaderText="Required Date" DataField="requireddate"
DataFormatString="{0:d}"></asp:BoundField>
<asp:BoundField HeaderText="Shipped Date" DataField="shippeddate"
DataFormatString="{0:d}"></asp:BoundField>
</Columns>
</asp:GridView><br />
</div>
</form>
</body>
</html>
File: Web.config
<configuration>
<connectionStrings>
<add name="DSN_Northwind"
connectionString="Data Source=localhost\SQLEXPRESS;Initial Catalog=Northwind;Integrated Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
</configuration>
The Poll approach of working with asynchronous commands (VB)
<%@ Page Language="VB" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<%@ Import Namespace="System.Configuration" %>
<script runat="server">
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Dim DBCon As SqlConnection
Dim Command As SqlCommand = New SqlCommand()
Dim OrdersReader As SqlDataReader
Dim ASyncResult As IAsyncResult
DBCon = New SqlConnection()
DBCon.ConnectionString = ConfigurationManager.ConnectionStrings("DSN_NorthWind").ConnectionString
Command.rumandText = "SELECT TOP 5 Customers.rupanyName, Customers.ContactName, " & _
" Orders.OrderID, Orders.OrderDate, " & _
" Orders.RequiredDate, Orders.ShippedDate " & _
" FROM Orders, Customers " & _
" WHERE Orders.CustomerID = Customers.CustomerID " & _
" ORDER BY Customers.rupanyName, Customers.ContactName "
Command.rumandType = CommandType.Text
Command.Connection = DBCon
DBCon.Open()
ASyncResult = Command.BeginExecuteReader()
While Not ASyncResult.IsCompleted
System.Threading.Thread.Sleep(10)
End While
OrdersReader = Command.EndExecuteReader(ASyncResult)
gvOrders.DataSource = OrdersReader
gvOrders.DataBind()
DBCon.Close()
End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>The Poll Approach</title>
</head>
<body>
<form id="form1" runat="server">
<div><br />
<asp:GridView ID="gvOrders" Runat="server"
AutoGenerateColumns="False" Width="100%">
<Columns>
<asp:BoundField HeaderText="Company Name"
DataField="CompanyName"></asp:BoundField>
<asp:BoundField HeaderText="Contact Name"
DataField="ContactName"></asp:BoundField>
<asp:BoundField HeaderText="Order Date"
DataField="orderdate" DataFormatString="{0:d}"></asp:BoundField>
<asp:BoundField HeaderText="Required Date" DataField="requireddate"
DataFormatString="{0:d}"></asp:BoundField>
<asp:BoundField HeaderText="Shipped Date" DataField="shippeddate"
DataFormatString="{0:d}"></asp:BoundField>
</Columns>
</asp:GridView><br />
</div>
</form>
</body>
</html>
File: Web.config
<configuration>
<connectionStrings>
<add name="DSN_Northwind"
connectionString="Data Source=localhost\SQLEXPRESS;Initial Catalog=Northwind;Integrated Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
</configuration>
The wait approach of handling a single asynchronous process (C#)
<%@ Page Language="C#" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<%@ Import Namespace="System.Configuration" %>
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection DBCon;
SqlCommand Command = new SqlCommand();
SqlDataReader OrdersReader;
IAsyncResult ASyncResult;
System.Threading.WaitHandle WHandle;
DBCon = new SqlConnection();
DBCon.ConnectionString =
ConfigurationManager.ConnectionStrings["DSN_NorthWind"].ConnectionString;
Command.rumandText = "SELECT TOP 5 Customers.rupanyName, Customers.ContactName, " +
" Orders.OrderID, Orders.OrderDate, " +
" Orders.RequiredDate, Orders.ShippedDate " +
" FROM Orders, Customers " +
" WHERE Orders.CustomerID = Customers.CustomerID " +
" ORDER BY Customers.rupanyName, Customers.ContactName ";
Command.rumandType = CommandType.Text;
Command.Connection = DBCon;
DBCon.Open();
ASyncResult = Command.BeginExecuteReader();
WHandle = ASyncResult.AsyncWaitHandle;
if (WHandle.WaitOne() == true)
{
OrdersReader = Command.EndExecuteReader(ASyncResult);
gvOrders.DataSource = OrdersReader;
gvOrders.DataBind();
DBCon.Close();
}
else
{
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>The Wait Approach</title>
</head>
<body>
<form id="form1" runat="server">
<div><br />
<asp:GridView ID="gvOrders" Runat="server"
AutoGenerateColumns="False" Width="100%">
<Columns>
<asp:BoundField HeaderText="Company Name"
DataField="CompanyName"></asp:BoundField>
<asp:BoundField HeaderText="Contact Name"
DataField="ContactName"></asp:BoundField>
<asp:BoundField HeaderText="Order Date"
DataField="orderdate" DataFormatString="{0:d}"></asp:BoundField>
<asp:BoundField HeaderText="Required Date" DataField="requireddate"
DataFormatString="{0:d}"></asp:BoundField>
<asp:BoundField HeaderText="Shipped Date" DataField="shippeddate"
DataFormatString="{0:d}"></asp:BoundField>
</Columns>
</asp:GridView><br />
</div>
</form>
</body>
</html>
File: Web.config
<configuration>
<connectionStrings>
<add name="DSN_Northwind"
connectionString="Data Source=localhost\SQLEXPRESS;Initial Catalog=Northwind;Integrated Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
</configuration>
The wait approach of handling a single asynchronous process (VB)
<%@ Page Language="VB" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<%@ Import Namespace="System.Configuration" %>
<script runat="server">
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Dim DBCon As SqlConnection
Dim Command As SqlCommand = New SqlCommand()
Dim OrdersReader As SqlDataReader
Dim ASyncResult As IAsyncResult
Dim WHandle As Threading.WaitHandle
DBCon = New SqlConnection()
DBCon.ConnectionString = ConfigurationManager.ConnectionStrings("DSN_NorthWind").ConnectionString
Command.rumandText = "SELECT TOP 5 Customers.rupanyName, Customers.ContactName, " & _
" Orders.OrderID, Orders.OrderDate, " & _
" Orders.RequiredDate, Orders.ShippedDate " & _
" FROM Orders, Customers " & _
" WHERE Orders.CustomerID = Customers.CustomerID " & _
" ORDER BY Customers.rupanyName, Customers.ContactName ";
Command.rumandType = CommandType.Text
Command.Connection = DBCon
DBCon.Open()
ASyncResult = Command.BeginExecuteReader()
WHandle = ASyncResult.AsyncWaitHandle
If WHandle.WaitOne = True Then
OrdersReader = Command.EndExecuteReader(ASyncResult)
gvOrders.DataSource = OrdersReader
gvOrders.DataBind()
DBCon.Close()
Else
End If
End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>The Wait Approach</title>
</head>
<body>
<form id="form1" runat="server">
<div><br />
<asp:GridView ID="gvOrders" Runat="server"
AutoGenerateColumns="False" Width="100%">
<Columns>
<asp:BoundField HeaderText="Company Name"
DataField="CompanyName"></asp:BoundField>
<asp:BoundField HeaderText="Contact Name"
DataField="ContactName"></asp:BoundField>
<asp:BoundField HeaderText="Order Date"
DataField="orderdate" DataFormatString="{0:d}"></asp:BoundField>
<asp:BoundField HeaderText="Required Date" DataField="requireddate"
DataFormatString="{0:d}"></asp:BoundField>
<asp:BoundField HeaderText="Shipped Date" DataField="shippeddate"
DataFormatString="{0:d}"></asp:BoundField>
</Columns>
</asp:GridView><br />
</div>
</form>
</body>
</html>
File: Web.config
<configuration>
<connectionStrings>
<add name="DSN_Northwind"
connectionString="Data Source=localhost\SQLEXPRESS;Initial Catalog=Northwind;Integrated Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
</configuration>
Use of the WaitAny method of processing multiple asynchronous processes (C#)
<%@ Page Language="C#" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<%@ Import Namespace="System.Configuration" %>
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection DBCon;
SqlCommand OrdersCommand = new SqlCommand();
SqlCommand CustCommand = new SqlCommand();
SqlDataReader OrdersReader;
SqlDataReader CustReader;
IAsyncResult OrdersASyncResult;
IAsyncResult CustAsyncResult;
int WHIndex;
System.Threading.WaitHandle[] WHandles = new System.Threading.WaitHandle[1];
System.Threading.WaitHandle OrdersWHandle;
System.Threading.WaitHandle CustWHandle;
DBCon = new SqlConnection();
DBCon.ConnectionString = ConfigurationManager.ConnectionStrings["DSN_NorthWind"].ConnectionString;
CustCommand.rumandText = " SELECT * FROM Customers WHERE CompanyName = "A" ";
CustCommand.rumandType = CommandType.Text;
CustCommand.Connection = DBCon;
OrdersCommand.rumandText =
" SELECT Customers.rupanyName, Customers.ContactName, " +
" Orders.OrderID, Orders.OrderDate, " +
" Orders.RequiredDate, Orders.ShippedDate " +
" FROM Orders, Customers " +
" WHERE Orders.CustomerID = Customers.CustomerID " +
" AND Customers.rupanyName = "Alfreds Futterkiste" " +
" ORDER BY Customers.rupanyName, Customers.ContactName ";
OrdersCommand.rumandType = CommandType.Text;
OrdersCommand.Connection = DBCon;
DBCon.Open();
CustAsyncResult = CustCommand.BeginExecuteReader();
OrdersASyncResult = OrdersCommand.BeginExecuteReader();
CustWHandle = CustAsyncResult.AsyncWaitHandle;
OrdersWHandle = OrdersASyncResult.AsyncWaitHandle;
WHandles[0] = CustWHandle;
WHandles[1] = OrdersWHandle;
for (int Index = 0; Index < 2; Index++ )
{
WHIndex = System.Threading.WaitHandle.WaitAny(WHandles);
switch (WHIndex)
{
case 0:
CustReader = CustCommand.EndExecuteReader(CustAsyncResult);
gvCustomers.DataSource = CustReader;
gvCustomers.DataBind();
break;
case 1:
OrdersReader =
OrdersCommand.EndExecuteReader(OrdersASyncResult);
gvOrders.DataSource = OrdersReader;
gvOrders.DataBind();
break;
}
}
DBCon.Close();
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>The Wait Any Approach</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="gvCustomers" Width="100%" Runat="server"></asp:GridView>
<br /><br />
<asp:GridView ID="gvOrders" Width="100%" AutoGenerateColumns="False"
Runat="server">
<Columns>
<asp:BoundField HeaderText="Company Name"
DataField="CompanyName"></asp:BoundField>
<asp:BoundField HeaderText="Contact Name"
DataField="ContactName"></asp:BoundField>
<asp:BoundField HeaderText="Order Date" DataField="orderdate"
DataFormatString="{0:d}"></asp:BoundField>
<asp:BoundField HeaderText="Required Date" DataField="requireddate"
DataFormatString="{0:d}"></asp:BoundField>
<asp:BoundField HeaderText="Shipped Date" DataField="shippeddate"
DataFormatString="{0:d}"></asp:BoundField>
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>
File: Web.config
<configuration>
<connectionStrings>
<add name="DSN_Northwind"
connectionString="Data Source=localhost\SQLEXPRESS;Initial Catalog=Northwind;Integrated Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
</configuration>
Use of the WaitAny method of processing multiple asynchronous processes (VB)
<%@ Page Language="VB" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<%@ Import Namespace="System.Configuration" %>
<script runat="server">
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Dim DBCon As SqlConnection
Dim OrdersCommand As SqlCommand = New SqlCommand()
Dim CustCommand As SqlCommand = New SqlCommand()
Dim OrdersReader As SqlDataReader
Dim CustReader As SqlDataReader
Dim OrdersASyncResult As IAsyncResult
Dim CustAsyncResult As IAsyncResult
Dim WHIndex As Integer
Dim WHandles(1) As Threading.WaitHandle
Dim OrdersWHandle As Threading.WaitHandle
Dim CustWHandle As Threading.WaitHandle
DBCon = New SqlConnection()
DBCon.ConnectionString = ConfigurationManager.ConnectionStrings("DSN_NorthWind").ConnectionString
CustCommand.rumandText = " SELECT * FROM Customers WHERE CompanyName = "A" "
CustCommand.rumandType = CommandType.Text
CustCommand.Connection = DBCon
OrdersCommand.rumandText = _
" SELECT Customers.rupanyName, Customers.ContactName, " & _
" Orders.OrderID, Orders.OrderDate, " & _
" Orders.RequiredDate, Orders.ShippedDate " & _
" FROM Orders, Customers " & _
" WHERE Orders.CustomerID = Customers.CustomerID " & _
" AND Customers.rupanyName = "Alfreds Futterkiste" " & _
" ORDER BY Customers.rupanyName, Customers.ContactName "
OrdersCommand.rumandType = CommandType.Text
OrdersCommand.Connection = DBCon
DBCon.Open ()
CustAsyncResult = CustCommand.BeginExecuteReader()
OrdersASyncResult = OrdersCommand.BeginExecuteReader()
CustWHandle = CustAsyncResult.AsyncWaitHandle
OrdersWHandle = OrdersASyncResult.AsyncWaitHandle
WHandles(0) = CustWHandle
WHandles(1) = OrdersWHandle
For Index As Integer = 0 To 1
WHIndex = Threading.WaitHandle.WaitAny(WHandles)
Select Case WHIndex
Case 0
CustReader = CustCommand.EndExecuteReader(CustAsyncResult)
gvCustomers.DataSource = CustReader
gvCustomers.DataBind()
Case 1
OrdersReader = _
OrdersCommand.EndExecuteReader(OrdersASyncResult)
gvOrders.DataSource = OrdersReader
gvOrders.DataBind()
End Select
Next
DBCon.Close()
End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>The Wait Any Approach</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="gvCustomers" Width="100%" Runat="server"></asp:GridView>
<br /><br />
<asp:GridView ID="gvOrders" Width="100%" AutoGenerateColumns="False"
Runat="server">
<Columns>
<asp:BoundField HeaderText="Company Name"
DataField="CompanyName"></asp:BoundField>
<asp:BoundField HeaderText="Contact Name"
DataField="ContactName"></asp:BoundField>
<asp:BoundField HeaderText="Order Date" DataField="orderdate"
DataFormatString="{0:d}"></asp:BoundField>
<asp:BoundField HeaderText="Required Date" DataField="requireddate"
DataFormatString="{0:d}"></asp:BoundField>
<asp:BoundField HeaderText="Shipped Date" DataField="shippeddate"
DataFormatString="{0:d}"></asp:BoundField>
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>
File: Web.config
<configuration>
<connectionStrings>
<add name="DSN_Northwind"
connectionString="Data Source=localhost\SQLEXPRESS;Initial Catalog=Northwind;Integrated Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
</configuration>