aimlion11yahoo
Instructor
Below is a small piece of code that connects to a database, puts the data into a DataReader, loops through the DataReader, then attemps to close the connection.
I run the code, everything works.
After the code has executed, and the webpage is still up, I run the following query in SQL Query Analyzer. The connection is still open?!
I run the code, everything works.
Code:
Dim myConnectionString As String = ConfigurationSettings.AppSettings("ConnectionString_BillyBob")
Dim mySelectQuery As String = "Select OrderGID from ORDERS"
Dim myConnection As New SqlClient.SqlConnection(myConnectionString)
Dim myCommand As New SqlClient.SqlCommand(mySelectQuery, myConnection)
Dim tempValue As Integer
myConnection.Open()
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Implicitly closes the connection because CommandBehavior.CloseConnection is specified.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Dim dr As SqlClient.SqlDataReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection)
Do While dr.Read
For tempValue = 0 To dr.FieldCount - 1
Debug.WriteLine(dr.Item(tempValue).ToString)
Next
Loop
dr.Close()
dr = Nothing
myConnection.Close()
myConnection.Dispose()
myConnection = Nothing
myCommand.Dispose()
myCommand = Nothing
After the code has executed, and the webpage is still up, I run the following query in SQL Query Analyzer. The connection is still open?!
Code:
SELECT spid, uid=rtrim(loginame), Program_name=rtrim(Program_name),
dbname=db_name(dbid), status=rtrim(status) FROM master.dbo.sysprocesses
WHERE loginame = 'Adam' and db_name(dbid)='ASP';