Retrieving multiple results (multiple SELECT results) in a single batch

by Jagadish Pulakhandam on 8/5/2011 1:10:43 PM
Rated 0 from 0 votes
Brief: Demonstrates the feature of "Multiple Active Result Sets" (MARS) available from .NET Framework 2.0 and SQL Server 2005
Posted to: Connected way of Querying Data using ADO.NET
Add to DiggAdd to del.icio.usAdd to FURLAdd to RedditAdd to YahooAdd to BlinklistAdd to GoogleAdd to ma.gnoliaAdd to ShadowsAdd to Technorati

This is simple example which demonstrates the feature of "MARS" (Multiple Active Result Sets).  This feature allows us to retrieve result sets from multiple SELECT statements issued as a single batch.

Screen shot:



Source Code:

01.using System;
02.using System.Collections.Generic;
03.using System.ComponentModel;
04.using System.Data;
05.using System.Drawing;
06.using System.Linq;
07.using System.Text;
08.using System.Windows.Forms;
09.using System.Data.SqlClient;
10. 
11.namespace CSMARS
12.{
13.    public partial class Form1 : Form
14.    {
15.        string CONNSTR = "Data Source=.\\sql2k8; Initial Catalog=Northwind; User Id=sa; Password=sql2008";
16. 
17.        public Form1()
18.        {
19.            InitializeComponent();
20.        }
21. 
22.        private void btnShow_Click(object sender, EventArgs e)
23.        {
24.            this.TextBox1.Text = string.Empty;
25.            using (SqlCommand cmd = new SqlCommand("SELECT CategoryID, CategoryName FROM Categories; SELECT EmployeeID, LastName FROM Employees; SELECT OrderID, CustomerID FROM Orders", new SqlConnection(CONNSTR)))
26.            {
27.                StringBuilder sb = new StringBuilder();
28.                cmd.Connection.Open();
29.                SqlDataReader rdr = cmd.ExecuteReader();
30.                if (rdr.HasRows)
31.                {
32.                    do
33.                    {
34.                        while (rdr.Read())
35.                        {
36.                            sb.AppendFormat("{0} - {1}", rdr[0], rdr[1]);
37.                            sb.AppendLine();
38.                        }
39.                        sb.AppendLine("---------------------------");
40.                    }
41.                    while (rdr.NextResult());
42.                    this.TextBox1.Text = sb.ToString();
43.                }
44.            }
45.        }
46.    }
47.}

Join the .NET Code Central Community and join the discussion!
Signing-up is FREE and quick. Do it now, we want to hear your opinion
0

Rated 0 from 0 votes ( login  to rate)
DotnetKicks DotnetKicksDe DotNetShoutout

Attachments / Source Code
You need to Login or Join for FREE to download the following