C#VB.net语法转换建议

时间:2010-08-10 22:12:42

标签: sql-server vb.net

我正在尝试按照以下方式在我的vb.net应用程序中实现sqldependency:

http://www.dreamincode.net/forums/topic/156991-using-sqldependency-to-monitor-sql-database-changes/

我遇到了使用代码的问题,当收到通知时,应用程序会锁定而没有错误。 (当通过deligate调用MyOnChanged时)

我在C#>中犯了错误吗? VB.net转换?

  Private Sub GetNames()
    Dim connectionString As String = "String..."
    ListView1.Items.Clear()

    SqlDependency.[Stop](connectionString)
    SqlDependency.Start(connectionString)

    Using cn As New SqlConnection(connectionString)
        Using cmd As SqlCommand = cn.CreateCommand()
            cmd.CommandType = CommandType.Text
            cmd.CommandText = "SELECT [finish_time], [file_name] FROM dbo.[log_table];"

            cmd.Notification = Nothing

            Dim dep As New SqlDependency(cmd)

            AddHandler dep.OnChange, New OnChangeEventHandler(AddressOf MyOnChanged)

            cn.Open()

            Using dr As SqlDataReader = cmd.ExecuteReader()
                While dr.Read()

                    Dim ls As New ListViewItem(dr.Item("finish_time").ToString.Trim())
                    ls.SubItems.Add(dr.Item("file_name").ToString.Trim())
                    ListView1.Items.Add(ls)

                End While
            End Using
        End Using
    End Using
End Sub




Private Sub MyOnChanged(ByVal sender As Object, ByVal e As SqlNotificationEventArgs)
    If ListView1.InvokeRequired Then
        ListView1.Invoke(New MethodInvoker(AddressOf GetNames))
    Else
        GetNames()
    End If
    Dim dep As SqlDependency = TryCast(sender, SqlDependency)
    AddHandler dep.OnChange, New OnChangeEventHandler(AddressOf MyOnChanged)
End Sub

2 个答案:

答案 0 :(得分:0)

正如Merlyn推荐的那样,我使用.NET反射器来计算正确的.net转换:

Private Sub MyOnChanged(ByVal sender As Object, ByVal e As SqlNotificationEventArgs)
    If MyBase.InvokeRequired Then
        Me.LVFTPXfers.BeginInvoke(New MethodInvoker(AddressOf Me.GetNames))
    Else
        Me.GetNames()
    End If
    Dim dependency As SqlDependency = TryCast(sender, SqlDependency)
    RemoveHandler dependency.OnChange, New OnChangeEventHandler(AddressOf Me.MyOnChanged)
End Sub

答案 1 :(得分:0)

您可能想尝试使用SharpDevelop中的代码转换工具进行Mono项目:

“......它包含的功能...... C#和VB.NET之间的代码转换 ......”

alt text http://www.softpedia.com/screenshots/SharpDevelop_5.png