473,324 Members | 2,239 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

destructors,closing connections

Hello ppl!
How exactly do I close all the database connections that was open in
the config file and how do I use the destructor in C#.
Thanks a lot!
Nov 16 '05 #1
2 1538
juli,

The config file does not open database connections, you have to do that
explicitly in your code.

In C#, there is no destructor, rather, the term is "finalizer". This is
important, because the difference goes way beyond just a name change. The
finalizer is called when an object is collected by the garbage collector, it
doesn't get called when it goes out of scope. Because of this, you will
have to explicitly release the connection when you are done with it.

For this purpose, MS defined the IDisposable interface, indicating that
an object needs to be closed explicitly. To make it easier to use in C#,
there is the "using" statement as well.

For more information, check out the section of the .NET framework titled
"Implementing a Dispose Method", located at (watch for line wrap):

http://msdn.microsoft.com/library/de...posemethod.asp

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

"juli" <ji****@gmail.com> wrote in message
news:1f**************************@posting.google.c om...
Hello ppl!
How exactly do I close all the database connections that was open in
the config file and how do I use the destructor in C#.
Thanks a lot!

Nov 16 '05 #2
Hi juli:

The config file doesn't open a database connection, your C# code does.

The easiest way to ensure a connection is closed is with the using
clause:

using(SqlConnection connection = new SqlConnection(...))
{
// put connection to work
}

The using block ensures a Dispose method is called even if there is an
exception. Dispose for a SqlConnection will clean up the connection,
and return the connection to the free pool by default.

--
Scott
http://www.OdeToCode.com/blogs/scott/

On 8 Dec 2004 08:15:44 -0800, ji****@gmail.com (juli) wrote:
Hello ppl!
How exactly do I close all the database connections that was open in
the config file and how do I use the destructor in C#.
Thanks a lot!


Nov 16 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: dustin lee | last post by:
Over the years I've gotten out of the habit of explicitly closing file objects (whether for reading or writing) since the right thing always seems to happen auto-magically (e.g. files get written...
3
by: Anthony | last post by:
Hey all, Here's a question for you, my hosts have told me that that one my pages, php, was causing their server to reboot because there were too many open connections and that they should be...
1
by: C Sharp beginner | last post by:
I'm sorry about this verbose posting. This is a follow-up to my yesterday's posting. Thanks William for your reply. I understand it is a good practice to open connections as late as possible and...
3
by: Jimmy | last post by:
Hi when do you use destructors and what are the advantages /disadvantages? ch Jim
13
by: Simon Harvey | last post by:
Hi All, I have a colleague that I wprk with that develops using ASP. I develop using ASP.net. He seems to make sites much faster than me and I am wondering if its because of the two different...
7
by: darrel | last post by:
We're running into a problem on our new site. Once a week or so, our site goes down with an 'out of memory error'. Rebooting the web server fixes things. Googling the error doesn't return many...
7
by: Tumurbaatar S. | last post by:
Is it so important to close database connections? As I understand, after processing a request and sending a response, page object destroyed. So all used connections also destroyed. Yes?
17
by: vgrebinski | last post by:
Hi everyone, Suppose we have a diamond shaped diagram: A / \ B1 B2 \ / C and assume that C' constructor invokes
3
by: donna.lenharth | last post by:
Hello, I am debugging a C++ application that was ported from PostgreSQL to DB2 UDB (v8.1.0.112) on Linux and have run across a problem. We have a singleton object that handles connection...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.