Do I Need To Close SQL Connection
-
here is the portion of source code to retrieve data from database --------------------------- Dim dr as SQLDataReader Dim cmd as SQLCommand Dim cnn as SQLConnection cnn=new SQLConnection(ConfigurationSettings.AppSettings("ConnectionString")) cnn.open() cmd=new SQLCommand(sqlstr,cnn) dr=cmd.ExecuteReader() do while dr.read() hp=dr("handphone") loop dr.close() --------------------------- i was wondering do i need to close the SQLConnection cnn ?or when i close SQLDataReader dr, cnn will automatically be closed? thank you. regards yccheok
-
here is the portion of source code to retrieve data from database --------------------------- Dim dr as SQLDataReader Dim cmd as SQLCommand Dim cnn as SQLConnection cnn=new SQLConnection(ConfigurationSettings.AppSettings("ConnectionString")) cnn.open() cmd=new SQLCommand(sqlstr,cnn) dr=cmd.ExecuteReader() do while dr.read() hp=dr("handphone") loop dr.close() --------------------------- i was wondering do i need to close the SQLConnection cnn ?or when i close SQLDataReader dr, cnn will automatically be closed? thank you. regards yccheok
You should close the connection only after you are done with the data reader. Fortunately, there is a way to automatically close the connection when you close the data reader, that is by supplying the following argument in the ExecuteReader Function:- cmd.ExecuteReader(CommandBehavior.CloseConnection) Notorious SMC
The difference between the almost-right word & the right word is a really large matter - it's the difference between the lightning bug and the Lightning Mark Twain
Get your facts first, and then you can distort them as much as you please Mark Twain