首页 > 解决方案 > Excel VBA 分割代码行 - ADO 连接

问题描述

我有一些代码从 xl 工作表中获取数据并将其发送到 SQL 服务器表。

在我尝试将代码拆分为单独的行之前,一切正常。尝试了各种各样的东西,但似乎没有任何效果。

我试图分割的那条线

            "values ('" & strFirstName & "', '" & strLastName & " ')"

谢谢。

代码如下:

Sub TEST_UPLOAD() '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 = "QQQQQQ"
        table = "test1"
        database = "QQQQDB"


        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 test1"
        'End If

        'set first row with records to import
        'you could also just loop thru a range if you want.
        intImportRow = 2

        Do Until .Cells(intImportRow, 1) = ""
            strFirstName = .Cells(intImportRow, 1)
            strLastName = .Cells(intImportRow, 2)

            'insert row into database
            con.Execute "insert into test1 (firstname, lastname) " & _
            "values ('" & strFirstName & "', '" & strLastName & " ')"

            intImportRow = intImportRow + 1
        Loop

        MsgBox "Done importing", vbInformation

        con.Close
        Set con = Nothing

End With

退出子

标签: excelvbaado

解决方案


您可以通过使用参数化查询来避免将数据与代码混合。

Sub TEST_UPLOAD()
  
    Const server = "QQQQQQ"
    Const database = "QQQQDB"
    Const table = "test1"

    ' sql with placeholders ? for values
    Const SQL = " INSERT INTO " & table & _
                " (firstname, lastname) " & _
                " VALUES (?,?)"

    Dim con As New ADODB.Connection, sCon As String
    Dim intImportRow As Integer

    sCon = "Provider=SQLOLEDB;Data Source=" & server & _
            ";Initial Catalog=" & database & _
            ";Integrated Security=SSPI;"

    If con.State <> 1 Then
        con.Open sCon
    End If
  
    ' prepare sql with parameters
    Dim cmd As New ADODB.Command
    With cmd
       .ActiveConnection = con
       .CommandText = SQL
       .Parameters.Append .CreateParameter("P1", adVarChar, adParamInput, 50) ' varchar(50)
       .Parameters.Append .CreateParameter("P2", adVarChar, adParamInput, 50)
    End With

    ' clear table
    con.Execute "DELETE FROM " & table

    intImportRow = 2
    With Sheets("Sheet1")
        ' execute sql with parameter values
        Do Until .Cells(intImportRow, 1) = ""
            cmd.Parameters(0) = .Cells(intImportRow, 1) ' firstname
            cmd.Parameters(1) = .Cells(intImportRow, 2) ' lastname
            cmd.Execute
            intImportRow = intImportRow + 1
        Loop
    End With
    MsgBox intImportRow - 2 & " rows imported", vbInformation

    con.Close
    Set con = Nothing
End Sub

推荐阅读