Here is some code that I got working for a demo:
Option Strict On
Imports System.Data
Imports System.Data.SqlClient
Public Class Form1
Private mintChangeCount As Integer
Private mds As New DataSet
Private Delegate Sub UICallback()
Private Sub btnGetData_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnGetData.Click
GetData()
lblInfo.Text = "Aucun changement"
mintChangeCount = 0
btnGetData.Enabled = False
txtSQL.Enabled = False
End Sub
Private Sub GetData()
Dim cn As SqlConnection = Nothing
Try
mds.Clear()
cn = New SqlConnection("Data Source=.\sql2005; " & _
"Integrated Security = true; " & _
"Initial Catalog = AdventureWorks")
'cn = New SqlConnection("Data Source=.\sql2005; " & _
' "Integrated Security = true; " & _
' "Initial Catalog = HedgeFund_PRD")
Dim dad As New SqlDataAdapter(txtSQL.Text, cn)
Dim dependency As New SqlDependency(dad.SelectCommand)
AddHandler dependency.OnChange, AddressOf Me.DataChanged
SqlDependency.Start(cn.ConnectionString)
dad.Fill(mds, "Data")
grdDemo.DataSource = mds
grdDemo.DataMember = "Data"
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
Private Sub DataChanged(ByVal sender As Object, ByVal e As
SqlNotificationEventArgs)
mintChangeCount += 1
Me.Invoke(New UICallback(AddressOf RebindOnUIThread))
End Sub
Private Sub RebindOnUIThread()
GetData()
lblInfo.Text = String.Format("{0} changements.", mintChangeCount)
End Sub
'Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Handles Me.Load
' txtSQL.Text = "SELECT MAJDate FROM dbo.DatabaseEvent WHERE Event =
'BloombergUpdate'"
'End Sub
End Class
--
HTH
Éric Moreau, MCSD, Visual Developer - Visual Basic MVP
Conseiller Principal / Senior Consultant
Moer inc. (
http://www.emoreau.com/)