Click here to Skip to main content
13,147,037 members (64,915 online)
Rate this:
Please Sign up or sign in to vote.
See more: , +
As per the Subject line, a very quick question ...

I have code similar to this in various places in my program :

If mySqlConn.State = ConnectionState.Closed Then mySqlConn.Open()
            Using mySqlRdr As SqlCeDataReader = mySqlCmd.ExecuteReader()
            End Using

I've just noticed that although I always Open mySqlConn ... in some places I close MySqlConn & in some places I close mySqlRdr ... which should I be doing, or should I be doing both ?!?
Posted 20-Jul-12 8:24am
Wes Aday 20-Jul-12 14:28pm
For objects that implement IDisposable you should be wrapping their usage in using blocks like you are for your reader.
Gary Heath 20-Jul-12 14:36pm
Some of them are in Using blocks, some aren't, I need to go through and standardise them ... thanks

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

In the code above, you close both your Connection (explicitly, via teh Close method) and the Readewr (implitly, via the using block which does a Close and Dispose).

What you should be doing is closing both and (preferably) Disposing both, since they are scarce resources and should not be hogged!
Personally, I use a using block on the Connection, Command and Reader to be sure.
Gary Heath 20-Jul-12 14:37pm
Yes, I just mentioned in the comment above that some of them are, I need to standardise them ... does this mean that if they are in Using blocks, I can omit the Close, or is best practice to close the Connection anyway ?
lewax00 20-Jul-12 14:42pm
Generally, Close is just a call to Dispose anyways (assuming it derives from the Stream class, or uses a similar design). Calling it again doesn't hurt, but when the object is disposed it should release all resources, so you shouldn't need to. The best practice is really just to wrap it in a using block, and let the framework/language do it's job.
Gary Heath 20-Jul-12 14:54pm
Everything's wrapped in Using blocks now, thanks everybody ... and yes, lewax00, that makes a lot of sense !!!
OriginalGriff 20-Jul-12 15:02pm
That is certainly not the case for SqlConnections - Close and Dispose are different animals. Forms are the same, and I would guess that MySqlConnections are as well.
Gary Heath 20-Jul-12 15:15pm
Bloody hell, excuse me if I give that article a miss for the time being, I'm still struggling with the basic concept of the language !!!
OriginalGriff 20-Jul-12 15:28pm
It was aimed to lewax00 - it does get a bit technical for beginners!

Check the indentation on the messages - it shows what was a reply to what. :)
Gary Heath 20-Jul-12 15:38pm
Ok yes, I see now !!! I'm an experienced programmer, but all on IBM mainframes & have been mucking around with VBA, VB6 & now for about 4 or 5 years, I tried C# but gave it up, it's way too mental for an old boy like me !!! This is my first foray into SQL as well (though I've used DB2) and whilst the power and the adaptability of all this stuff is brilliant, it's also very difficult to grasp !!! Thanks for your help, both here and on previous questions, it's always appreciated ...
OriginalGriff 20-Jul-12 15:43pm
You're welcome!
lewax00 20-Jul-12 16:05pm
MSDN disagrees, "Close and Dispose are functionally equivalent", from

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web04 | 2.8.170915.1 | Last Updated 20 Jul 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100