使用SqlDataReader处理多个结果集

// 使用SqlDataReader处理多个结果集
// MultipleResults.cs
using System;
using System.Data;
using System.Data.SqlClient;
namespace Ch12
{
    class MultipleResults
    {
        static void Main( string[] args)
        {
            string strConn = "server=.\\MSSQL2012;integrated security=true;database=Northwind;";
            string sqla = "select firstname, lastname from employees;" ;
            string sqlb = "select RegionID, RegionDescription  from Region;";
            string sql = sqla + sqlb;
            SqlConnection conn = new SqlConnection(strConn);
            try
            {
                conn.Open();
                SqlCommand cmd = new SqlCommand(sql, conn);
                SqlDataReader rdr = cmd.ExecuteReader();
                // 循环处理结果集
                do
                {
                    while (rdr.Read())
                    {
                        Console.WriteLine( "{0} : {1}", rdr[0], rdr[1]);
                    }
                    Console.WriteLine( "".PadLeft(20,'-' ));
                }
                while (rdr.NextResult());
                rdr.Close();
            }
            catch ( Exception ex)
            {
                Console.WriteLine( "发生错误:" + ex);
            }
            finally
            {
                conn.Close();
                Console.ReadLine();
            }
        }
    }
}
--------------------
Nancy : Davolio
Andrew : Fuller
Janet : Leverling
Margaret : Peacock
Steven : Buchanan
Michael : Suyama
Robert : King
Laura : Callahan
Anne : Dodsworth
--------------------
1 : Eastern
2 : Western
3 : Northern
4 : Southern
--------------------


来自为知笔记(Wiz)


posted on 2013-08-22 01:07  伊利丹  阅读(529)  评论(0编辑  收藏  举报