.NET Tip: Managing Resources with the Using Statement Dallas TX

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

Videotex Systems
972 231 9200 x124
10255 Miller Road
Dallas, TX
elKore Inc
1-877-355-6731
12225 Greenville Avenue
Dallas, TX
Information Processing Corporation
972-404-9244
13747 Montfort, Suite 300
Dallas, TX
Maxtrus Micro
(800) 910-2380
4781 Snow Dr
Mesquite, TX
Spatial Data Inc.
972-791-0911
4545 Fuller Drive
Irving, TX
C. Pitman Baker & Associates
972-579-1642 X 227
1431 Greenway Dr.
Irving, TX
In2 Solutions Inc
(972) 494-7400
3534 W Walnut St
Garland, TX
P O Express Inc
(972) 272-0603
3717 Burning Tree Ln
Garland, TX
Sixred, Inc.
(972) 365-3437
15950 North Dallas Parkway
Dallas, TX
Freeman Software
(972) 840-1119
506 Valencia Dr
Garland, TX

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

Videotex Systems

972 231 9200 x124
10255 Miller Road
Dallas, TX
http://www.videotexsystems.com/

Related Local Events
Unconventional Gas Conference & Exhibition
Dates: 9/29/2009 - 10/1/2009
Location: Fort Worth Convention Center
Fort Worth, TX
View Details

Tech Titans Fast Tech Awards Gala
Dates: 8/28/2009 - 8/28/2009
Location: Renaissance Dallas Richardson Hotel
Richardson, TX
View Details

AHS International 65th Annual Forum and Technology Display
Dates: 5/27/2009 - 5/29/2009
Location: Gaylord Texan Resort & Convention Center
Grapevine, TX
View Details