简体   繁体   中英

Safest DB connection in c# ASP.NET

I'm refactoring some old code as we're periodically having connection pool issues on a clients site.

While some code has wrapped DB connections in try/catch blocks I'll be using the using(){} thing (is there a proper term for this?)

So I'd like some clarification on how to use it in our situation to make as few changes as possible.

What we currently have is (mostly) something like this:

SqlConnection sqlConn = new SqlConnection();
SqlCommand sqlCmd = new SqlCommand();

try
{
  // DB stuff here
}
finally
{
  sqlCmd.Dispose();
  sqlConn.Dispose();
}

Some connections aren't wrapped up at all.

So, I'm thinking of changing it to this:

using (SqlConnection ThisConnection = new SqlConnection(ConnectionString))
{
  using (SqlCommand sqlCmd = new SqlCommand())
  {
    sqlCmd.Connection = ThisConnection;
    // DB stuff here
  }
}

I've tried this and it works. But I know there's a difference between something working and something working well :)

So, a few questions:

  1. If the SqlConnection is assigned to SqlCommand.Connection and I only wrap up the SqlCommand in a using, will the connection be closed and disposed then the SqlCommand is?
  2. If not, is the above code ok? Or should the SqlCommand using be before the SqlConnection or does it not matter?
  3. When the object is disposed at the end of the using is this absolutely guaranteed to close the DB connection?

All feedback greatly appreciated, cheers.

The using approach is preferable; much harder to get it wrong and leave scenarios where objects can escape without being disposed. Unless you have a very specific reason , it should be your default. You can reduce the nesting, though:

using (SqlConnection ThisConnection = new SqlConnection(ConnectionString))
using (SqlCommand sqlCmd = new SqlCommand())
{
    sqlCmd.Connection = ThisConnection;
    // DB stuff here
}

When the object is disposed at the end of the using is this absolutely guaranteed to close the DB connection?

It will dispose it; the underlying connection (via any of these approaches) will usually still be pooled, though.

Of course, I also submit that the easiest way to do this is to use less code - less to get wrong. For example, I'd be very tempted to use "dapper", which avoids the need to mess with the commands, readers, parameter collections, etc:

int id = 123;
string name = "abc";
using (var conn = new SqlConnection(ConnectionString))
{
    conn.Execute("Some TSQL", new { id, name });
}
  1. if you dispose your connection, you need dispose your command, calling Dispose on it will supress the call to the finalizer. Add using command after using conenction

  2. Don't set your command before connection

  3. Yes, because you close your not managed object , who is coonnection

Remark : using bloc is preferred that try catch finally for basic need without custom treatment in catch bloc

msdn sample link : http://msdn.microsoft.com/fr-fr/library/system.data.sqlclient.sqlconnection(v=vs.110).aspx this article don't treat using command but speak about connection

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM