Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
关系表上的SQL依赖关系_Sql_Vb.net_Dependencies - Fatal编程技术网

关系表上的SQL依赖关系

关系表上的SQL依赖关系,sql,vb.net,dependencies,Sql,Vb.net,Dependencies,我有两个相关的表(tbVehicles和tbVehiclesDoc),我正在运行下面的sql依赖项,但它运行了很多次,我的程序冻结了。请帮忙 我希望程序在其他用户修改这些表时更新它们。 我的代码: 公共子getdata() Try If tbdataset.Tables.Contains("tbVehicles") Then If tbdataset.Tables("tbVehiclesDoc") IsNot Nothing Then

我有两个相关的表(tbVehicles和tbVehiclesDoc),我正在运行下面的sql依赖项,但它运行了很多次,我的程序冻结了。请帮忙

我希望程序在其他用户修改这些表时更新它们。 我的代码:

公共子getdata()

    Try
        If tbdataset.Tables.Contains("tbVehicles") Then

            If tbdataset.Tables("tbVehiclesDoc") IsNot Nothing Then
                For f As Integer = tbdataset.Tables("tbVehiclesDoc").ChildRelations.Count - 1 To 0 Step -1
                    tbdataset.Tables("tbVehiclesDoc").ChildRelations(f).ChildTable.Constraints.Remove(tbdataset.Tables("tbVehiclesDoc").ChildRelations(f).RelationName)
                    tbdataset.Tables("tbVehiclesDoc").ChildRelations.RemoveAt(f)
                Next
                tbdataset.Tables("tbVehiclesDoc").ChildRelations.Clear()
                tbdataset.Tables("tbVehiclesDoc").ParentRelations.Clear()
                tbdataset.Tables("tbVehiclesDoc").Constraints.Clear()
                tbdataset.Tables.Remove("tbVehiclesDoc")
            End If
            tbdataset.Tables.Remove("tbVehicles")
            tbdataset.EnforceConstraints = True
        End If
        gridVehicles.DataSource = Nothing
    Catch ex As Exception
        MsgBox(ex.ToString)
        '   Exit Sub
    End Try
    command.Notification = Nothing
    Dim dependency As New SqlDependency(command)
    AddHandler dependency.OnChange, AddressOf dependency_OnChange

    Dim adapter As New SqlDataAdapter(command)
    adapter.Fill(tbdataset, "tbVehicles")
    '----
End Sub

Private Sub toupdate()
    If CanRequestNotifications() Then
        If connection Is Nothing Then
            connection = New SqlConnection(GetConnectionString())
            connection.Open()
        End If
        If connection.State = ConnectionState.Closed Then
            connection.Open()
        End If
        If command Is Nothing Then
            command = New SqlCommand(GETSQL(), connection)
        End If

    End If
    getdata()
End Sub

Private Function GETSQL() As String
    Return "Select Rtrim(VehcID) as VehcID,RTrim(VehcModel), Rtrim(VehcRegNo) as VehcRegNo, Rtrim(VehcFourWheel), Rtrim(VehcCondition), Rtrim(VehcLastKM),Rtrim(VehcBranch), Rtrim(VehcDepartment), Rtrim(VehcDriver), Rtrim(VehcRemarks), VehcPic from dbo.tbVehicles"
End Function

Private Sub dependency_OnChange(ByVal sender As Object, ByVal e As SqlNotificationEventArgs)

    ' This event will occur on a thread pool thread.
    ' It is illegal to update the UI from a worker thread
    ' The following code checks to see if it is safe
    ' update the UI.
    Dim i As ISynchronizeInvoke = CType(Me, ISynchronizeInvoke)

    ' If InvokeRequired returns True, the code
    ' is executing on a worker thread.
    If i.InvokeRequired Then
        ' Create a delegate to perform the thread switch
        Dim tempDelegate As New OnChangeEventHandler( _
            AddressOf dependency_OnChange)

        Dim args() As Object = {sender, e}

        ' Marshal the data from the worker thread
        ' to the UI thread.
        i.BeginInvoke(tempDelegate, args)

        Return
    End If

    ' Remove the handler since it's only good
    ' for a single notification
    Dim dependency As SqlDependency = _
        CType(sender, SqlDependency)

    RemoveHandler dependency.OnChange, _
       AddressOf dependency_OnChange

    ' Reload the dataset that's bound to the grid.
    getdata()
End Sub