473,654 Members | 3,078 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 7186
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.co m

"Marcel Hug" <ma**********@c h.abb.com> wrote in message
news:u6******** ******@TK2MSFTN GP14.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
3140
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
1934
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 DataSet, it is trimming the trailing spaces. Anybody know why? Here is my code: System.Data.OleDb.OleDbConnection connection = null;
15
9134
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
1963
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 OleDbConnection(ConnectionString); theConn.Open(); OleDbCommand cmd = new OleDbCommand(theSQL, theConn);
1
1229
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), Friday char(5))"), tableName); OleDbConnection conn = new OleDbConnection(Form1.ConnectionString); conn.Open(); OleDbCommand cmd = new OleDbCommand(cmdText, conn); ...
6
2740
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 OleDbConnection. After the thread is complete, I'm noticing that the Access ..ldb lock file still remains, and does not disappear until I close all the way out of my program. Since the call to Open the OleDbConnection is inside of the thread (in the...
54
5186
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, ~MyLogClass(), I call: tw.WriteLine("some stuff"); tw.Close();
1
2079
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 reader? I thought there might be a cleaner way, this code seems to take "forever" to run. '-------BEGINNING OF CODE EXAMPLE------- Dim SBInvNum As String Dim strConInv As String = "Provider=Microsoft.JET.OLEDB.4.0;Data Source =" &...
3
2512
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 in a com interop dll and it must be guaranteed, that the final sequence is send to the serial device, when the calling application dont send Close() or Dispose().
0
8372
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8814
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8475
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8591
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6160
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5621
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4149
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4293
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1915
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.