Multiple Active Result Sets (MARS) in ADO.NET 2.0 and SQL Server 2005

 

Posted by Rickie Lee, http://rickie.cnblogs.com

Multiple Active Result Sets (MARS) is a new feature of ADO.NET 2.0 that provides the capability to open more than one result set over the same connection and lets you access them all concurrently. Prior to MARS, each result set required a separate connection. Currently, the first commercial database to support MARS is SQL Server 2005.

 

1. Enable MARS by setting MultipleActiveResultSets=True in the connection string

<connectionStrings>

      <add name="Northwind" connectionString="Server=localhost; Database=Northwind; User ID=sa; Password=developer; MultipleActiveResultSets=True" />

</connectionStrings>

 

Otherwise, you will get the following exception.

"Systerm.InvalidOperationException: There is already an open DataReader associated with this connection which must be closed first".

 

This setting only has an effect when used with SQL Server 2005 or a later version.

 

2. Follow these steps to create a demo web page.

(1) Retrieve the Order result set using a SqlDataReader object and binds it to a GridView control.

(2) Set up the OnRowDataBound property of the GridView control.

        <asp:GridView ID="gvOrders" Runat="server" AutoGenerateColumns="False"

                OnRowDataBound="gvOrders_RowDataBound" Width="100%">

When the GridView control starts to bind the DataReader, it starts firing the OnRowDataBound event for each record.

 

(3) Create an OnRowDataBound event handler.

In the method, we can get reference to each DataReader record by using the IDataRecord interface, then access the specified column and get the value. Finally, we retrieve the database again over the same SQL connection.

        IDataRecord OrderRecord;

 

        // Retrieving the currently bound record from the Data Reader

        // using the IDataRecord interface

        OrderRecord = e.Row.DataItem as IDataRecord;

 

        // Retrieving reference to the Label Control inside the current

        // GridView row. This Label will be populated with Order Details

        lblOrderDetail = e.Row.FindControl("lblOrderDetail") as Label;

 

        if ((OrderRecord == null) || (lblOrderDetail == null))

            return;

      ………………………………………        

 

 

The following full code of the ASPX page is abstracted from the reference 1. Please get more detail information in the book.

 

Multiple Active Result Sets (MARS) in ADO.NET 2.0 and SQL Server 2005<%@ Page Language="C#" %>
Multiple Active Result Sets (MARS) in ADO.NET 2.0 and SQL Server 2005
<%@ Import Namespace="System.Data" %>
Multiple Active Result Sets (MARS) in ADO.NET 2.0 and SQL Server 2005
<%@ Import Namespace="System.Data.SqlClient" %>
Multiple Active Result Sets (MARS) in ADO.NET 2.0 and SQL Server 2005
<%@ Import Namespace="System.Configuration" %>
Multiple Active Result Sets (MARS) in ADO.NET 2.0 and SQL Server 2005
Multiple Active Result Sets (MARS) in ADO.NET 2.0 and SQL Server 2005
<script runat="server">
Multiple Active Result Sets (MARS) in ADO.NET 2.0 and SQL Server 2005    
// Declaring connection here allows us to use it inside all methods 
Multiple Active Result Sets (MARS) in ADO.NET 2.0 and SQL Server 2005    
// of this class
Multiple Active Result Sets (MARS) in ADO.NET 2.0 and SQL Server 2005
    SqlConnection DBCon;
Multiple Active Result Sets (MARS) in ADO.NET 2.0 and SQL Server 2005     
Multiple Active Result Sets (MARS) in ADO.NET 2.0 and SQL Server 2005    
protected void Page_Load(object sender, EventArgs e)

 

 

References:

1. Professional ASP.NET 2.0, by Bill Evjen, Scott Hanselman, Farhan Muhammad, Srinivasa Sivakumar, Devin Rader. Wrox - Wiley Publishing Company 2005

 

 

相关文章: