C# VB.net 语法转换建议
我正在尝试在我的 vb.net 应用程序中实现 sqldependency:
I'm trying to implement sqldependency in my vb.net application as per:
http://www.dreamincode.net/forums/topic/156991-using-sqldependency-to-monitor-sql-database-changes/
我在调用代码时遇到问题,当收到通知时,应用程序锁定且没有错误.(当通过委托调用 MyOnChanged 时)
I'm having problems with the involking code, when a notification is recieved the application locks up with no errors. (When MyOnChanged is called via the deligate)
我是否在 C# > VB.net 转换中犯了错误?
Have I made a mistake in my C# > VB.net conversion?
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
推荐答案
按照 Merlyn 的建议,我使用 .NET 反射器来计算正确的 .net 转换:
As Merlyn recommended, I used .NET reflector to work out the proper .net conversion:
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
相关文章