2014-11-06 111 views
0

昨天我学会了如何使用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 Server目标表[DestDb] [DBO] [活动]有田

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

,并在内存中的数据表中有场

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

对于我而言唯一的记录是由[CityCode],[CarNum],[VIN],[EventItm],以及[EVENTDATE]

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

+1

有您的问题[这里] [1] [1]一个很好的解决方案:http://stackoverflow.com/questions/16460397/sql-insert-into-table-only-if -record-doesnt-exist – 2014-11-06 23:33:41

+0

谢谢。我结束了使用INSERT INTO SELECT FROM WHERE HAVING方法,详见这里http://stackoverflow.com/questions/913841/mysql-conditional-insert。我循环通过每个记录在这里指出http://stackoverflow.com/questions/26940195/sql-server-parameterized-query-with-a-where-clause-with-nulls – sinDizzy 2014-11-20 19:11:28

回答

0

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

相关问题