VB错误:"已经有一个与此命令关联的数据加载器"

时间:2015-07-21 14:17:11

标签: sql vb.net servicestack

尝试运行ServiceStack服务时,我收到以下错误:

调试时,代码只运行一次而不会循环两次,我还在所有其他函数上放置了断点,并且没有一个被点击,因此设法将问题缩小到此一个功能。

Public Function GetVisitList(SiteKey As String) As List(Of VisitDetail) Implements IVisitorData.GetVisitList
        Dim vlcmd As SqlClient.SqlCommand = New SqlCommand
        vlcmd.CommandTimeout = 60

        Try
            vlcmd.Connection = Conn
            vlcmd.CommandType = CommandType.StoredProcedure
            vlcmd.CommandText = "GetVisitList"

            vlcmd.Parameters.AddWithValue("@sitekey", SiteKey)

            Dim dr As SqlDataReader = vlcmd.ExecuteReader()

            Dim visitList As New List(Of VisitDetail)

            While dr.Read()
                Dim visit As New VisitDetail

                If Not IsDBNull(dr("VKey")) Then
                    visit.VisitorKey = dr("VKey")
                End If

                If Not IsDBNull(dr("VisitIP")) Then
                    visit.IP = dr("VisitIP")
                End If

                If Not IsDBNull(dr("SiteKey")) Then
                    visit.SiteKey = dr("SiteKey")
                End If

                If Not IsDBNull(dr("Alert")) Then
                    visit.AlertDescription = dr("Alert")
                End If

                If Not IsDBNull(dr("AlertNo")) Then
                    visit.AlertNumber = dr("AlertNo")
                End If

                If Not IsDBNull(dr("VisitNo")) Then
                    visit.VisitNumber = dr("VisitNo")
                Else
                    visit.VisitNumber = 0
                End If

                If Not IsDBNull(dr("Invited")) Then
                    visit.Invited = dr("Invited")
                End If

                If Not IsDBNull(dr("Chatted")) Then
                    visit.Chatted = dr("Chatted")
                End If

                If Not IsDBNull(dr("Prospect")) Then
                    visit.Prospect = dr("Prospect")
                End If

                If Not IsDBNull(dr("Customer")) Then
                    visit.Customer = dr("Customer")
                End If

                If Not IsDBNull(dr("HackRaised")) Then
                    visit.Hacker = dr("HackRaised")
                End If

                If Not IsDBNull(dr("Spider")) Then
                    visit.Spider = dr("Spider")
                End If

                If Not IsDBNull(dr("Cost")) Then
                    visit.ThisVisitCost = dr("Cost")
                End If

                If Not IsDBNull(dr("Revenue")) Then
                    visit.ThisVisitRevenue = dr("Revenue")
                End If

                If Not IsDBNull(dr("Visits")) Then
                    visit.Visits = dr("Visits")
                Else
                    visit.Visits = 0
                End If

                If Not IsDBNull(dr("FirstDate")) Then
                    visit.FirstVisitDate = dr("FirstDate")
                End If

                If Not IsDBNull(dr("TotalCost")) Then
                    visit.TotalCost = dr("TotalCost")
                End If

                If Not IsDBNull(dr("TotalRevenue")) Then
                    visit.TotalRevenue = dr("TotalRevenue")
                End If

                If Not IsDBNull(dr("OperatingSystem")) Then
                    visit.OperatingSystem = dr("OperatingSystem")
                End If

                If Not IsDBNull(dr("Browser")) Then
                    visit.Browser = dr("Browser")
                End If

                If Not IsDBNull(dr("SearchEngine")) Then
                    visit.SearchEngine = dr("SearchEngine")
                End If

                If Not IsDBNull(dr("Referrer")) Then
                    visit.Referrer = dr("Referrer")
                End If

                If Not IsDBNull(dr("Keywords")) Then
                    visit.Keywords = dr("Keywords")
                End If

                If Not IsDBNull(dr("ReferrerQuery")) Then
                    visit.ReferrerQuery = dr("ReferrerQuery")
                End If

                If Not IsDBNull(dr("Name")) Then
                    visit.ContactName = dr("Name")
                End If

                If Not IsDBNull(dr("Email")) Then
                    visit.ContactEmail = dr("Email")
                End If

                If Not IsDBNull(dr("Company")) Then
                    visit.ContactCompany = dr("Company")
                End If

                If Not IsDBNull(dr("Telephone")) Then
                    visit.ContactTelephone = dr("Telephone")
                End If

                If Not IsDBNull(dr("Fax")) Then
                    visit.ContactFax = dr("Fax")
                End If

                If Not IsDBNull(dr("Street")) Then
                    visit.ContactStreet = dr("Street")
                End If

                If Not IsDBNull(dr("City")) Then
                    visit.ContactCity = dr("City")
                    visit.City = dr("City")
                End If

                If Not IsDBNull(dr("Zip")) Then
                    visit.ContactZip = dr("Zip")
                End If

                If Not IsDBNull(dr("Country")) Then
                    visit.ContactCountry = dr("Country")
                    visit.Country = dr("Country")
                End If

                If Not IsDBNull(dr("Web")) Then
                    visit.ContactWebSite = dr("Web")
                End If

                If Not IsDBNull(dr("Organization")) Then
                    visit.Organization = dr("Organization")
                End If

                If Not IsDBNull(dr("CRMID")) Then
                    visit.CrmID = dr("CRMID")
                End If

                If Not IsDBNull(dr("Notes")) Then
                    visit.ContactNotes = dr("Notes")
                End If

                If Not IsDBNull(dr("DNS")) Then
                    visit.DNS = dr("DNS")
                End If

                If Not IsDBNull(dr("Region")) Then
                    visit.Region = dr("Region")
                End If

                If Not IsDBNull(dr("FirstAlert")) Then
                    visit.FirstAlertDescription = dr("FirstAlert")
                End If

                If Not IsDBNull(dr("FirstVisitReferrer")) Then
                    visit.FirstReferrer = dr("FirstVisitReferrer")
                End If

                If Not IsDBNull(dr("ProspectTypes")) Then
                    visit.ProspectTypes = dr("ProspectTypes")
                End If

                If Not IsDBNull(dr("VisitDate")) Then
                    visit.SessionStarted = dr("VisitDate")
                End If

                If Not IsDBNull(dr("SecondsOnSite")) Then
                    visit.TimeOnSite = dr("SecondsOnSite")
                End If

                If Not IsDBNull(dr("Page")) Then
                    visit.Page = dr("Page")
                End If

                visitList.Add(visit)

                dr.Close()
                Conn.Close()
            End While

            Return visitList

        Catch ex As Exception
            Throw ex
        End Try

    End Function

