473,408 Members | 1,707 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,408 software developers and data experts.

destructor behaviour

What are the advantages and disadvantages of opening and closing database
connection in a web application in the following examples?

1: using(SqlConnection connection = new SqlConnection()) {}

2:

SqlConnection connection = null;
try
{
connection = new SqlConnection();
}
finally
{
if (connection != null) connection.Close();
}

3. SqlConnection connection = new SqlConnection();
...
connection.Close();

Is there a general preferred way?
Nov 16 '05 #1
1 1017
Not sure what this has to do with destructors but anyway ...

1. Generally the preferred way - you have to write less code and its exception proof - although you cannot reopen the connect after the constryct (which isn't a problem as hte connection variable out of scope

2. More manual, easier to make mistakes but doesa allow you to reopen the connection later in the method

3. A disaster if an exception is thrown between the Open and Close (even though you don't show the Open). Don't do this.

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

What are the advantages and disadvantages of opening and closing database
connection in a web application in the following examples?

1: using(SqlConnection connection = new SqlConnection()) {}

2:

SqlConnection connection = null;
try
{
connection = new SqlConnection();
}
finally
{
if (connection != null) connection.Close();
}

3. SqlConnection connection = new SqlConnection();
...
connection.Close();

Is there a general preferred way?

--
No virus found in this incoming message.
Checked by AVG Anti-Virus.
Version: 7.0.300 / Virus Database: 265.7.1 - Release Date: 19/01/2005

[microsoft.public.dotnet.languages.csharp]
Nov 16 '05 #2

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

Similar topics

2
by: Paul MG | last post by:
Hi A brief question: Is it generally considered idiomatic/good C++ to have behaviour in the destructor of a function object? For instance, consider a function object whose constructor takes...
52
by: Newsnet Customer | last post by:
Hi, Statement 1: "A dynamically created local object will call it's destructor method when it goes out of scope when a procedure returms" Agree. Statement 2: "A dynamically created object...
11
by: Stub | last post by:
Please answer my questions below - thanks! 1. Why "Derived constructor" is called but "Derived destructor" not in Case 1 since object B is new'ed from Derived class? 2. Why "Derived destructor"...
4
by: Charles Jamieson | last post by:
I declare a class class myClass{ public: ~myClass();
37
by: WittyGuy | last post by:
Hi, I wonder the necessity of constructor and destructor in a Abstract Class? Is it really needed? ? Wg http://www.gotw.ca/resources/clcm.htm for info about ]
11
by: Ken Durden | last post by:
I am in search of a comprehensive methodology of using these two object cleanup approaches to get rid of a number of bugs, unpleasantries, and cleanup-ordering issues we currently have in our...
6
by: sacgarg | last post by:
Following code is behaving strange: class WithPointerMember { char* str; const size_t size; public: WithPointerMember():size(15) //ctor {
17
by: Phlip | last post by:
C++ers: I have this friend who thinks C++ cannot throw from a destructor. I think throwing from a destructor is bad Karma, and should be designed around, but that C++ cannot strictly prevent...
4
by: =?utf-8?Q?David_C=C3=B4me?= | last post by:
Hello everybody. I have a question. When i want use a pure virtual destructor in one of my classe, i must give a definition of this destructor like this: class A { //a lot of things virtual...
12
by: mc | last post by:
Hi Experts, This may be obvious but I can't find anything one way or another. We have a class which is always allocated using new, e.g. Foo* foo = new Foo() so we came up with the idea of...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...
0
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
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...

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.