473,503 Members | 11,783 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 7176
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
3124
by: Venkat | last post by:
Hi, I am using ifstream to open a file read it and then close it. Sample Code ------------------- ifStream inFile("file1.txt"); ....... .......
0
1930
by: Seth | last post by:
First off, my apologies if this is in the wrong newsgroup, but I hope I'm close enough. I'm trying to do some parsing of a CSV file using OleDbConnection, but for some reason, when I populate my...
15
9110
by: Charlie | last post by:
Hi: I have a destructor in my base class that is supposed to close a database connection when class is released. ~BaseTable() { this.Conn.Close(); }
1
1949
by: Keith | last post by:
Hi, if have this method in a cs file that connections to a database and returns a datareader to my web form. public OleDbDataReader GetDataReader(string theSQL) { theConn = new...
1
1220
by: Souloron | last post by:
Here what I did: string cmdText = String.Format(("CREATE TABLE {0} (dno int primary key, Sunday char(5), Monday char(5), Tuesday char(5), Wednesday char(5), Thursday char(5),...
6
2735
by: eforden | last post by:
Hi, I am new at using the BackgroundWorker so please forgive me if this topic has already been covered. I'm using BackgroundWorker to import data into an Access 2000 database using...
54
5134
by: Zytan | last post by:
I have a log class that makes a synchronized TextWriter like so, in the constructor: StreamWriter sw = new StreamWriter(filename); tw = TextWriter.Synchronized(sw); In the destructor,...
1
2067
by: Mel | last post by:
I am performing the same recordset multiple times, just passing different parameters each time. Is there a way to do this more efficiently without having to close and re-open the connection and...
3
2495
by: Rudi | last post by:
Hello, following problem: At program end or release an assembly a serial device should get a final exit sequence. How can I do this? With Dispose() it's no problem, but this assembly is used...
0
7194
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7070
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
6976
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
4993
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4666
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3160
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3148
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1495
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
372
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.