使用vb.net从数据集或数据表中选择其他数据库中不存在的数据

时间:2019-05-21 18:42:19

标签: vb.net datatable dataset sqldataadapter

我想将数据从远程MSSQL数据库插入本地MSSQL数据库。但是应该有一个条件,只有那些数据将被选择插入,而这些数据在我的本地database.table中不存在。我可以从远程数据填充数据集或数据表,并更新本地数据表。但是在第一次成功插入后出现错误。错误与重复数据插入有关。因此,有什么方法可以从填充的数据集或数据表中仅选择本地数据库中不存在的那些数据?我的代码在下面。

Imports System.Data
Imports System.Data.Sql
Imports System.Data.SqlClient
Imports System.Data.DataTable
Imports System.Data.DataSet
Class MainWindow
Dim dsl As New DataSet()
Dim dal As New SqlDataAdapter()
Dim conr As String = ("Data Source=MOONLOADER\MSSQLSERVER2005;Initial Catalog=SSBYM;User ID=sa;Password=sasa;")
Dim cnr As New SqlConnection(conr)
Dim conl As String = ("Data Source=MOONLOADER\MSSQLSERVER2005;Initial Catalog=SSBY;User ID=sa;Password=sasa;")
Dim cnl As New SqlConnection(conl)
Private Sub Button_Click(sender As Object, e As RoutedEventArgs)
    Try
        'UPDATING URNLIST
        dal.SelectCommand = New SqlCommand("select URN from TxnEnrollment", cnr)
        dal.AcceptChangesDuringFill = False
        dal.Fill(dsl, "URNLIST")
        Dim dar As New SqlDataAdapter("select * from URNLIST", cnl)
        Dim U As SqlCommandBuilder = New SqlCommandBuilder(dar)
        dar.Update(dsl, "URNLIST")
        'UPLOADING ENROLLMENT DATA
        dal.SelectCommand = New SqlCommand("select * from TxnEnrollment where Personalized='1' and URN not in (SELECT URN FROM URNLIST)", cnl)
        dal.AcceptChangesDuringFill = False
        dal.Fill(dsl, "TxnEnrollment")
        Dim dar0 As New SqlDataAdapter("select * from TxnEnrollment where Personalized='0'", cnr)
        Dim U0 As SqlCommandBuilder = New SqlCommandBuilder(dar0)
        dar0.Update(dsl, "TxnEnrollment")
        'UPLOADING DEPENDENT DATA
        dal.SelectCommand = New SqlCommand("select * from TxnDependents where Enrolled='1' and URN not in (SELECT URN FROM URNLIST)", cnl)
        dal.AcceptChangesDuringFill = False
        dal.Fill(dsl, "TxnDependents")
        Dim dar1 As New SqlDataAdapter("select * from TxnDependents where Enrolled='0'", cnr)
        Dim U1 As SqlCommandBuilder = New SqlCommandBuilder(dar1)
        dar1.Update(dsl, "TxnDependents")
        cnl.Close()
        cnr.Close()
        MessageBox.Show("Uploaded Successfully")
    Catch ex As Exception
        MessageBox.Show(ex.Message.ToString())
    End Try
End Sub

End Class

0 个答案:

没有答案