20 pts.
 DataReader
Can anybody explain with some code snippet how to run multiple queries with a single datareader in C# .Net 2.0

Software/Hardware used:
ASKED: May 30, 2008  7:17 AM
UPDATED: July 12, 2008  2:49 AM

Answer Wiki:
Hi, The key here is to call NextResult on the SqlDataReader, rdr in the example below: <pre> private static void GetDataReaderMultipleResults() { string connStr = "Data Source=.\sqlexpress;AttachDbFilename="C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Data\AdventureWorksLT_Data.mdf";Initial Catalog=AdventureWorksLT_Data;Integrated Security=True"; string selectStatements = "select CustomerID, FirstName, LastName from SalesLT.Customer;select AddressID, AddressLine1, City, StateProvince from SalesLT.Address;"; using (SqlConnection conn = new SqlConnection(connStr)) using (SqlCommand cmd = new SqlCommand(selectStatements, conn)) { conn.Open(); Console.WriteLine("nCustomers:n"); using (SqlDataReader rdr = cmd.ExecuteReader()) { while (rdr.Read()) { Console.WriteLine( "ID: {0}, Name: {1} {2}", rdr["CustomerID">, rdr["FirstName">, rdr["LastName">); } Console.WriteLine("nAddresses:n"); rdr.NextResult(); while (rdr.Read()) { Console.WriteLine( "ID: {0}, Address: {1}, {2}, {3}", rdr["AddressID">, rdr["AddressLine1">, rdr["City">, rdr["StateProvince">); } } } }</pre> Joe
Last Wiki Answer Submitted:  July 12, 2008  2:49 am  by  JoeMayo   15 pts.
All Answer Wiki Contributors:  JoeMayo   15 pts.
To see all answers submitted to the Answer Wiki: View Answer History.


Discuss This Question:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _