我寫了一個小程序,它在輸入中獲取一個.xlsx文件(該文件是定期更新的),並將數據提取到一個sql表中。 我想通過比較excel文件的內容和sql表格的內容來定期更新這個sql表格,並且如果它們存在,將所有新行插入到它中。 我搜索了很多沒有成功的解決方案。 我該怎麼辦?使用vb.net從excel文件更新SQL表
我的實際代碼如下所示:
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