我已经设法通过 VB.net 从 Excel 表中获取数据到我的 SQL SERVER 表中,但是每次运行查询(单击按钮)时,相同的数据都会再次上传到我的表中。有没有办法只导入附加数据?(列“电子邮件”在我的 SQLSERVER 表中应该是唯一的)下面是我的代码:
谢谢您的帮助 !
Dim ExcelConnection As New System.Data.OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Excel.xlsm;Extended Properties=""Excel 12.0 Macro;HDR=No""")
ExcelConnection.Open()
Dim expr As String = "SELECT * FROM [Sheet1$]"
Dim objCmdSelect As OleDb.OleDbCommand = New OleDb.OleDbCommand(expr, ExcelConnection)
Dim objDR As OleDb.OleDbDataReader
Dim SQLconn As New SqlConnection()
Dim ConnString As String = "Data Source=USER\SQLEXPRESS;Initial Catalog=DBName;Integrated Security=SSPI"
SQLconn.ConnectionString = ConnString
SQLconn.Open()
Using bulkCopy As SqlBulkCopy = New SqlBulkCopy(SQLconn)
bulkCopy.DestinationTableName = "TableName"
Try
objDR = objCmdSelect.ExecuteReader
bulkCopy.WriteToServer(objDR)
objDR.Close()
SQLconn.Close()
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Using