open the connection before each non-request or a single connection for the set?

advertisements

If I have about 2000 record and I make a multiple insert. Which method has better performance than the other?

  • connection with each single insert .and close after the insertion .
  • one connection for the whole bulk and close the connection at the end. and what about the connection timeout in this case.

Notes :

  • The database is informix db.

  • It takes about 3.5 to 4 minutes to insert about 6000 record.(with the first method)


Application connection pooling will largely make this question irrelevant as c# application pools are optimized for multiple calls to the same database.

That said, what I do conforms to the following rule:

If you can open a connection and then do multiple operations by use of the "using" syntax:

using (SqlConnection connection = new SqlConnection(connectionString))
{
    connection.Open();
    // Do work here; connection closed on following line.
}

Then keep the connection open and do all your operations.

If however, each record is manipulated and then saved in such a fashion that you have to open a new connection at each point, don't fret overmuch. You'll still perform great with connection pooling.

From MSDN:

http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnection.aspx

To deploy high-performance applications, you must use connection pooling. When you use the .NET Framework Data Provider for SQL Server, you do not have to enable connection pooling because the provider manages this automatically, although you can modify some settings. For more information, see SQL Server Connection Pooling (ADO.NET).

Even if your connection isn't the SQL one, the built in connection providers for the other sources behave in similar manner.