使用ADODB连接将Excel列插入SQL

时间:2017-11-06 21:23:02

标签: sql-server excel vba excel-vba

'我正在使用ADODB连接从SQL中的Excel表中插入值。到目前为止它的作用。 '我想知道如何在Excel表格中引用一列单元格,以便将其添加到下面的INSERT语句中。 '会是这样的:

Dim MyRange As Range
Set MyRange = Range("A2:A20")

Server_Name = "MyServer" '
Database_Name = "dbMyDataBase" '

Set conn = New ADODB.Connection
conn.Open "Driver={SQL Server};Server=" & Server_Name & ";Database=" & Database_Name & ";"


' Compose the INSERT statement.
statement = "INSERT INTO [dbMyDatabase].[dbo].[tbl_MyTable] VALUES (1,'90702', MyRange, '2014', 'ABC'), (2,'90703', MyRange, '2014', 'ABCD')"
' Execute the statement.
conn.Execute statement, , adCmdText

' Close the connection.
conn.Close

0 个答案:

没有答案