【问题标题】:How to Upload Excel Data into SQL with Access Vba?如何使用 Access Vba 将 Excel 数据上传到 SQL?
【发布时间】:2017-05-24 07:10:15
【问题描述】:

我在 Excel 中有大量数据需要上传到 SQL Server,但我使用 Access 作为前端。 Excel 中的列数约为 90,记录数超过 700,000

【问题讨论】:

  • 欢迎来到 StackOverflow。请阅读stackoverflow.com/help/how-to-ask,并尝试通过更多信息改进您的问题。您已经尝试了什么,什么不起作用,等等。
  • 下载 SQL Server Management Studio 并在 Access OLEDB 提供程序的帮助下使用 OPENROWSET 查询文件。
  • 嗨 Pawel,我尝试了下面的代码,但它给了我错误或找不到对象,因为我正在使用“匹配”功能。首先使用 SQL 列检查 Excel 数据的列 For col = 0 To .Fields.count - 1 index = xlApp.Application.Match(.Fields(col).Name, sourceRange.Rows(2), 0) If index > 0 Then exportFieldsCount = exportFieldsCount + 1 tableFields(exportFieldsCount) = col rangeFields(exportFieldsCount) = index End If Next 然后将数据添加到 Recordset。但是 Recordset.UpdateBatch 也不起作用
  • 嗨 Pawel,你能分享任何可以帮助我的文章或链接吗
  • For col = 0 To .Fields.count - 1 index = xlApp.Application.Match(.Fields(col).Name, sourceRange.Rows(2), 0) If index > 0 Then exportFieldsCount = exportFieldsCount + 1 tableFields(exportFieldsCount) = col rangeFields(exportFieldsCount) = index End If Next 上面的代码给了我 Method Not found 因为我使用的是 Access 但函数是 Excel 的

标签: sql-server ms-access file-upload vba


【解决方案1】:

大约一个小时前,我刚刚回答了这样一个问题。

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/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-09-09
    • 2013-04-25
    • 1970-01-01
    • 2013-04-16
    • 2021-08-28
    • 2013-08-17
    • 2018-09-13
    相关资源
    最近更新 更多