2

The following simple code retrieve data with SqlDataReader:

    SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=myDB;Integrated Security=True");
    conn.Open();
    SqlCommand cmd = conn.CreateCommand();
    cmd.CommandText = "SELECT [t0].[ID], [t0].[Name] FROM [Persons] AS [t0]";
    SqlDataReader rdr = cmd.ExecuteReader();
    while (rdr.Read())
    {
        Console.WriteLine(rdr["id"] + " " + rdr["name"]);
    }

While the code is running, I use Sql Profiler to monitor the DB. And I saw only one select command.

SELECT [t0].[ID], [t0].[Name] FROM [Persons] AS [t0]

So it seems the SqlDataReader will retrieve ALL the data from server first, and then enumerate them.

Is that true? What if the data is too many?

1
  • 1
    I tested where I started a reader and changed the next row but the reader did not get the revised value. So it seems to read ahead. Commented Sep 27, 2012 at 16:08

1 Answer 1

2

SqlDataReader has a 8 kilobytes buffer.

Duplicate question: How DataReader works?

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.