473,506 Members | 13,088 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

sql connection; multiple connections?

Hi,

I have an .net windows form application and sql server 2005.
In one of the functions I have one connection for a sqldatareader and
after that I want to use other connections to sql server (do some
things like insert and update statements).
At this moment I create other SqlConnection objects in this way :
SqlConnection newSqlConn=new
SqlConnection(basicSqlConnection.ConnectionString) . And this procedure
two or three times.

Is there other possibilities to do this? (If it is possible to avoid so
many SqlConnections)?

Thanks.

Apr 4 '06 #1
5 15051
Iwan Petrow wrote:
[...snip...]
In one of the functions I have one connection for a sqldatareader and
after that I want to use other connections to sql server (do some
things like insert and update statements). [...snip...]

Why would you want to use another connection ? Using the same connection for
doing both would be fine (as long as you don't need different credentials to
perform selects, inserts and updates, but I'd consider this useless).

Is there other possibilities to do this? (If it is possible to avoid so
many SqlConnections)?

[...snip...]

Continue using your connection until you need to close it ;-)
Apr 4 '06 #2
Iwan,

Quite simply, you won't need to create multiple SQLConnections. The
first time you create your first, VS will open a pool of them for you,
and the next time you need a SQLConnection object, VS will look at the
pool to see if any are currently available. If not, it will dynamically
add more to the pool.

The best way to do all of this cleanly is to use the "Using ()..."
syntax as in this code fragment:

using (SqlConnection sqlConn = new
SqlConnection(AppGlobals.ConnString))
{
sqlConn.Open();

cSelect = " select blah from Yaaah";

SqlDataAdapter sdaLVData = new SqlDataAdapter(cSelect, sqlConn);
DataSet dsLVData = new DataSet();
sdaLVData.Fill(dsLVData, "SerNums");

// Do something!...
foreach (DataRow oRow in dsLVData.Tables["SerNums"].Rows)
{
blahh...
}
sqlConn.Close();
sqlConn.Dispose();

}

Just do a google on SQL and connection and pool, and you'll get lots
more on this.

Happy coding
Robert

Apr 4 '06 #3
PS - forgot to add that you do this anywhere and everywhere you need a
SQLConnection. That's all there is to it!

Apr 4 '06 #4
I use datareader (one open connection). When I try to use the same open
connection it throws an exception that the connection is opened and is
used by datareader.

Apr 4 '06 #5
Iwan,

Look at the example I gave you - you need this kind of construction
each time you wish to create and use a datareader. Don't worry about
the number of connections - this is all handled automatically by VS
itself. Each time you use a construction like the following:

using (SqlConnection sqlConn = new
SqlConnection(AppGlobals.ConnString))
{
sqlConn.Open();

//Create datareader #1...
//
// use it...
//
// then discard it.

sqlConn.Close();
sqlConn.Dispose();
}

using (SqlConnection sqlConn = new
SqlConnection(AppGlobals.ConnString))
{
sqlConn.Open();

//Create datareader #2...
//
// use it...
//
// then discard it.

sqlConn.Close();
sqlConn.Dispose();
}
, etc.,

you are simply asking VS to give you one of the available connections
in the pool. Nothing is being wasted here, and pooling connections is
the most efficient way of handling things. That's why VS does this for
you...

HTH,
Robert

Apr 4 '06 #6

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

Similar topics

8
1587
by: Natan | last post by:
Hi, I`m creating a asp.net intranet system, and would like to put all the logic of the system in a single dll. That is, when i need to create an user for example, i would use "User.Check()" and...
26
16455
by: Rajeev Tipnis | last post by:
http://support.microsoft.com/default.aspx?scid=kb;en- us;819450 Questions: 1) Is this patch (fix) applicable to the .NET 1.1 Framework as well? That is, if we have Framework 1.1 (On...
6
5625
by: David Rasmussen | last post by:
I use urllib2 to do some simple HTTP communication with a web server. In one "session", I do maybe 10-15 requests. It seems that urllib2 opens op a connection every time I do a request. Can I...
14
3480
by: WebMatrix | last post by:
Hello, I have developed a web application that connects to 2 different database servers. The connection strings with db username + password are stored in web.config file. After a code review,...
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
1681
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...
0
1246
by: =?Utf-8?B?TWFyYyBXb29sZnNvbg==?= | last post by:
Hello, I can't seem to find any information on how best to implement multiple connections on a consumer web part and was wondering if anyone could point me in the right direction. I have...
1
5921
by: jacobyte | last post by:
HI have a few questions regarding IIS5.1 and the the 10 connection limit, If anyone can help clarify this I would greatly appreciate any help. - this is the situation. We host a site on a win2k3...
9
377
by: Bill E. | last post by:
I'm creating an MS Access application that connects to a SQL Server 2005 database using pass-through queries and ADO to call stored procedures. I ran a SQL trace and started clicking through...
9
6256
by: Gordon | last post by:
I want to add a feature to a project I'm working on where i have multiple users set up on my Postgres database with varying levels of access. At the bare minimum there will be a login user who...
0
7308
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
7371
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
7023
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
7479
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
5617
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
5037
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
3188
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...
0
3178
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
757
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.