Geek
Geek

Reputation: 3329

Iterating through the resultset .Net

I have code like below, this stored procedure 'get2Rows' returns 2 rows, i want to get the 1st row and get the column values and then the 2nd rows and get the column values. How do i need to iterate?

using (System.Data.Common.DbCommand dbCommand = DataAccess.Instance().Database.GetStoredProcCommand("get2Rows"))
{       
    using (IDataReader reader = DataAccess.Instance().Database.ExecuteReader(dbCommand))
    {
         while (reader.Read())//here i want to get the 1st row
         {
             //here i want to get the columns of the 1st row....
         }                            
    };                        
}

Upvotes: 3

Views: 647

Answers (1)

IrishChieftain
IrishChieftain

Reputation: 15253

while (reader.Read())//here i want to get the 1st row
{
    //here i want to get the columns of the 1st row....
    int firstColumn = Convert.ToInt32(dr[0]["intColumn"].ToString());
    string secondColumn = dr[0]["stringColumn"].ToString();
    // etc.
}

You're getting both rows because you are looping through them. It's up to you how you want to store them, maybe in a collection?

More info: http://www.csharp-station.com/Tutorial/AdoDotNet/lesson04

My advice would be to work with one row at a time if you are using a DataReader because that is how it operates. If you want all the rows in memory, you should use a DataSet instead.

Upvotes: 1

Related Questions