2012-01-29 7 views
0

基本的に私のコードは http://www.dreamincode.net/forums/topic/185244-using-sqldependency-to-monitor-sql-database-changes/複数のsqldependancy vb.netで可能ですか?

現在の状況は、私は2台を持っていますされ、ここで私は単純な一sqldependancyと他の同様のコードを複製監視する基づいていますが、それが失敗したと置き換えられます最新sqldependancyのように見えるています以前のsqldependancy関数ここ

は最終的に私はによりgetNamesを私のコード

Public Sub GetNames() 
     If Not DoesUserHavePermission() Then 
      Return 
     End If 


     lbQueue.Items.Clear() 

     ' You must stop the dependency before starting a new one. 
     ' You must start the dependency when creating a new one. 
     Dim connectionString As String = GetConnectionString() 
     SqlDependency.Stop(connectionString) 
     SqlDependency.Start(connectionString) 


     Using cn As SqlConnection = New SqlConnection(connectionString) 

      Using cmd As SqlCommand = cn.CreateCommand() 

       cmd.CommandType = CommandType.Text 
       cmd.CommandText = "SELECT PatientID FROM dbo.[patient_queue]" 

       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_onchange 

       cn.Open() 

       Using dr As SqlDataReader = cmd.ExecuteReader() 

        While dr.Read() 

         lbQueue.Items.Add(dr.GetInt32(0)) 
         doctor.lbqueue.items.add(dr.GetInt32(0)) 

        End While 

       End Using 

      End Using 

     End Using 
    End Sub 

    Private Sub dep_onchange(ByVal sender As System.Object, ByVal e As System.Data.SqlClient.SqlNotificationEventArgs) 

     ' this event is run asynchronously so you will need to invoke to run on the UI thread(if required) 
     If Me.InvokeRequired Then 

      lbQueue.BeginInvoke(New MethodInvoker(AddressOf GetNames)) 
      My.Computer.Audio.PlaySystemSound(Media.SystemSounds.Asterisk) 

     Else 

      GetNames() 

     End If 

     ' this will remove the event handler since the dependency is only for a single notification 
     Dim dep As SqlDependency = DirectCast(sender, SqlDependency) 
     RemoveHandler dep.OnChange, AddressOf dep_onchange 

    End Sub 


    Public Sub GetMedID() 
     If Not DoesUserHavePermission() Then 
      Return 
     End If 


     lbMedQueue.Items.Clear() 

     ' You must stop the dependency before starting a new one. 
     ' You must start the dependency when creating a new one. 
     Dim connectionString As String = GetConnectionString() 
     SqlDependency.Stop(connectionString) 
     SqlDependency.Start(connectionString) 


     Using cn As SqlConnection = New SqlConnection(connectionString) 

      Using cmd As SqlCommand = cn.CreateCommand() 

       cmd.CommandType = CommandType.Text 
       cmd.CommandText = "SELECT RecordID FROM dbo.[medicine_queue]" 

       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_onchange2 

       cn.Open() 

       Using dr As SqlDataReader = cmd.ExecuteReader() 

        While dr.Read() 

         lbMedQueue.Items.Add(dr.GetInt32(0)) 


        End While 

       End Using 

      End Using 

     End Using 
    End Sub 


    Private Sub dep_onchange2(ByVal sender As System.Object, ByVal e As System.Data.SqlClient.SqlNotificationEventArgs) 

     ' this event is run asynchronously so you will need to invoke to run on the UI thread(if required) 
     If Me.InvokeRequired Then 

      lbMedQueue.BeginInvoke(New MethodInvoker(AddressOf GetMedID)) 
      My.Computer.Audio.PlaySystemSound(Media.SystemSounds.Asterisk) 

     Else 

      GetMedID() 

     End If 

     ' this will remove the event handler since the dependency is only for a single notification 
     Dim dep As SqlDependency = DirectCast(sender, SqlDependency) 
     RemoveHandler dep.OnChange, AddressOf dep_onchange2 

    End Sub 

と呼ばれ、GetMedIDは負荷フォームに、それだけでGetMedIDが機能しているとonChangedイベントときによりgetNamesがイベントを発火しない、うまく働きました。

答えて

1

ここでの主な問題は、.Stopを呼び出してから.Startをデータアクセスメソッドごとに呼び出すことで、メソッドにアクセスするたびに依存関係を取り消して再起動することです。

アプリケーション起動時に.Startに一度だけ電話する必要があります。終了時には.Stopと同様に電話する必要があります。

たとえば、Webアプリケーションでは、これに最も適したのはGlobal.asax Application_StartApplication_Endです。

1

あなたは正しいと思いますが、私は同じ問題に遭遇しました。 SqlDependency.Start(connectionString)への2回目の呼び出しは、新しいSqlDependency(cmd)の後であっても、既存の初期のデフォルトのService Brokerサービスとキューを置き換えました。

サービスブローカはサービスおよびキュー名の一部としてGUIDを使用して各開始時にデフォルトのサービスとキューを作成します。サービス{GUID}とキュー{GUID} - ただし、1つのデファクトサービス/利用可能

最初の開始直後と2回目の開始直後にブレークポイントを設定することで、これを確認できます。 SQL Serverに移動し、dBaseに移動して Service Broker/ServicesとService Broker/Queuesフォルダを確認します。サービスとキューのフォルダを右クリックして、2番目のブレークポイント後に更新を選択する必要があります

関連する問題