Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Iterating through the resultset .Net

Tags:

.net

asp.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....
         }                            
    };                        
}
like image 304
Geek Avatar asked Jan 23 '26 13:01

Geek


1 Answers

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.

like image 103
IrishChieftain Avatar answered Jan 25 '26 11:01

IrishChieftain



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!