Sunday, October 19, 2008
We can mention two select statements in the command text for command object of SqlDataReader. Then execute the reader.
SqlCommand sqlComm = new SqlCommand("Select * from Table1; Select * from Table2");
SqlDataReader sqlDR = new SqlDataReader();
sqlDR = sqlComm.ExecuteReader(CommandBehavior.CloseConnection);
To read data from two table write code as below where we will read two result sets using while loops.
// Your Code Here
Note: Dont forget to close DataReader after using in finally block. Especially when we are passing reader to other layer, So as to close connection attached to Reader and avoid in connection leaks(As we are using CommandBehavior.CloseConnection connection attached to Reader will be closed when reader is closed). Or better to use using statement.
Also Read other Top Articles
- JSON Serialization in VS 2008
- Implementing Forms Authentication in Silverlight Application.
- Making GridView Rows or Individual Cells Clickable and Selectable.
- Enabling browser back button for GridView Paging and Sorting in Ajax 1.1 and 3.5 (using Visual Studio 2005/ Visual studio 2008)
- How to pass values from User Control to Page or calling Page methods from User Control.
- What is WCF?
- New features in C# 4.0
- C# to VB.NET and VB.NET to C# online free converter tools.