473,776 Members | 2,542 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

When do i have to open the connection ?!

Hi,

My project must connect to a Sql Server quite often to read/write datas.
I am wondering : do i have to open the connection to the Sql Server at the
beginning when the application loads, then
leave the connection open and close it when the application closes ? Or is
it more efficient toopen and close the connection each time i have to use a
datareader, a command etc...

Thanks !
Sep 20 '07 #1
4 1537
On Sep 20, 9:23 am, "Marine" <n...@none.orgw rote:
My project must connect to a Sql Server quite often to read/write datas.
I am wondering : do i have to open the connection to the Sql Server at the
beginning when the application loads, then
leave the connection open and close it when the application closes ? Or is
it more efficient toopen and close the connection each time i have to use a
datareader, a command etc...
Open the connection when you need it and close it immediately
afterwards. Connection pooling will take care of the underlying
connection to the database.

Jon

Sep 20 '07 #2
Hi Marine,

ado.net is designed to be used this way. Actually, connections are
pooled and even if you think the connection is completely
created/disposed each time you use it, in fact it's still in memory.
The key word is : let ado.net handle this for you.

By the way : if you store the connection object in the
session/application store it has to be marchalled/unmarshalled each time
you acccess it (slow).

Hope it helps.

Marine wrote :
Hi,

My project must connect to a Sql Server quite often to read/write datas.
I am wondering : do i have to open the connection to the Sql Server at the
beginning when the application loads, then
leave the connection open and close it when the application closes ? Or is
it more efficient toopen and close the connection each time i have to use a
datareader, a command etc...

Thanks !

Sep 20 '07 #3
My project must connect to a Sql Server quite often to read/write datas.
I am wondering : do i have to open the connection to the Sql Server at
the beginning when the application loads, then leave the connection open
and close it when the application closes ? Or is it more efficient
toopen and close the connection each time i have to use a datareader, a
command etc...
Hello Marine,

Create the connection object and close the connection after every use.
The Connections to Sql Server are pooled anyways, so there is little
overhead creating new oonnection objects.

Always use the 'using' keyword to ensure you really close/dispose your
connections like so :
using (SqlConnection connection = new SqlConnection(c onnectionString ))
{

connection.Open ();
command.Execute ();
}

If you don't, you might end up leaking connections.

Ben
Sep 20 '07 #4
Hi Marine,

Its a good practice to close the connection once the task has been done.
Try to use Data Application Blocks for much more performance oriented tasks,
as it takes care of most of the things.

Thanks..!

"Marine" wrote:
Hi,

My project must connect to a Sql Server quite often to read/write datas.
I am wondering : do i have to open the connection to the Sql Server at the
beginning when the application loads, then
leave the connection open and close it when the application closes ? Or is
it more efficient toopen and close the connection each time i have to use a
datareader, a command etc...

Thanks !
Sep 20 '07 #5

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

Similar topics

1
7388
by: David Gamble | last post by:
I am having problems with long timeouts when connecting to or executing commands in SQL Server 2000 SP3. I am writing an application that will be used on laptops within an environment that is wireless. The application is written in VB.NET using the SQL Server managed provider. I have set the "Connect Timeout" parameter on the connection string to 15 seconds and whenever I execute a command, I set the CommandTimeout property to 15...
13
3482
by: Jan | last post by:
Hi I have a database that I use to keep track of the sales promotions that we send to companies. I normally send a mailing based on a subset of the companies in the database (found using the query: QryICTMassDistribution3) , I then use a form and the code below to create a new record in the corrispondence table to show what corrispondence has been sent to various companies.
1
3100
by: Brian | last post by:
It's still not working - is still looking for the Database in the same folder as the photo just added. But i've realised something... bear with me here! :) The basics of the way my photo album works is as follows: - A database containing photo_id (as a primary key), location (location of photo on disk), caption etc are contained in a table in a Database (photoDB.mdb) in the Debug folder of the project
5
1575
by: Joachim | last post by:
When executing the following code ArrayList list = new ArrayList(); try { //Make sure connection is open if (!connection.State.ToString().Equals("Open")) connection.Open(); DataSet list = new DataSet();
6
1327
by: Brian Henry | last post by:
is there a way to tell when the database connection is retrieveing data? i saw the connectionstate.fetching enumerated value, but of course it's not implemented yet... is there another way to do this? thanks!
1
3898
by: Billy | last post by:
Hi All, I'm attempting to use the MapNetworkDrive <snippedbelow from entire code below with very poor performance results. Basically, I have very small 73kb text files that are rewritten daily to a network share using the MapNetworkDrive command to connect to the valid \\servername\share path. After successfully connecting the drive in the ASP code, the text file
2
12305
by: =?Utf-8?B?U3VuaWwgUGFuZGl0YQ==?= | last post by:
Hi All, I have created a VB.Net application that used classic ADO to access the oracle database. I have mentained a persistant connection in it. When i start the application and it works fine. All of a sudden i start receiving the following error messages countineoully as my appllication polls the database afetr some specified amount odf time. "Error : No. :3709 :The requested operation requires an OLE DB Session object, which is not...
8
13257
by: mark_aok | last post by:
Hi all, I have a split database. Both the forms, and the tables are stored on a shared network drive (this is Access 2003). The users use the forms, and the tables on the network drive, there are no local copies. When connection to this drive is lost, Access CRASHES. It does it every single time. Does anyone know if there is a way to check if
2
2449
by: TG | last post by:
Hi! Once again I have hit a brick wall here. I have a combobox in which the user types the server name and then clicks on button 'CONNECT' to populate the next combobox which contains all the databases in that server. Then after the user selects a database, I have another button that he/she click and I want to retrieve file groups from a specific table. At this point when he/she clicks on that button I get an error:
0
9625
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
9459
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10285
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...
0
10117
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9921
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
7467
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
6721
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
5365
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
5489
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.