473,761 Members | 3,542 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Best practice to work with multiple DB connections

Hi,

We have a windows application developed in c# and SQL Server 2005. Our
application need to execute more than one command (ExecuteReader and
ExecuteScalar) at a single time. Till now we have we use with only one
connection created during start up of application and will be
displose/closed when user logs out of application. With the single database
connection we have problems when trying to execute more than command at a
particular time.
Can you tell me the best practice to solve this problem? I am thinking the
following ways

1) Is it better to open and close connection whenever we need to execute a
command? But the problem with this is the Sql server runs on a network
server and it is taking much time to establish/create a database connection.

2) By using Connection pool. I am thinking to have a connection pool with
minimum connections of 1 and maximum connections as 5. But I read some
articles that connection pooling is generally used in web applications. I am
not sure whether it is helpful for windows based application or not.

Please suggest me the best practice to solve my problem.

Thanks in advance.
Srikanth
Oct 1 '07 #1
3 3166
0) Since you are on SQL2005, try enabling MARS; this will allow you to
call ExecuteScalar -on the same connection - while ExecuteReader is in
progress. Sounds too good to be true, but worth a try. Just tweak the
connection string to include ";MultipleActiv eResultSets=Tru e"

1/2) is not as big an issue as you think, since .NET provides pooling
by default. When you Close() a managed connection it (by default)
simply goes back into the pool. There is a very good chance (in such
an app) that you will get the same actual connection (albeit reset)
next time.

Marc
Oct 1 '07 #2
Thanks for your valuable input. Can you suggest the best one out of your two
suggestions?

"Marc Gravell" <ma**********@g mail.comwrote in message
news:uV******** ******@TK2MSFTN GP05.phx.gbl...
0) Since you are on SQL2005, try enabling MARS; this will allow you to
call ExecuteScalar -on the same connection - while ExecuteReader is in
progress. Sounds too good to be true, but worth a try. Just tweak the
connection string to include ";MultipleActiv eResultSets=Tru e"

1/2) is not as big an issue as you think, since .NET provides pooling by
default. When you Close() a managed connection it (by default) simply goes
back into the pool. There is a very good chance (in such an app) that you
will get the same actual connection (albeit reset) next time.

Marc
Oct 1 '07 #3
both ;-p

Use MARS when MARS is appropriate, otherwise pooling. Personally I
tend to use the "open, use, close" approach to connection management -
i.e. keep it local and short-lived (and let pooling work its magic),
until you /know/ (ideally via profiling) that a block of code really
warrants explicit connection handling. You can use a long-lived
connections, but it doesn't always fit well with every situation (for
example enterprise library will handle the connections internally).
Additionally, this approach tends to make it easy to port the code
into a more stateless model (such as aspx or web-services).

Marc
Oct 1 '07 #4

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

Similar topics

2
3656
by: Joe Bloggs | last post by:
I have a general question on best practice regarding data access. I have the code below, a static method defined in a class that I use in a data layer dll. The method takes a string as its parameter, connects to the database , executes the string (in this case SQL) in the form of a SqlDataReader and then returns the SqlDataReader. Here's the method... public static SqlDataReader SQLServerExecuteSQL(string SQLstr) {
0
1178
by: Ken Foster | last post by:
I built some remote services, a couple singletons and one single call. On the client side I thought I'd save on connection latency by caching the remote connection in a shared variable. Create it once, then use it as long as my app was running, rather than take the hit on creating and destroying it. The issues that one has with ADO connections staying open didn't seem to apply here. However when there is a lot of client requests,...
2
1149
by: Steve | last post by:
Hi, I am a relatively new user to vs.NET, and currently developing a ASP.NET web site using MS access, with the view to moving it to SQLServer. My problem is, how do I set up the connection to be utilise connection pooling, and to be easily maintained and modified. I was thinking of setting up a DSN, which is how I am currently connecting in ASP but I'm not sure how to do it in .NET. So I created a class which opens a connection and...
4
1574
by: Bill Borg | last post by:
Hello, I have a large commerce app, hosted for several hundred companies (i.e. each "company" is a small business selling something through my site, independent of all the others). Each company has a unique ID (e.g. 12345), which is a primary key in the tables for products, customers, etc. That key also identifies company-specific customization of my site, such as text strings, various styles, etc.
17
8041
by: | last post by:
I have an app that retrieves data from an Access database. At the moment I have the SQL string as a Const in my app. I understand this is not best practice. I don't want the user to have access to read or modify this string so I don't want to store it in an INI / Text file or in registery. Can someone please tell me the best practice for this. Thanks Mike
20
6640
by: Keith G. Murphy | last post by:
I'm trying to get a feel for what most people are doing or consider best practice. Given a mod_perl application talking to a PostgreSQL database on the same host, where different users are logging onto the web server using LDAP for authentication, do most people 1) have the web server connecting to the database using its own user account (possibly through ident), and controlling access to different database entities strictly through...
1
5326
by: Yelena Varshal via AccessMonster.com | last post by:
Hello, What are the pre-requisites / conditions for the ability to create multiple connections to MS ACCESS database and what is the precedence of its application? adModeShareDenyNone in the code, Exclusive checkbox in the ODBC source, Tools->Options->Advanced->Shared in the Database Options, what else? I have a problem when multiple connections could not be opened at the same time from my code on one server but could be on another server....
9
7301
by: david | last post by:
I have a class with some business-logic and with every roundtrip, I need an instance of this class, so I have to create it, every time again. That doesn't seem very efficient. I thought it would be 'better' to store an instance of this class in a session-variable, so it's available all the time and needs to be instanced only once. Is this, generally speaking, a good idea, storing objects in session-variables ? Do you guys ever use this...
51
3662
by: bigHairy | last post by:
Hello. I have been teaching myself .NET over the last few months and have had some success. I would like to ask a question though... A number of examples I have followed have the following in their finally statement Try ......
0
9554
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
9811
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...
0
8814
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7358
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
6640
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
5405
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3913
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3509
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2788
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.