-2
我有Excel中的大數據,我需要上傳到SQL Server,但我使用Access作爲前端。 Excel中的列數約爲90,記錄數超過700,000如何使用Access Vba將Excel數據上傳到SQL?
我有Excel中的大數據,我需要上傳到SQL Server,但我使用Access作爲前端。 Excel中的列數約爲90,記錄數超過700,000如何使用Access Vba將Excel數據上傳到SQL?
我剛剛在一個小時前回答了這樣的問題。
Sub Button_Click()
'TRUSTED CONNECTION
On Error GoTo errH
Dim con As New ADODB.Connection
Dim rs As New ADODB.Recordset
Dim strPath As String
Dim intImportRow As Integer
Dim strFirstName, strLastName As String
Dim server, username, password, table, database As String
With Sheets("Sheet1")
server = .TextBox1.Text
table = .TextBox4.Text
database = .TextBox5.Text
If con.State <> 1 Then
con.Open "Provider=SQLOLEDB;Data Source=" & server & ";Initial Catalog=" & database & ";Integrated Security=SSPI;"
'con.Open
End If
'this is the TRUSTED connection string
Set rs.ActiveConnection = con
'delete all records first if checkbox checked
If .CheckBox1 Then
con.Execute "delete from tbl_demo"
End If
'set first row with records to import
'you could also just loop thru a range if you want.
intImportRow = 10
Do Until .Cells(intImportRow, 1) = ""
strFirstName = .Cells(intImportRow, 1)
strLastName = .Cells(intImportRow, 2)
'insert row into database
con.Execute "insert into tbl_demo (firstname, lastname) values ('" & strFirstName & "', '" & strLastName & "')"
intImportRow = intImportRow + 1
Loop
MsgBox "Done importing", vbInformation
con.Close
Set con = Nothing
End With
Exit Sub
errH:
MsgBox Err.Description
End Sub
https://www.excel-sql-server.com/excel-sql-server-import-export-using-vba.htm
http://tomaslind.net/2013/12/26/export-data-excel-to-sql-server/
歡迎StackOverflow上。請閱讀https://stackoverflow.com/help/how-to-ask,並嘗試通過更多信息改進您的問題。你已經嘗試了什麼,什麼是不工作等 –
下載SQL Server Management Studio並使用OPENROWSET在Access OLEDB提供程序的幫助下查詢該文件。 – PacoDePaco
嗨,Pawel,我嘗試了下面的代碼,但它給了我錯誤或對象沒有找到,因爲我使用「匹配」功能。 首先使用SQL列檢查Excel數據列 對於col = 0 To .Fields.count - 1 index = xlApp.Application.Match(.Fields(col).Name,sourceRange.Rows(2),0) If index> 0然後 exportFieldsCount = exportFieldsCount + 1 tableFields(exportFieldsCount)= col rangeFields(exportFieldsCount)= index End If Next 然後將數據添加到Recordset中。 但是Recordset.UpdateBatch也不能正常工作 –