{love to code?}

July 11, 2009

ADO.NET best practices – Reading data from data reader

Filed under: C# — navaneethkn @ 5:45 PM
Tags: , , ,

I have seen many people using DataReader incorrectly. In this post, I will try to explain some good practices that can be followed when reading from a data reader. Consider the following problematic code,

SqlDataReader reader = /* ... */;
while (reader.Read())
{
    string userName = reader["user_name"].ToString();
    int age = int.Parse( reader["age"].ToString() );
    /* ... */
}
reader.Close();

How many problems can you figure out from the above code? There are many problems with this code, (more…)

Blog at WordPress.com.