当远程数据库中不存在数据行时,是否可以从本地数据库中选择数据行?
我写了一个代码,但是不起作用:conlocal
是本地数据库连接,而conremote
是远程数据库连接; Dim
命令为
sqlcommand = new sqlcommand ("select id from table", conlocal, "where id not in table",conremote)
答案 0 :(得分:0)
在线评论
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
'Get ID's from remote database
Dim RemoteDT As New DataTable
Using cn As New SqlConnection("Your remote connection string")
Using cmd As New SqlCommand("Select Id From [RemoteTableName];", cn)
cn.Open()
RemoteDT.Load(cmd.ExecuteReader)
End Using
End Using
'Put the Ids into an array
Dim IdsInRemote As Integer() = RemoteDT.AsEnumerable().[Select](Function(x) x.Field(Of Integer)("Id")).ToArray()
'Change the array to a single string that can be used in the Select statement
Dim Remotes As String = String.Join(",", IdsInRemote)
Dim LocalDT As New DataTable
Using cn As New SqlConnection("Your local connection string")
Using cmd As New SqlCommand($"Select Id From [LocalTableName] Where Id Not In ({Remotes});", cn)
cn.Open()
LocalDT.Load(cmd.ExecuteReader)
End Using
End Using
For Each row As DataRow In LocalDT.Rows
Debug.Print(row(0).ToString)
Next
End Sub
编辑 对于版本14(2015)之前的vb.net版本
Using cmd As New SqlCommand(String.Format("Select Id From [LocalTableName] Where Id Not In ({0});", Remotes), cn)