【发布时间】: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 & " ')"] 在下一行。方括号仅用于说明。我意识到通常会将 [ & _ ] 放在一行的末尾,但这似乎不起作用。谢谢