2012-01-29 4 views
0

기본적으로 내 코드 http://www.dreamincode.net/forums/topic/185244-using-sqldependency-to-monitor-sql-database-changes/가능한 복수의 sqldependancy vb.net에서?

현재 상황은 내가 2 테이블을 데있다 여기에 내가 간단한 첫번째 sqldependancy와 다른 유사한 코드를 복제 모니터링 할을 기반으로하지만, 실패하고를 대체 할 최신 sqldependancy처럼 보일 것 이전 sqldependancy 함수. 여기

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 

마침내 내가 GetNames라는 광산의 코드, GetMedID 부하 양식에, 그냥 GetMedID가 작동하고는 onChanged 때 GetNames가 이벤트를 트리거하지 않습니다, 괜찮 았는데.

답변

1

여기서 주된 문제는 .Stop.Start을 각각의 데이터 액세스 메서드 내에서 호출하므로 메서드에 액세스 할 때마다 종속성이 취소되고 다시 시작된다는 것입니다.

응용 프로그램이 시작될 때 .Start 한 번만 호출하면됩니다. 끝나면 .Stop으로 끝납니다.

예를 들어 웹 응용 프로그램에서 가장 적합한 곳은 Global.asax Application_StartApplication_End입니다.

1

나는 네가 옳다고 생각하는데 나는 같은 문제에 부딪쳤다. SqlDependency.Start (connectionString)에 대한 두 번째 호출은 새 SqlDependency (cmd) 후에도 기존의 초기 기본 Service Broker 서비스와 큐를 대체합니다.

Service Broker는 GUID를 서비스 및 큐 이름의 일부로 사용하여 각 시작에 기본 서비스 및 큐를 만듭니다. 서비스 {GUID} 및 큐 {GUID} -하지만 서비스/큐 쌍 하나만 존재하는 것으로 보입니다 사용 가능

첫 번째 시작 직후와 두 번째 시작 직후에 중단 점을 넣음으로써이를 확인할 수 있습니다. SQL Server로 이동하여 dBase로 이동하여 Service Broker/Services 및 Service Broker/Queues 폴더를 확인합니다. 서비스 및 대기열 폴더를 마우스 오른쪽 단추로 클릭하고 두 번째 중단 점 이후 새로 고침을 선택해야합니다.