471,319 Members | 2,675 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,319 software developers and data experts.

Close of OleDBConnection in Destructor

Hi NG !
I would like to close my OleDBConnection in the destructor of the class,
but i get a failure, that the handle is not initialized. I read in the
msdn, that it is not good the close the oledbconnection in the finalize.
So it's the same with the destructor ?

Thanks ?
Regards
Dec 29 '05 #1
4 7060
Looks like your oledbconnection is already set to nothig
-------
Regards ,
C#, VB.NET , SQL SERVER , UML , DESIGN Patterns Interview question book
http://www.geocities.com/dotnetinterviews/
My Interview Blog
http://spaces.msn.com/members/dotnetinterviews/

Dec 29 '05 #2
Marcel,

It is not a destructor. It is a finalizer, which means that the object
does not get cleaned up when it goes out of scope. In this case, you need
to implement the IDisposable interface and expose an implementation of
Dispose which will close the handle when you are done with it.

Additionally, as a general rule, I wouldn't keep database references as
fields in classes anyways. I would create my connection in my method as
needed, and then close the connection when I am done with it (in the
method). If I do store it on the class level, then that says to me that the
class is a utility class which ultimately has a short life as well.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Marcel Hug" <ma**********@ch.abb.com> wrote in message
news:u6**************@TK2MSFTNGP14.phx.gbl...
Hi NG !
I would like to close my OleDBConnection in the destructor of the class,
but i get a failure, that the handle is not initialized. I read in the
msdn, that it is not good the close the oledbconnection in the finalize.
So it's the same with the destructor ?

Thanks ?
Regards

Dec 29 '05 #3
In C#, destructors are the way you implement Object.Finalize()
So the answer to your question is Yes

"Marcel Hug" wrote:
Hi NG !
I would like to close my OleDBConnection in the destructor of the class,
but i get a failure, that the handle is not initialized. I read in the
msdn, that it is not good the close the oledbconnection in the finalize.
So it's the same with the destructor ?

Thanks ?
Regards

Dec 29 '05 #4
Hi Nicholas
It is not a destructor. It is a finalizer, which means that the object
does not get cleaned up when it goes out of scope. In this case, you need
to implement the IDisposable interface and expose an implementation of
Dispose which will close the handle when you are done with it.
Ohh thanks! I'm new in .NET programming so I'm sorry. But now I know
that I have to read the fundamental concept.
Additionally, as a general rule, I wouldn't keep database references as
fields in classes anyways. I would create my connection in my method as
needed, and then close the connection when I am done with it (in the
method). If I do store it on the class level, then that says to me that the
class is a utility class which ultimately has a short life as well.


Yes in each example I saw this kind of connecting a database. I have
changed my code now. Thanks

Regards
Dec 29 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

10 posts views Thread by Venkat | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.