tags:

views:

501

answers:

2

If I do something like this:

using (SqlCommand cmd = new SqlCommand("SELECT * FROM TBL"))
{
    using (SqlDataReader reader = cmd.ExecuteReader())
    {
        while (reader.Read())
        {
            string s = reader.GetString(7);
        }
    }
}

does the Read() call read the entire row into memory, or does the GetString(7) call mean that the reader only ever reads the data in column 7?

+4  A: 

it reads the whole row on the read operation.

lomaxx
Is there anything in ADO.NET that doesn't read the whole row, or does everything have to be limited by the columns included in the SELECT statement?
MusiGenesis
@MusiGenesis: the latter; that's kind of the point of specifying the columns in the select statement!
Steven A. Lowe
I agree with Steven, you should only specify the columns that you want
lomaxx
What I really want to do is reference columns by ordinal in the select statement, something like "SELECT 0, 1, 7 FROM TBL" to only select the first, second and eighth columns. This is not possible, it seems.
MusiGenesis
You can't do that in the sql, but you can in the reader... once you've read the whole row, of course.
Joel Coehoorn
@Joel: reading the whole row is what I'm trying to avoid. The table has 1000 columns.
MusiGenesis
A: 

I think you are looking for the ExecuteScalar method of the datareader and you want to specify your columns in your sql command.

MatthewMartin

related questions