将唯一记录从一个服务器插入另一个服

时间:2014-11-06 22:00:39

标签: sql-server datatable ado.net unique sql-insert

昨天我学会了如何使用ADO.NET获取DataTable并将所有记录批量插入SQL Server数据库。这对我项目的一部分非常有用。现在我需要做的是从DataTable获取记录并仅插入SQL服务器中尚不存在的记录。我使用的是VB2010,代码如下:

'get the data from our Teradata server and fill up a DataTable
Dim dtCheck As New DataTable("TableCheck")
dtCheck = GetDataTableFromTeradataServer

'connect to our SQL server
Dim connSqlSvr As New System.Data.SqlClient.SqlConnection
connSqlSvr.ConnectionString = "Data Source=DestSqlServer;Initial Catalog=DestDb;Connect Timeout=15"
connSqlSvr.Open() 

'how do I take records from the DataTable and insert only those records that do not
'already exist in the SQL server?

'close and dispose the SQL server database connection
connSqlSvr.Close()
connSqlSvr.Dispose()

SQL服务器目标表[DestDb]。[dbo]。[Events]包含字段

[CityCode],[CarNum],[VIN],[Fleet],[CarMileage],[EventItm],[EventDate]

并且内存中的DataTable具有字段

City,CarNo,VIN,Fleet,Mileage,EventDesc,EventDate

为了我的目的,一个独特的记录由[CityCode],[CarNum],[VIN],[EventItm]和[EventDate]

组成

通常情况下,我会每天运行一次这个例行程序,并从源头获取大约200条记录。一直在寻找几个小时,但不知道如何处理这个问题。

1 个答案:

答案 0 :(得分:0)

您可以尝试在目标服务器的临时表中加载数据。然后从那里做独特的插入。

相关问题