I've written a little programm which gets an .xlsx file in input (this file is periodically updated), and it extracts the data to an sql table. I would like to periodically update this sql table by comparing the content of the excel file with the content of sql table, and insert into it all the new rows, if they exist. I searched for many solutions with no success. How could I do?
My actual code looks like:
Dim ExcelConnection As New System.Data.OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & SourceFile & ";Extended Properties=""Excel 12.0 Xml;HDR=Yes""")
ExcelConnection.Open()
Dim RequeteExcelMat As String = "SELECT * FROM [feuil 1$]"
Dim objCmdSelect As OleDbCommand = New OleDbCommand(RequeteExcelMat, ExcelConnection)
Dim objDR As OleDbDataReader
Dim ConnexionBDDMat As New SqlConnection("SERVER=(local);DATABASE=MatStat;Trusted_Connection=True")
Try
ConnexionBDDMat.Open()
Catch ex As Exception
MsgBox(ex.Message)
End Try
Using bulkCopy As SqlBulkCopy = New SqlBulkCopy(ConnexionBDDMat)
bulkCopy.DestinationTableName = "dbo.Material"
'If SourceFile Content different from "Material" Sql Table Content Then
' Update "Material" Table by inserting the new rows from SourceFile
'Else
' MsgBox("Table is already updated")
'End If
Try
objDR = objCmdSelect.ExecuteReader
bulkCopy.WriteToServer(objDR)
objDR.Close()
ConnexionBDDMat.Close()
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Using