views:

92

answers:

4

I am using ADO.Net for some database connectivity and I was just hoping I was doing it the right way. I am opening and closing multiple connections for each stored procedure. Or should I be wrapping this up in just one open connection (less resource on the database maybe?) thanks, and if there is anything odd or something I could be doing better let me know thanks!

Example: this but I have like 6 of them...

using (SqlConnection conn = new SqlConnection(ConnectionString))
        {
            SqlCommand cmd = new SqlCommand("spSelectAllTrip", conn);
            cmd.CommandType = CommandType.StoredProcedure;
            conn.Open();
            ddlTripTypeA.DataSource = cmd.ExecuteReader();
            ddlTripTypeA.DataTextField = "TripType";
            ddlTripTypeA.DataValueField = "TripTypeAID";
            ddlTripTypeA.DataBind();
        }
        using (SqlConnection conn = new SqlConnection(ConnectionString))
        {
            SqlCommand cmd = new SqlCommand("spSelectAllTripB", conn);
            cmd.CommandType = CommandType.StoredProcedure;
            conn.Open();
            ddlTripTypeB.DataSource = cmd.ExecuteReader();
            ddlTripTypeB.DataTextField = "TripType";
            ddlTripTypeB.DataValueField = "TripTypeBID";
            ddlTripTypeB.DataBind();
        }
+1  A: 

You should keep connections open as short a time as possible. Thus, you want to open a connection, execute a query or stored procedure and then close the connection. Although this sounds expensive, it leverages ADO.NET's built-in connection pooling. When you close a connection, it is returned to a pool and reused so you do not suffer a performance hit.

Thomas
point is moot, if all operations against the connection happen in the same method.
Mitch Wheat
@Mitch Wheat - Is it the same method? That isn't clear from the OP.
Thomas
@Mitch Wheat - There is also another issue of intermingling data access code with presentation code...
Thomas
@Thomas: not sure why you mention that? I would never suggest anyone do that.
Mitch Wheat
all within the same method. setting my controls
Spooks
@Mitch Wheat - I did not mean to infer that you were (if you thought that, my apologies). Just stating out loud that if the data access code were encapsulated, the entire issue would be moot. An outer method would be calling something akin to "GetDataTable("spSelectAllTripB", myparam)" for each dropdown list and the thought of calling it multiple times within the same method wouldn't come up.
Thomas
@Mitch Wheat - (That is the question of reusing the same connection or opening and close it multiple times would not come up).
Thomas
A: 

ADO.Net uses connection pooling, so that should lower the cost of opening new connections avoiding any need to have one connection open throughout your whole application.

However, there is probably still some overhead to shuffling the connections in the pool, so if you have code that is run sequentially and immediately, within the same portion of your code, then you are probably better off using a single connection for that short span. Check Adam's answer for a bit more on that - you want to get everything else set up first so that the connection is open for as short a time as possible.

If any ADO.Net programmers can confirm or correct this, please do.

Tom H.
Feel free to point out what's wrong with my answer when you down vote me so that, I don't know, maybe people can actually learn something
Tom H.
It wasn't me, but connection pooling in .NET happens automatically. Other than that, what you have said is pretty much correct.
Adam
Thanks. I'll alter my answer to reflect that.
Tom H.
A: 

A better way to do this would be to prep both of the commands and then open the connection and execute them both in quick succession:

conn.Open();
comm1.ExecuteReader();
comm2.ExecuteReader();

Always keep them open for as short a time as possible.

Adam
I will have to give this a try, seems pretty clean
Spooks
+2  A: 

.Net has connection pooling already managed for you so you don't need to worry about reusing connections like you might have with old asp. I always go with several small quick calls vs keeping one open the whole time because not all the time it's open is generally used for calls. You have your site code running doing some things between as well.

Now if you are going to make 6 consecutive calls one after another then it might make sense to open one and reuse. But other than that I say just stick with what you are doing.

The only thing you might want to look into is a connection manager so that you aren't having to create the connection object in .net over and over again. But that doesn't have anything to do with db connections vs just object creation.

spinon
have a good link for connection manager? for C#
Spooks
Agreed, if you are running multiple items in a "batch", do all the work you can, then close soon; be sure to not have the connection linger around. So, as much as possible ... open the db conn as late as possible, execute, close the db conn as early as possible, and try not to keep the connection open longer than is necessary.
SnapJag