2014-11-06 121 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

您可以嘗試在目標服務器的臨時表中加載數據。然後從那裏做獨特的插入。

相關問題