Sunday, November 14, 2010

Closing connection in ADO.net

When using DataReaders, specify CommandBehavior.CloseConnection.
● Do not explicitly open a connection if you use Fill or Update for a single
operation.
● Avoid checking the State property of OleDbConnection.


Use the using statement, instead of Dispose or Close, when you are working with
a single type, and you are coding in Visual C#. Dispose is automatically called for
you when you use the using statement, even when an exception occurs.

Dispose internally calls Close. In addition, Dispose clears the connection string.


using (SqlConnection conn = new SqlConnection(connString))
{
conn.Open();
. . .
} // Dispose is automatically called on the conn variable here.

No comments:

Post a Comment