开发者

Unusually long pause trying to close ADO.NET connection object: is this normal?

开发者 https://www.devze.com 2023-04-13 06:17 出处:网络
I am performing a simple exercise of opening an SQL Server database connection, pulling the first record of a table from a DataReader object, and then closing the object.However, I have noticed that t

I am performing a simple exercise of opening an SQL Server database connection, pulling the first record of a table from a DataReader object, and then closing the object. However, I have noticed that theres bit of a delay, about 5 seconds or so, in closing the connection. However, the delay only occurs after the command object executes the specified query. I've worked in a setup like this before and 开发者_如何学Cdon't remember there being such a long delay while closing the connection.

   Public Sub TestDb()
    Dim cnStrBuilder As New SqlClient.SqlConnectionStringBuilder
    Dim cn As New SqlClient.SqlConnection
    Dim sqlSelectName As New SqlClient.SqlCommand
    Dim drName As SqlClient.SqlDataReader
    Dim newName As New SymName
    Dim i As Integer

    cnStrBuilder.UserID = "sa"
    cnStrBuilder.ConnectTimeout = 30
    cnStrBuilder.Password = ""
    cnStrBuilder.PersistSecurityInfo = True
    cnStrBuilder.DataSource = "EMARKET\FL_DB"
    cnStrBuilder.InitialCatalog = "EmailMarketing"

    sqlSelectName.CommandType = CommandType.Text
    sqlSelectName.CommandText = "SELECT * FROM [NAME]"

    System.Console.WriteLine(cnStrBuilder.ConnectionString)

    cn.ConnectionString = cnStrBuilder.ConnectionString

    Try
        If cn.State = ConnectionState.Closed Then
            cn.Open()
        End If
        System.Console.WriteLine("Connection success")

        sqlSelectName.Connection = cn

        System.Console.WriteLine("Execute Reader")
        drName = sqlSelectName.ExecuteReader

        If drName.HasRows = True Then
            System.Console.WriteLine("Read Row")
            drName.Read()

            For i = 0 To drName.FieldCount - 1
                Console.WriteLine(drName.Item(i).ToString)
            Next
        End If

        System.Console.WriteLine("Closing connection")

        sqlSelectName.Connection.Close()

    Catch ex As Exception
        System.Console.WriteLine("Something Happened")
        System.Console.WriteLine(ex.Message)
    End Try

System.Console.WriteLine("Done.")

End Sub

If I omit the lines

'System.Console.WriteLine("Execute Reader")
'drName = sqlSelectName.ExecuteReader
'
'If drName.HasRows = True Then
'    System.Console.WriteLine("Read Row")
'    drName.Read()
'
'    For i = 0 To drName.FieldCount - 1
'        Console.WriteLine(drName.Item(i).ToString)
'    Next
'End If

The connection closes almost imediately. What gives? I have narrowed it down to the where the .ExecuteReader line that causes the delay in the connection close. Whats causing the delay and how do I resolve it?


You're telling SQL Server to retrieve the entire table. Yet after the first row, you stop, and close the connection. Like you, I would expect the connection to close immediately, but perhaps the server is busy spooling the table to a place where it can return your data quickly.

Does the connection still close slowly if you only ask the server for one row? F.e.

 sqlSelectName.CommandText = "SELECT TOP 1 * FROM [NAME]"


Generally, you should be wrapping any objects that implement IDisposable in using statements which includes the connection object. I would try implementing something like the following which is from MSDN:

http://msdn.microsoft.com/en-us/library/y6wy5a0f.aspx#Y400

Public Sub CreateCommand(ByVal queryString As String, _
  ByVal connectionString As String)
    Using connection As New SqlConnection(connectionString)
        Dim command As New SqlCommand(queryString, connection)
        connection.Open()
        Dim reader As SqlDataReader = _
            command.ExecuteReader(CommandBehavior.CloseConnection)
        While reader.Read()
            Console.WriteLine("{0}", reader(0))
        End While
    End Using
End Sub
0

精彩评论

暂无评论...
验证码 换一张
取 消