【问题标题】:Excel VBA Splitting Line of Code - ADO ConnectionExcel VBA 分割代码行 - ADO 连接
【发布时间】:2021-09-06 13:10:14
【问题描述】:

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

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

我试图分割的那条线

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

谢谢。

代码如下:

子 TEST_UPLOAD() '可信连接 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

退出子

【问题讨论】:

  • 你是什么意思,“拆分” firstname 或 lastname 是否包含 ' ?
  • 将代码拆分为不同的行。例如:我想将这一行 ... "values ('" & strFirstName & "', '" & strLastName & " ')" 分成两行,两部分。至于为什么,是因为我要使用的真实查询非常大。谢谢
  • `"values ('" & strFirstName & _ "', '" & strLastName & " ')" 喜欢上面那行吗?
  • 这就是我要拆分的行。理想情况下,我希望 ["values ('" & strFirstName &] 在一行上, ["', '" & strLastName & " ')"] 在下一行。方括号仅用于说明。我意识到通常会将 [ & _ ] 放在一行的末尾,但这似乎不起作用。谢谢

标签: excel vba ado


【解决方案1】:

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

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

【讨论】:

  • 感谢您的回复,我会看看这个。
  • 这比我原来的方法好得多。现在已经实现了我更新包含 100 多列的表的位置。非常感谢安德鲁
猜你喜欢
  • 1970-01-01
  • 2019-08-06
  • 1970-01-01
  • 1970-01-01
  • 2019-01-10
  • 1970-01-01
  • 2020-10-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多