自行编码合并外部数据
我们知道,用Merger可以合并外部数据到表中,不过Merger没有排除重复内容的选项,有时不能满足我们的要求。
例如项目中有一个员工表,要从一个外部文件中合并数据到这个员工表中,希望在合并过程中检查身份证号码,如果员工表已经存在相同身份证号码的行,则跳过此行。
假定被合并的表和员工表的结构相同,代码如下:
Dim
cmd
As New SQLCommand
Dim dt As
DataTable
cmd.ConnectionName = "数据源名称"
cmd.CommandText = "SELECT * From {员工}"
dt = cmd.ExecuteReader()
For Each
dr As
DataRow
In dt.DataRows
If
DataTables("员工").Find("身份证号码
= '" & dr("身份证号码")
& "'") Is
Nothing
Then
Dim nr
As
DataRow =
DataTables("员工").AddNew()
For
Each dc
As
DataCol
In dt.DataCols
nr(dc.name) =
dr(dc.name)
Next
End
If
Next
可见要自己设计一个功能更强的Merger,其实是件很简单的事情。
如果两个表的结构不同,可以参考下面的代码:
Dim
cmd
As New
SQLCommand
Dim dt
As DataTable
Dim sCols()
As String
= {"列名1",
"列名2",
"列名3"}
Dim
dCols()
As
String =
{"列名A",
"列名B", "列名C"}
cmd.ConnectionName
= "数据源名称"
cmd.CommandText
= "SELECT * From {员工}"
dt =
cmd.ExecuteReader()
For Each dr
As DataRow
In dt.DataRows
Dim nr
As DataRow
= DataTables("员工").AddNew()
For i
As Integer
=0
To sCols.Length
-1
nr(dCols(i))
= dr(sCols(i))
Next
Next
本页地址:http://www.foxtable.com/webhelp/topics/2137.htm