1

好的,我有 2 个查询正在运行 SQL Dependecy 来模拟推送 - 通知

真的是我自己的问题是为什么我的两段代码运行起来如此不同(更慢/更快)然后其他

这是快速代码(毫秒更新)

lbnoes.Text = ""
    'You must stop the dependency before starting a new one.
    'You must start the dependency when creating a new one.
    SqlDependency.Stop(getSQLString())
    SqlDependency.Start(getSQLString())
    Using cn As SqlConnection = New SqlConnection(getSQLString())
        Using cmd As SqlCommand = cn.CreateCommand()
            cmd.CommandType = CommandType.Text
            cmd.CommandText = "SELECT test1, test2 FROM dbo.[ztest]"
            cmd.Notification = Nothing
            ' creates a new dependency for the SqlCommand
            Dim dep As SqlDependency = New SqlDependency(cmd)
            ' creates an event handler for the notification of data changes in the database
            AddHandler dep.OnChange, AddressOf dep_onchange1
            cn.Open()
            Using dr As SqlDataReader = cmd.ExecuteReader()
                While dr.Read()
                    lbnoes.Text = lbnoes.Text & vbCrLf & (dr.GetString(0) & " " & dr.GetString(1))
                    'PopupNotifier1.ContentText = dr.GetString(0) & " " & dr.GetString(1)
                    'PopupNotifier1.Popup()
                End While
            End Using
        End Using
    End Using

这里是慢代码(几乎 2 分钟 /update 似乎)它几乎就像它一遍又一遍地调用 on 变化。- 希望你能告诉我为什么我宁愿使用这段代码

lbnoes.Text = ""
    Try

        Dim con As New SqlConnection
        Dim myConString As String = getSQLString()
        Dim objcommand As SqlCommand = New SqlCommand
        'con.ConnectionString = myConString

        With objcommand
            .Connection = con
            Dim cmdText As String = "SELECT test1, test2 FROM ztest"
            .CommandText = cmdText
        End With
        con.ConnectionString = myConString
        SqlDependency.Stop(getSQLString())
        SqlDependency.Start(getSQLString())
        Dim dep1 As SqlDependency = New SqlDependency(objcommand)
        AddHandler dep1.OnChange, AddressOf dep_onchange1
        con.Open()
        Using readerObj As SqlClient.SqlDataReader = objcommand.ExecuteReader
            'This will loop through all returned records 
            While readerObj.Read
                Dim t1 As String = readerObj("test1").ToString
                Dim t2 As String = readerObj("test2").ToString
                lbnoes.Text = lbnoes.Text & vbCrLf & (t1 & " " & t2)
                '                'PopupNotifier1.ContentText = dr.GetString(0) & " " & dr.GetString(1)
                '                'PopupNotifier1.Popup()
            End While
        End Using
        con.Close()
    Catch ex As Exception
    End Try

两个 on Changes 都以相同的方式被调用(不同的调用方法/removehandles)......

请帮助这个困惑的小块。

提前致谢

4

1 回答 1

2

好吧,如果不深入应用程序代码就不容易分辨,但是通过索引访问 datareader 成员比通过列名更有效。

另外,也许类型转换有关系..

于 2013-04-29T20:10:23.097 回答