473,407 Members | 2,629 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,407 software developers and data experts.

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 1526
On Sep 20, 9:23 am, "Marine" <n...@none.orgwrote:
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(connectionString))
{

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
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...
13
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...
1
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...
5
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 =...
6
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...
1
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...
2
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....
8
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...
2
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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
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
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
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...

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.