【问题标题】:Importing more 1000+ rows from Excel to SQL Server pt.2将更多 1000 多行从 Excel 导入 SQL Server pt.2
【发布时间】:2017-09-15 12:19:51
【问题描述】:

最初的问题是在 2015 年提出的。我想知道这里是否有人知道从那时起是否生成了一个新的 vba 代码来字符串 1000+ 行?

如果没有必要,我不想涉及服务器和更复杂的问题。

查看原帖: Importing more than 1000 rows from Excel to SQL-server

感谢您的帮助。

【问题讨论】:

    标签: sql-server string excel vba


    【解决方案1】:

    您应该可以在这里找到解决方案。

    https://www.excel-sql-server.com/excel-sql-server-import-export-using-vba.htm#Introduction

    或者,考虑一下。

    Sub Rectangle1_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
    

    或者........使用 WHERE 子句从 Excel 到 SQL Server。

    Sub InsertInto()
    
    'Declare some variables
    Dim cnn As adodb.Connection
    Dim cmd As adodb.Command
    Dim strSQL As String
    
    'Create a new Connection object
    Set cnn = New adodb.Connection
    
    'Set the connection string
    cnn.ConnectionString = "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=True;Initial Catalog=Northwind;Data Source=Excel-PC\SQLEXPRESS"
    'cnn.ConnectionString = "DRIVER=SQL Server;SERVER=Excel-PC\SQLEXPRESS;DATABASE=Northwind;Trusted_Connection=Yes"
    
    
    'Create a new Command object
    Set cmd = New adodb.Command
    
    'Open the Connection to the database
    cnn.Open
    
    'Associate the command with the connection
    cmd.ActiveConnection = cnn
    
    'Tell the Command we are giving it a bit of SQL to run, not a stored procedure
    cmd.CommandType = adCmdText
    
    'Create the SQL
    strSQL = "UPDATE TBL SET JOIN_DT = '2013-01-22' WHERE EMPID = 2"
    
    'Pass the SQL to the Command object
    cmd.CommandText = strSQL
    
    
    'Execute the bit of SQL to update the database
    cmd.Execute
    
    'Close the connection again
    cnn.Close
    
    'Remove the objects
    Set cmd = Nothing
    Set cnn = Nothing
    
    End Sub
    

    【讨论】:

    • 感谢您的投入和时间。 @ryguy72
    猜你喜欢
    • 2016-03-16
    • 1970-01-01
    • 1970-01-01
    • 2015-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-20
    相关资源
    最近更新 更多