■ SqlDataReader 클래스 : 결과 SQL문이 2개 이상인 경우 처리하기

----------------------------------------------------------------------------------------------------

using System;

using System.Data.SqlClient;

 

SqlConnection sqlConnection = new SqlConnection("Server=127.0.0.1;Database=Northwind;UID=sa;PWD=*****");

 

sqlConnection.Open();

 

string sql = @"

SELECT COUNT(*) FROM Customers;

 

SELECT CustomerID, CompanyName FROM Customers;

";

 

SqlCommand sqlCommand = new SqlCommand(sql, sqlConnection);

 

SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();

 

Console.WriteLine("--------------------------------------------------");

Console.WriteLine("첫번째 결과");

Console.WriteLine("--------------------------------------------------");

 

while(sqlDataReader.Read())

{

    for(int i = 0; i < sqlDataReader.FieldCount; i++)

    {

        Console.Write("{0} ", sqlDataReader[i]);

    }

 

    Console.WriteLine();

}

 

Console.WriteLine("--------------------------------------------------");

Console.WriteLine("두번째 결과");

Console.WriteLine("--------------------------------------------------");

 

if(sqlDataReader.NextResult())

{

    while(sqlDataReader.Read())

    {

        for(int i = 0; i < sqlDataReader.FieldCount; i++)

        {

            Console.Write("{0} ", sqlDataReader[i]);

        }

 

        Console.WriteLine();

    }

}

 

sqlDataReader.Close();

 

sqlConnection.Close();

----------------------------------------------------------------------------------------------------

Posted by 사용자 icodebroker