Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best practice? open and close multi connections, or one large open connection for ado.net

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();
}
like image 394
Spooks Avatar asked Jul 27 '10 15:07

Spooks


People also ask

Why is it important to close an ADO.NET application?

Q #3) Why is it important to close an ADO.NET application? Answer: Connections need to be closed properly because it affects the scalability and reliability of the applications. Open connections are always vulnerable to attack, so to be short, 'Open connections as late as possible and close it as early as possible'.

Is ADO.NET outdated?

It isn't obsolete, it is the foundation for working with databases in . NET.

How many connections SQL Server can handle?

By default, SQL Server allows a maximum of 32767 concurrent connections which is the maximum number of users that can simultaneously log in to the SQL server instance.

Why do we need connection pooling?

Using connection pools helps to both alleviate connection management overhead and decrease development tasks for data access. Each time an application attempts to access a backend store (such as a database), it requires resources to create, maintain, and release a connection to that datastore.


1 Answers

.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.

like image 89
spinon Avatar answered Oct 16 '22 03:10

spinon