Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Closing datareaders and connections. 1

Status
Not open for further replies.

columbo2

Technical User
Jul 14, 2006
97
GB
Hi All,

I've seen a bit on the web about closing conncetions and datareaders.

I have consequently been putting try-catch-finally blocks around my connections and datareaders and closing the connections in the finally block.

My question is: If you have a data source or datareader dragged and dropped on to the HTML page (not expilitly define in the code behind), do you need to close it in the code behind or is this done automatically?

Also, what happens if you don't close data readers, connections etc?

Do datarepeaters need to be closed as well.

Tahnks
C
 
actions like accessing files and database connections require alot of resources by the machine. by alot i mean in comparison to the other non-file access operations (loops, switch statements, equations...) because of this is always a good idea (should be required) to close/flush and dispose of resource when done.

I'm pretty sure the drag/drop objects clean up after themselves. another trick to save a few keys strokes is the using keyword.
Code:
try
{
   using (IDbConnection cnn = new SqlConnection())
   {
      new IDbCommand cmd = new SqlCommand(sql, cnn);
      cmd.ExecuteReader();
   }
}
catch(SqlException) { }
this will automatically close and dispose of the sql connection object along with any object created within the using block.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Thanks Jason, great tip, I'll do that in future
C
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top