473,509 Members | 6,048 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Having SQL Connection Open throughout C# windows application

AS
Hi,

I have a C# windows desktop application that will be used only by one
user at a time. The database server (SQL Server 2000) also resides on
that machine only.

This application has three logical layers (UI, Business layer and
DataAccess layer). Currently, SQL Server connection is opened when a
user first logs in. This connection object (a global variable) is
passed from every form to the business layer and then to the data
access layer where it is used. Finally, when the user logs out, the
connection is closed.

I am not sure if this is an efficient method. I would rather like to
open conenction in the data access layer when required and close the
connection once the job is done.

But I am not clear about the advantages and disadvantages of either of
these approaches. Any advice would be appreciated.

Jan 12 '06 #1
2 3046
Passing the connection (and/or a transaction) between your forms and to all
your data-access calls is an abvious headache; plus (to the purist view) it
breaks the functional separation of UI and data-access, since your UI now
knows about the database.

In ADO.net, you can use your second method very easily and efficiently; by
default, ADO.net connections are pooled, so when you let release it, it is
held for "a while". Next time you create a connection with the same
connection string, it is highly likely that you will retreive the same,
already open, connection to the database. You can see this by running sp_who
after closing a connection (in C#) - you will probably see it remains there
for a short while.

Before .Net 2.0, the main disadvantage of the second approach is that it
made it hard to carry a transaction between related database calls -
however, even this is now addressed if you use a TransactionScope (via
using); this acts like "COM+ without COM+", i.e. it creates an ambient
transaction that ADO.net connections will enlist within if necessary. Under
SQL Server 2005, this uses promotable transactions, which means that it uses
an ADO transaction if you work on one (SQL 2005) server, and then upgrades
to a full DTC transaction if you hit multiple servers in the same
TransactionScope; unfortunately, under SQL 2000 this will always use a full
DTC transaction (and IIRC, serializable isolation), so this may (OK, will)
have a performance hit over ADO transactions. For my view, the benefits
outweigh this (especially since I am migrating code that historically uses
DTC...)

Marc

"AS" <Su*****************@gmail.com> wrote in message
news:11*********************@g47g2000cwa.googlegro ups.com...
Hi,

I have a C# windows desktop application that will be used only by one
user at a time. The database server (SQL Server 2000) also resides on
that machine only.

This application has three logical layers (UI, Business layer and
DataAccess layer). Currently, SQL Server connection is opened when a
user first logs in. This connection object (a global variable) is
passed from every form to the business layer and then to the data
access layer where it is used. Finally, when the user logs out, the
connection is closed.

I am not sure if this is an efficient method. I would rather like to
open conenction in the data access layer when required and close the
connection once the job is done.

But I am not clear about the advantages and disadvantages of either of
these approaches. Any advice would be appreciated.

Jan 12 '06 #2
AS

Thanks Marc, for your guidance.

Marc Gravell wrote:
Passing the connection (and/or a transaction) between your forms and to all
your data-access calls is an abvious headache; plus (to the purist view) it
breaks the functional separation of UI and data-access, since your UI now
knows about the database.

In ADO.net, you can use your second method very easily and efficiently; by
default, ADO.net connections are pooled, so when you let release it, it is
held for "a while". Next time you create a connection with the same
connection string, it is highly likely that you will retreive the same,
already open, connection to the database. You can see this by running sp_who
after closing a connection (in C#) - you will probably see it remains there
for a short while.

Before .Net 2.0, the main disadvantage of the second approach is that it
made it hard to carry a transaction between related database calls -
however, even this is now addressed if you use a TransactionScope (via
using); this acts like "COM+ without COM+", i.e. it creates an ambient
transaction that ADO.net connections will enlist within if necessary. Under
SQL Server 2005, this uses promotable transactions, which means that it uses
an ADO transaction if you work on one (SQL 2005) server, and then upgrades
to a full DTC transaction if you hit multiple servers in the same
TransactionScope; unfortunately, under SQL 2000 this will always use a full
DTC transaction (and IIRC, serializable isolation), so this may (OK, will)
have a performance hit over ADO transactions. For my view, the benefits
outweigh this (especially since I am migrating code that historically uses
DTC...)

Marc

"AS" <Su*****************@gmail.com> wrote in message
news:11*********************@g47g2000cwa.googlegro ups.com...
Hi,

I have a C# windows desktop application that will be used only by one
user at a time. The database server (SQL Server 2000) also resides on
that machine only.

This application has three logical layers (UI, Business layer and
DataAccess layer). Currently, SQL Server connection is opened when a
user first logs in. This connection object (a global variable) is
passed from every form to the business layer and then to the data
access layer where it is used. Finally, when the user logs out, the
connection is closed.

I am not sure if this is an efficient method. I would rather like to
open conenction in the data access layer when required and close the
connection once the job is done.

But I am not clear about the advantages and disadvantages of either of
these approaches. Any advice would be appreciated.


Jan 12 '06 #3

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

Similar topics

14
3944
by: Agoston Bejo | last post by:
Hi! Why doesn't the following code work? Sub Session_OnEnd Dim Conn Set Conn = Server.CreateObject("ADODB.Connection") Conn.ConnectionString = Application("Connection_String") Conn.Open ...
4
1744
by: Mahesh D. Rane | last post by:
Hi, I am connecting to sql databse from windows C# form. when i close the form, then also the connection to databse is active. Its not allowing me to delete the database. But if i close the...
4
2745
by: Mark | last post by:
OK. Here we go. I have an ASP.NET application that does many hits to a SQL Server DB on a separate server. When I first created this application (2 years ago) and was very new to ASP/ASP.NET, to...
1
12073
by: mingki | last post by:
Hi Developers, I am a .Net developer of a Large Online Retailling Company. I would like to have your help on a Connection Pool issue. Recently we have developed a Web Application on ODP .Net...
35
4806
by: Terry Jolly | last post by:
Web Solution Goal: Have a global database connection Why: (There will be 30+ tables, represented by 30+ classes) I only want to reference the database connection once. I put the connection...
10
1682
by: rich | last post by:
I have a PHP5 application that accepts external messages, and inserts them into a database. It's rather high traffic.. the server could be receiving thousands at a time, and recently mysql has...
1
6038
by: Sankalp | last post by:
Hi, I am using VB 2005. My application has many data bound controls. The connection is stored in the app.config file. I want the application to start with a default connection string and while...
20
3244
by: fniles | last post by:
I am using VS2003 and connecting to MS Access database. When using a connection pooling (every time I open the OLEDBCONNECTION I use the exact matching connection string), 1. how can I know how...
3
4877
by: fniles | last post by:
In the Windows application (using VB.NET 2005) I use connection pooling like the following: In the main form load I open a connection using a connection string that I stored in a global variable...
0
7237
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
7347
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
7416
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...
1
7073
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...
0
7506
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
5656
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5062
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
3207
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
779
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.