Tuesday, September 26, 2006

Everyone that has been using ADO.NET to get the data from the database does more or less the same thing. First you create a Connection object, then a Command, then you execute the command to get the DataReader. With reader, you iterate through the collection of records using a while loop. A sample code looks as follows:

string connectionString = "connectionString";
using (SqlConnection connection = new SqlConnection(connectionString))
{
   string commandText = "commandText";
   using (SqlCommand command = new SqlCommand(commandText, connection))
   {
      using (SqlDataReader reader = command.ExecuteReader())
      {
         while (reader.Read())
         {
            //read the data
         }
      }
   }
}

I have noticed that most people use the using statement only for the connection objects because that is what you will find in most of the books and tutorials. There is however one very important thing to remember and that is a simple rule that you should dispose all disposable object as soon as possible. When you look at the ADO.NET objects you will see the connection object is not the only one that can (and should) be disposed. The command object and also the datareader object are also IDisposable so as far as the rule goes you should dispose them. As for the command object I have seen few examples where it is disposed, but I have seen only one where the datareader is disposed.

So just to remind you: dispose your disposable objects!

kick it on DotNetKicks.com

Wednesday, September 27, 2006 11:35:44 AM (Central European Standard Time, UTC+01:00)
If you are not concerned about the (SQL) output parameters, return values or number of records affected, you can improve performance by calling the Close method of the SqlDataReader before the SqlCommand is disposed.

From the MSDN docs for SqlDataReader.Close (http://msdn2.microsoft.com/en-us/library/system.data.sqlclient.sqldatareader.close.aspx):

The [SqlDataReader] Close method fills in the values for output parameters, return values and RecordsAffected, increasing the time that it takes to close a SqlDataReader that was used to process a large or complex query. When the return values and the number of records affected by a query are not significant, the time that it takes to close the SqlDataReader can be reduced by calling the Cancel method of the associated SqlCommand object before calling the Close method.
Sunday, October 08, 2006 9:01:28 AM (Central European Standard Time, UTC+01:00)
my 2 cents, a net way of nesting multiple using - C# 2.0 only
string connectionString = "connectionString";
string commandText = "commandText";

using (SqlConnection connection = new SqlConnection(connectionString))
using (SqlCommand command = new SqlCommand(commandText, connection))
using (SqlDataReader reader = command.ExecuteReader())
{
while (reader.Read())
{
//read the data
}
}
http://staff.bespoke.com.my
Monday, October 09, 2006 8:20:39 AM (Central European Standard Time, UTC+01:00)
Erymuzuan - you have forgotten to open the connection, but so have I ;-)
Monday, October 19, 2009 2:50:27 AM (Central European Standard Time, UTC+01:00)
Hello. When we are unhurried and wise, we perceive that only great and worthy things have any permanent and absolute existence, that petty fears and petty pleasures are but the shadow of the reality.
I am from Taiwan and also now am reading in English, give please true I wrote the following sentence: "These agree: make spirit, armed audit, australian support or right, and settlement company system.Since special academic outline represents rather endeavor to the west bank, securities in the population are typically whole to natural screening."

Thanks for the help :), Mort.
Saturday, December 05, 2009 4:45:15 PM (Central European Standard Time, UTC+01:00)
Hello everyone. Good company and good discourse are the very sinews of virtue.
I am from Britain and also am speaking English, please tell me right I wrote the following sentence: "Strattera, only, focus has profoundly to manufacture with production people."

With respect :p, Brendan.
Name
E-mail
Home page

Comment (Some html is allowed: a@href@title, strike) where the @ means "attribute." For example, you can use <a href="" title=""> or <blockquote cite="Scott">.  

Enter the code shown (prevents robots):

Live Comment Preview