当远程数据库中不存在时如何 select 来自本地数据库的数据
How to select data from local database when it is not present in remote database
当远程数据库中不存在这些行时,是否可以 select 来自本地数据库的数据行?
我写了一段代码,但是不行:conlocal
是本地数据库连接,conremote
是远程数据库连接; Dim
命令为
sqlcommand = new sqlcommand ("select id from table", conlocal, "where id not in table",conremote)
在线评论
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)
编辑
RemoteDT.AsEnumerable().[Select](Function(x) x.Field(Of Integer)("Id")).ToArray()
的解释
.AsEnumerable
是 DataTable
的一种方法,returns 是 DataRow
.
的可枚举
Select 将从可枚举中选择符合我们提供的条件的行。在这种情况下,我们提供了一个带有参数 x 的函数。
x 指的是 IEnumerable
的元素,在本例中是 DataRow
。您可以随意命名。该变量只能在这个内联函数中看到,所以一个简单的名字就可以了。一个描述性的名称也可以,但因为它只在行内显示,所以真的没有必要。
因此该函数检查可枚举(每个数据行)中的每个 x 并且 returns 仅检查整数字段中称为 "Id"
的值
这里是我的错误!
最终将可枚举转换为数组。我忘记在要转换为数组的可枚举对象周围添加括号。
(RemoteDT.AsEnumerable().[Select](Function(x) x.Field(Of Integer)("Id"))).ToArray()
编辑
因为我现在发现 Id 是一个字符串
'Put the Ids into an array
'Change the datatype of the array and the field to String
Dim IdsInRemote As String() = (RemoteDT.AsEnumerable().[Select](Function(x) x.Field(Of String)("Id"))).ToArray()
'Change the array to a single string that can be used in the Select statement
'Add single quotes to the .Join so Sql Server will interpret as String
Dim Remotes As String = String.Join("','", IdsInRemote)
'Put a single quote on the beginning and end of the Remotes string
'The Join does not do this
Remotes = "'" & Remotes & "'"
'You can inspect the string in the immediate window
Debug.Print(Remotes)
当远程数据库中不存在这些行时,是否可以 select 来自本地数据库的数据行?
我写了一段代码,但是不行:conlocal
是本地数据库连接,conremote
是远程数据库连接; Dim
命令为
sqlcommand = new sqlcommand ("select id from table", conlocal, "where id not in table",conremote)
在线评论
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)
编辑
RemoteDT.AsEnumerable().[Select](Function(x) x.Field(Of Integer)("Id")).ToArray()
.AsEnumerable
是 DataTable
的一种方法,returns 是 DataRow
.
Select 将从可枚举中选择符合我们提供的条件的行。在这种情况下,我们提供了一个带有参数 x 的函数。
x 指的是 IEnumerable
的元素,在本例中是 DataRow
。您可以随意命名。该变量只能在这个内联函数中看到,所以一个简单的名字就可以了。一个描述性的名称也可以,但因为它只在行内显示,所以真的没有必要。
因此该函数检查可枚举(每个数据行)中的每个 x 并且 returns 仅检查整数字段中称为 "Id"
的值这里是我的错误!
最终将可枚举转换为数组。我忘记在要转换为数组的可枚举对象周围添加括号。
(RemoteDT.AsEnumerable().[Select](Function(x) x.Field(Of Integer)("Id"))).ToArray()
编辑
因为我现在发现 Id 是一个字符串
'Put the Ids into an array
'Change the datatype of the array and the field to String
Dim IdsInRemote As String() = (RemoteDT.AsEnumerable().[Select](Function(x) x.Field(Of String)("Id"))).ToArray()
'Change the array to a single string that can be used in the Select statement
'Add single quotes to the .Join so Sql Server will interpret as String
Dim Remotes As String = String.Join("','", IdsInRemote)
'Put a single quote on the beginning and end of the Remotes string
'The Join does not do this
Remotes = "'" & Remotes & "'"
'You can inspect the string in the immediate window
Debug.Print(Remotes)