Yesterday I learned how to use ADO.NET to take a DataTable and bulk insert all records into a SQL server database. That works great for one part of my project. Now what I need to do is take the records from the DataTable and insert only the records that do not already exist in the SQL server. Im using VB2010 and the code looks something like:
'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()
The SQL server destination table [DestDb].[dbo].[Events] has fields
[CityCode],[CarNum],[VIN],[Fleet],[CarMileage],[EventItm],[EventDate]
and the DataTable in memory has fields
City,CarNo,VIN,Fleet,Mileage,EventDesc,EventDate
For my purposes a unique record is made up of [CityCode],[CarNum],[VIN],[EventItm], and [EventDate]
Typically i will run this routine once a day and will get back about 200 records from the source. Been looking for a couple hours but just not sure how to approach this.