Multiple Active Result Sets (MARS)

One of the fastest methods to retrieve data from the database is the DbDataReader object, but one of the problems with DbDataReader object is that it keeps an open server-side cursor while you are looping through the results of your query. If you try to execute another command while the first command is still executing, you will receive an InvalidOperationException, stating, "There is already an open DataReader associated with this Connection which must be closed first." You can avoid this exception by setting the MultipleActiveResultSets connection string option to true when connecting to Multiple Active Result Sets (MARS)–enabled hosts such as SQL Server 2005 and later.
 
The following example shows MARS enabled connection string named ConStrMARS
 
Application Configuration File
  1. <connectionStrings>  
  2.     <clear />  
  3.     <add name="ConStr" providerName="System.Data.SqlClient" connectionString="Data Source=localhost;Integrated security=SSPI;Initial Catalog=MyDatabase;" />  
  4.     <!--By default, MARS is disabled when connecting to a MARS-enabled host.  
  5.    
  6.       It must be enabled in the connection string. -->  
  7.     <add name="ConStrMARS" providerName="System.Data.SqlClient" connectionString="Data Source=localhost;Integrated security=SSPI;Initial Catalog=MyDatabase;MultipleActiveResultSets=True" />  
  8. </connectionStrings> 
The following example shows how to use two SqlDataReader objects with two SqlCommand objects and a single SqlConnection object with MARS enabled. It opens a single connection to the MyDatabase Database. Using a SqlCommand object, a SqlDataReader is created. As the reader is used, a second SqlDataReader is opened, using data from the first SqlDataReader as input to the WHERE clause for the second reader.
 
Example:
  1. using System;  
  2. using System.Data;  
  3. using System.Data.SqlClient;  
  4.   
  5. namespace MultipleActiveResultSets {  
  6.     class Program {  
  7.         static void Main(string[] args) {  
  8.             int CustomerID;  
  9.             SqlDataReader OrderReader = null;  
  10.             string connectionString = "Data Source=(local);Integrated Security=SSPI;" +  
  11.                 "Initial Catalog=MyDatabase;MultipleActiveResultSets=True";  
  12.   
  13.             string CustomerSQL = "SELECT CustomerID, CustomerName FROM Customers";  
  14.   
  15.             string OrderSQL = "SELECT * FROM Orders WHERE CustomerID = @CustomerID";  
  16.             using(SqlConnection connection = new SqlConnection(connectionString)) {  
  17.                 SqlCommand CustCmd = new SqlCommand(CustomerSQL, connection);  
  18.                 SqlCommand OrderCmd = new SqlCommand(OrderSQL, connection);  
  19.                 OrderCmd.Parameters.Add("@CustomerID", SqlDbType.Int);  
  20.   
  21.                 connection.Open();  
  22.                 using(SqlDataReader CustomerReader = CustCmd.ExecuteReader()) {  
  23.                     while (CustomerReader.Read()) {  
  24.                         Console.WriteLine(CustomerReader["CustomerName"]);  
  25.   
  26.                         CustomerID = (int) CustomerReader["CustomerID"];  
  27.                         OrderCmd.Parameters["@CustomerID"].Value = CustomerID;  
  28.   
  29.                         OrderReader = OrderCmd.ExecuteReader();  
  30.                         using(OrderReader) {  
  31.                             while (OrderReader.Read()) {  
  32.                                 //Console.WriteLine(  
  33.                             }  
  34.                         }  
  35.                     }  
  36.                 }  
  37.                 Console.WriteLine("Press any key to continue");  
  38.                 Console.ReadLine();  
  39.             }  
  40.         }  
  41.     }  
On a database server without MARS, you could first collect the list of customers into a collection and close the connection. After that, you can loop through the collection to get each customer ID and execute a query to get the list of Orders made by that customer. Another solution is simply to create two connections: one for the customer list and another one for orders.


Similar Articles