我有一個SqlBulkCopy
函數,用於導入Excel工作表。它工作正常,除了我想向Excel電子表格中不存在的數據庫表添加變量/常量值。我嘗試過的所有東西都不起作用。SqlBulkCopy將變量添加到Excel inport ASP.Net
我一直在試圖添加變量MyUser
,我不斷收到相同的錯誤OleDBException Was Unhandled
。註釋掉的兩行是我最後的兩次嘗試。我正在使用asp.net 4.0,VS2010,SQL Server 2008.
Dim excelConnectionString As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & MyFile & ";Extended Properties=""Excel 12.0;HDR=YES;"""
Using connection As New System.Data.OleDb.OleDbConnection(excelConnectionString)
Dim MyUser As String = UCase(My.User.Name)
Dim command As New System.Data.OleDb.OleDbCommand("Select MA_Code FROM [" & cboSheet.Text & "$]", connection)
'Dim command As New System.Data.OleDb.OleDbCommand("Select MA_Code, " & MyUser & " as MyUser FROM [" & cboSheet.Text & "$]", connection)
'Dim command As New System.Data.OleDb.OleDbCommand("Select MA_Code, " + MyUser + " as MyUser FROM [" & cboSheet.Text & "$]", connection)
connection.Open()
Using dr As System.Data.OleDb.OleDbDataReader = command.ExecuteReader()
Dim BKcn As New SqlClient.SqlConnection
BKcn.ConnectionString = My.Settings.MyConnection
BKcn.Open()
Try
Using bulkCopy As New System.Data.SqlClient.SqlBulkCopy(BKcn)
bulkCopy.BatchSize = 500
bulkCopy.BulkCopyTimeout = 3000
bulkCopy.DestinationTableName = cboSheet.Text
bulkCopy.ColumnMappings.Add("MA_Code", "MA_Code")
bulkCopy.WriteToServer(dr)
End Using