.NET Tip: Managing Resources with the Using Statement Nashville TN

Take control of the lifetime of your objects. Release your object's resources on your schedule, not when the CLR determines they are no longer needed.

Local Companies

Crosslin & Associates PC
615-320-5500
2525 W End Ave
Nashville, TN
Advanced Network Solutions
615-277-0500
105 Broadway
Nashville, TN
Peachtree Software Support
615-256-1111
415 4th Ave S
Nashville, TN
Advanced Technical Services
615-747-5810
200 Hill Ave
Nashville, TN
Computer Services
615-252-8044
230 Willow St
Nashville, TN
Affiliated Computer Services
615-874-3000
801 Royal Pkwy
Nashville, TN
Computer Repair
615-882-9446
2744 Old Lebanon RD
Nashville, TN
Cumberland Technical Services
615-297-6815
420 Elmington Ave
Nashville, TN
Cyberangel Security Solutions Inc
615-837-9191
475 Metroplex DR
Nashville, TN
Cbnc LLC
615-831-3270
5360 Edmondson Pike
Nashville, TN

provided by: 
Originally published at Internet.com


Last week, I showed you how you can help ensure that your objects release their resources by implementing IDisposable. This week, I want to show you the using statement that allows you to specify when your objects release their resources. Objects declared in the using statement will explicitly be disposed when the end of the using block is reached. I often use nested using statements for data access to precisely control the creation and disposal of my objects. Here is how you can take advantage of the using statement to create a database connection, create a command, and execute a data reader to access your data. using (SqlConnection CN = new SqlConnection("*** Your connection string here ***")) { CN.Open(); using (SqlCommand cmd = new SqlCommand("SELECT * FROM authors", CN)) { using (SqlDataReader dr = cmd.ExecuteReader()) { // Access elements items here } } }

By nesting the using statements above, you can be sure you are creating objects only when necessary and that, once everything is finished, your objects will all be disposed of in a timely manner. If an error should occur anywhere along the way, any objects created up to that point will still be disposed of when the using block is exited. The only caveat is that the object declared in the using statement must implement the IDisposable interface. Keep this in mind if you plan on taking advantage of the using statement with your own classes.

About the Author

Jay Miller is a Software Engineer with Electronic Tracking Systems, a company dedicated to robbery prevention, apprehension, and recovery based in Carrollton, Texas. Jay has been working with .NET since the release of the first beta and is co-author of Learn Microsoft Visual Basic.NET In a Weekend. Jay can be reached via email at jmiller@sm-ets.com.

Author: Jay Miller

Read article at Internet.com site

Featured Local Company

Crosslin & Associates PC

615-320-5500
2525 W End Ave
Nashville, TN

Related Local Events
Christians In Action Trade Show
Dates: 10/16/2009 - 10/17/2009
Location: Gaylord Opryland Resort & Convention Center
Nashville, TN
View Details

MS PowerPoint 2002 (XP) Level Two
Dates: 7/16/2009 - 7/16/2009
Location: Greater Nashville Association of Realtors
Nashville, TN
View Details

Intergraph 2009
Dates: 6/15/2009 - 6/18/2009
Location: Gaylord Opryland Resort & Convention Center
Nashville, TN
View Details