Re: Using SqlConnection



On Aug 21, 5:12 am, "Tony Johansson" <johansson.anders...@xxxxxxxxx>
wrote:
Hello!

Just a simple question is it good to always close a sqlConnection even
if the Connection has been closed by the DataAdapter ?

//Tony

Hi,

Yes, an ever better approach is to dispose it, like:
using(SqlConnection c = new SqlConnection(...)){
// use the connection
}
in this case you do not have to close it explicitely.
Not only that, but if there is an unhandled exception inside it will
close it too.
.



Relevant Pages

  • Re: scalability of single SqlConnection instance
    ... Do you mean lock on the specific SqlConnection instance being used for the operation? ... Yes, you can consider a new "feature" that can permit multiple operations on a single connection, but it has so many side-effects, I don't recommend it. ... I think that technically my architecture is thread-safe (since only a single thread can make use of my single SqlConnection instance at once), but it's hardly a desirable way to do things. ...
    (microsoft.public.dotnet.framework.adonet)
  • Re: scalability of single SqlConnection instance
    ... Do you mean lock on the specific SqlConnection instance being used for the operation? ... another connection when needed. ... I think that technically my architecture is thread-safe (since only a single thread can make use of my single SqlConnection instance at once), but it's hardly a desirable way to do things. ... there is a connection pool at client side for SQL Server. ...
    (microsoft.public.dotnet.framework.adonet)
  • Re: Data access layer + sql connections
    ... > would be interested in hearing opinions from more experienced .NET ... > I am building a data access layer that encapsulates my business entities. ... > SqlConnection and issues the relevant SQL commands. ... and this transaction object should contain the connection. ...
    (microsoft.public.dotnet.framework.adonet)
  • Re: Very strange SQL connection problem
    ... Try creating a *new instance* of SqlConnection and then open and after the ... > connection string again and got the second exception after ... > Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior ...
    (microsoft.public.dotnet.framework.adonet)
  • Developing asp.net with VS.NET 2003
    ... I made a base class for data that retrieved the connect string. ... a new SQLConnection with the connection string. ...
    (microsoft.public.vsnet.general)