.NET Tip: Managing Resources with the Using Statement Phoenix AZ

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

BBB Systems, LLC
(602) 424-0914
2340 W. Mission
Phoenix, AZ
Fields Consulting Group Inc.
(602) 944-3901
927 W. Hatcher
Phoenix, AZ
Lariat Software, LLC.
800-647-2764
9201 N. 25th Ave. #260
Phoenix, AZ
CREOSO Corporation
1.866.665.0533
2810 S. 24th Street
Phoenix, AZ
Lone Wolf Software Inc.
(623) 824-1140
6097 N 57th Drive
Glendale, AZ
Tec-Way
(602) 524-7203
Bret Phaneuf
Phoenix, AZ
Amerytech Networks
(732) 986-0125
6201 W. Olive Ave. #1130
Glendale, AZ
Mac Media Inc
480-964-6352
6928 E 5th Ave
Scottsdale, AZ
Lexcel Solutions
480-874-0443
4110 N Scottsdale RD
Scottsdale, AZ
Lynk Software Inc
480-998-1933
PO Box 5498
Scottsdale, AZ

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

BBB Systems, LLC

(602) 424-0914
2340 W. Mission
Phoenix, AZ

Related Local Events
Toastmasters
Dates: 11/26/2009 - 11/26/2009
Location: Peoria Chamber of Commerce
Peoria, AZ
View Details

The Big Draw VI
Dates: 12/1/2009 - 12/1/2009
Location: The Crown Room
Scottsdale, AZ
View Details

Toastmasters
Dates: 1/7/2010 - 1/7/2010
Location: Peoria Chamber of Commerce
Peoria, AZ
View Details

Toastmasters
Dates: 2/4/2010 - 2/4/2010
Location: Peoria Chamber of Commerce
Peoria, AZ
View Details

Toastmasters
Dates: 3/25/2010 - 3/25/2010
Location: Peoria Chamber of Commerce
Peoria, AZ
View Details