Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: SQL-CE VS2010 VB.NET
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()
                mySqlCmd.ExecuteNonQuery()
                mySqlConn.Close()
                Me.Hide()
                MasterForm.Show()
            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 9:24am
Comments
Wes Aday at 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 at 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
good
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.
  Permalink  
Comments
Gary Heath at 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 at 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 at 20-Jul-12 14:54pm
   
Everything's wrapped in Using blocks now, thanks everybody ... and yes, lewax00, that makes a lot of sense !!!
OriginalGriff at 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.
http://blogs.msdn.com/b/kimhamil/archive/2008/03/15/the-often-non-difference-between-close-and-dispose.aspx
lewax00 at 20-Jul-12 16:05pm
   
MSDN disagrees, "Close and Dispose are functionally equivalent", from http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnection.close.aspx#Y0
Gary Heath at 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 at 20-Jul-12 15:28pm
   
:laugh:
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 at 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 VB.net 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 at 20-Jul-12 15:43pm
   
You're welcome!

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

  Print Answers RSS
0 Marcin Kozub 310
1 OriginalGriff 248
2 Sergey Alexandrovich Kryukov 215
3 Richard MacCutchan 202
4 Praneet Nadkar 197
0 OriginalGriff 8,048
1 Sergey Alexandrovich Kryukov 7,287
2 DamithSL 5,614
3 Manas Bhardwaj 4,986
4 Maciej Los 4,910


Advertise | Privacy | Mobile
Web04 | 2.8.1411023.1 | Last Updated 20 Jul 2012
Copyright © CodeProject, 1999-2014
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