CommandBehavior - A Better Way to Close a Connection« View all ASP.net articles
March 20, 2007
In our previous topic Quickly Retrieve Data Using the DataReader we looked at a simple example on how to use DataReader with the ExecuteReader() method. We closed our connection after the loop. Using the CommandBehavior we do not have to explicitly close the connection, it will close the connection for you as soon as you close the DataReader.
Our code
Dim reader As SqlDataReader =
cmd.ExecuteReader()
'code....
reader.Close()
con.Close()
New code using the CommandBehavior.CloseConnection
Dim reader As SqlDataReader =
cmd.ExecuteReader(CommandBehavior.CloseConnection)
'code....
reader.Close()
'con.Close() - this is no longer needed
|