.NET Tip: Managing Resources with the Using Statement Lansdale PA

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

CPA Technology, LLC
610-862-1998
101 W. Elm Street
Conshohocken, PA
The MediGroup, Ltd.
610-666-1955 x12
1308 Egypt Road
Oaks, PA
Deacom, Inc.
610-971-2278
950 West Valley Road, Suite 3000
Wayne, PA
Quintiq Inc.
610-964-8111
565 E Swedesford Rd #303
Wayne, PA
KasTech Consulting, Inc.
215-702-8155
116 N. Bellevue Avenue
Langhorne, PA
Helpline Center Office- Childrens Aid Society
(215) 361-6942
306 N Madison Ave
Lansdale, PA
Mousley Consulting
(215) 631-1366
608 W Main St
Lansdale, PA
Orsino Consulting
(215) 362-6302
Lansdale, PA
Adept Consulting Services Inc
(215) 855-3610
408 W Main St
Lansdale, PA
Idea Revolution
(215) 855-7045
531 S Broad St
Lansdale, PA

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

CPA Technology, LLC

Sage software, consulting & implementation, training (Sage Authorized Training Center)

610-862-1998
101 W. Elm Street
Conshohocken, PA
www.cpatechnology.net

CPA Technology is a leader in providing Sage software solutions to the construction and real estate industries in Eastern Pennsylvania, New Jersey, and Delaware.

www.cpatechnology.net

Related Local Events
Hot Java at the Watercooler
Dates: 12/23/2009 - 1/23/2010
Location: Odyssey Travel
Skippack, PA
View Details

IT Roundtable
Dates: 11/10/2009 - 11/10/2009
Location: D&E Communications
Brownstown, PA
View Details

Fraser Advanced Information Systems Mixer
Dates: 10/29/2009 - 10/29/2009
Location:
Bethlehem, PA
View Details

PREP 2009: 22nd International Symposium, Exhibit and Workshops
Dates: 7/19/2009 - 7/22/2009
Location: Loews Hotel
Philadelphia, PA
View Details