473,395 Members | 1,762 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,395 software developers and data experts.

Database Connection Passing Problem

Hey all. I currently have two seperate namespaces and I'm trying to
pass a connection around to them. I want to use the same connection &
leave it open for 6 methods & then close it. However, all 6 of those
methods use that same connection.

So my solution was to pass the SqlConnection as a parameter in each
method. This works, but it goes super slow. When I changed it back
and put the methods all back in the same namespace & didnt pass the
connection object things went *much* faster. Is there a better (i.e.
quicker) way to accomplish this same thing? Maybe passing a connection
string and starting a new connection every time?? I just don't know.
Any help would be greatly appreciated!

namespace MyNameSpace
{
public static getThisID(string strName, sqlConnection sqlDbCon)
{
System.Data.SqlClient.SqlCommand sqlCommand = new
System.Data.SqlClient.SqlCommand();

// I then assign the connection for this command as the passed
sqlDbCon
sqlCommand.Connection = sqlDbCon;

// do the sql command execute code and then exit

}
}
namespace stupid
{
// pass the connection in every method
MyNameSpace.Database.getThisID(string strName, sqlConnection
sqlDbCon)
MyNameSpace.Database.getAnotherID(string strName, sqlConnection
sqlDbCon)
MyNameSpace.Database.getAndYetAnother(string strName, sqlConnection
sqlDbCon)
(8 times)
}

Apr 18 '06 #1
4 2078
shade73 <tk******@gmail.com> wrote:
Hey all. I currently have two seperate namespaces and I'm trying to
pass a connection around to them. I want to use the same connection &
leave it open for 6 methods & then close it. However, all 6 of those
methods use that same connection.

So my solution was to pass the SqlConnection as a parameter in each
method. This works, but it goes super slow. When I changed it back
and put the methods all back in the same namespace & didnt pass the
connection object things went *much* faster. Is there a better (i.e.
quicker) way to accomplish this same thing? Maybe passing a connection
string and starting a new connection every time?? I just don't know.
Any help would be greatly appreciated!


The namespace should be absolutely irrelevant.

Passing the connection around should be fine. It sounds very odd that
it would be slow. On the other hand, if you don't actually need the
methods to be operating on the same transaction, you could just open
and close the connection each time and rely on the connection pool.
Aside from performance, that would be the simpler way to keep things
clean and make sure you always release things appropriately.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Apr 18 '06 #2
Ok, I'll try opening and closing the connection. It will if nothing
else like you said it would be quite a bit cleaner. Thanks for the
quick response

Apr 18 '06 #3
shade73,

Either "pass the connection string around" or put it in an appSettings or
connectionStrings node in your config file and get it from there. Open the
connection in each class / method just before it is used, and close it
immediately thereafter. This is the preferred best practice in almost all
cases; it allows your connections to be handled by Connection Pooling which
is quite efficient at its job.
Peter

--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"shade73" wrote:
Hey all. I currently have two seperate namespaces and I'm trying to
pass a connection around to them. I want to use the same connection &
leave it open for 6 methods & then close it. However, all 6 of those
methods use that same connection.

So my solution was to pass the SqlConnection as a parameter in each
method. This works, but it goes super slow. When I changed it back
and put the methods all back in the same namespace & didnt pass the
connection object things went *much* faster. Is there a better (i.e.
quicker) way to accomplish this same thing? Maybe passing a connection
string and starting a new connection every time?? I just don't know.
Any help would be greatly appreciated!

namespace MyNameSpace
{
public static getThisID(string strName, sqlConnection sqlDbCon)
{
System.Data.SqlClient.SqlCommand sqlCommand = new
System.Data.SqlClient.SqlCommand();

// I then assign the connection for this command as the passed
sqlDbCon
sqlCommand.Connection = sqlDbCon;

// do the sql command execute code and then exit

}
}
namespace stupid
{
// pass the connection in every method
MyNameSpace.Database.getThisID(string strName, sqlConnection
sqlDbCon)
MyNameSpace.Database.getAnotherID(string strName, sqlConnection
sqlDbCon)
MyNameSpace.Database.getAndYetAnother(string strName, sqlConnection
sqlDbCon)
(8 times)
}

Apr 18 '06 #4
I don't know what your problem is but I do know that it has absolutely
nothing to do with namespaces so there must be something that you are not
telling us.

Are they in different dlls?
What do you mean by supper slow?

"shade73" <tk******@gmail.com> wrote in message
news:11**********************@g10g2000cwb.googlegr oups.com...
Hey all. I currently have two seperate namespaces and I'm trying to
pass a connection around to them. I want to use the same connection &
leave it open for 6 methods & then close it. However, all 6 of those
methods use that same connection.

So my solution was to pass the SqlConnection as a parameter in each
method. This works, but it goes super slow. When I changed it back
and put the methods all back in the same namespace & didnt pass the
connection object things went *much* faster. Is there a better (i.e.
quicker) way to accomplish this same thing? Maybe passing a connection
string and starting a new connection every time?? I just don't know.
Any help would be greatly appreciated!

namespace MyNameSpace
{
public static getThisID(string strName, sqlConnection sqlDbCon)
{
System.Data.SqlClient.SqlCommand sqlCommand = new
System.Data.SqlClient.SqlCommand();

// I then assign the connection for this command as the passed
sqlDbCon
sqlCommand.Connection = sqlDbCon;

// do the sql command execute code and then exit

}
}
namespace stupid
{
// pass the connection in every method
MyNameSpace.Database.getThisID(string strName, sqlConnection
sqlDbCon)
MyNameSpace.Database.getAnotherID(string strName, sqlConnection
sqlDbCon)
MyNameSpace.Database.getAndYetAnother(string strName, sqlConnection
sqlDbCon)
(8 times)
}

Apr 18 '06 #5

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

Similar topics

3
by: DarthMacgyver | last post by:
Hello, I recently wrote a survey application. Each question is very similar. The first questions gives me a problem when there are multiple people taking the survey (The Database connection...
5
by: news | last post by:
Well, I wrote my first PHP class today. Yeah! But to get it to work, in each function within the class I have to repeat the database connection lines, and that just seems redundant; there has to...
1
by: Macca | last post by:
Hi, I have a C# Solution/Application that contain 4 projects. Each of these projects needs at some time to access the same database. I would like to know how to share a single connection...
3
by: DarthMacgyver | last post by:
Hello, I recently wrote a survey application. Each question is very similar. The first questions gives me a problem when there are multiple people taking the survey (The Database connection...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
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...
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
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,...

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.