关联的连接初始化为:

Public Sub Init(connectionString As String) Implements IVisitorData.Init
        connString = connectionString
        Conn = New SqlConnection(connectionString)
        Conn.Close()
        If Conn.State = ConnectionState.Closed Then
            Conn.Open()
        End If
    End Sub

到目前为止,我已尝试过:

  • 关闭连接
  • 处理命令
  • 关闭datareader
  • 部署检查以仅在其当前状态关闭时打开数据库连接

这些都没有奏效。有没有人知道这里会发生什么?据我所知,datareader只打开一次然后关闭,但必须有一些我不知道的东西。

3 个答案:

答案 0 :(得分:4)

  dr.Close()
  Conn.Close()
End While

这些应该在while循环之外。您在关闭连接后尝试阅读。

End While
  dr.Close()
  Conn.Close()

答案 1 :(得分:2)

我会将连接,命令和datareader放入使用语句MSDN。它确保在使用块中完成代码时处理资源。所以这是我将使用的代码:

using con as new sqlconnection("connection string")
     con.open()
     using command as new sqlcommand("sql string", con)
          'Your command code...
          using rdr as sqldatareader = command.executereader()
               'Your reader code...

          end using

     end using
end using

我认为这是确保处置对象的最佳方法。

HTH

瓦德

答案 2 :(得分:1)

除了Kash的回答之外,你最好初始化SqlConnection类的本地实例,这样它就是你的函数的内部实例。默认实现无论如何都使用了背景上的连接池(link),因此性能方面没有任何区别。

Using语句也非常有用。它提供的优点是连接在关闭时自动关闭和自动处理。它也会避免你当前的问题。

Using conn as new SqlConnection()
    conn.Open()
    ...
End Using