Excel 请问如何在VBA中执行SQL语句

2024-11-27 06:41:20
推荐回答(2个)
回答1:

sub test()

Dim Conn As New ADODB.Connection
Dim strConn As String
Dim strSQL As String
Dim rs As New ADODB.Recordset

strConn = "Provider=sqloledb;Server=192.168.1.111;Database=db2014;Uid=用户名;Pwd=密码;"
Conn.Open strConn

Max_row = Range("A1").CurrentRegion.Rows.Count

for i=1 to Max_row
strSQL = "select name from sales2014 where id='" & range("A" & i).value & "'"
Conn.Execute strSQL
rs.Open strSQL, Conn, 1, 1
range("B" & i).value= rs(0)
rs.Close
Set rs = Nothing
next i

'关闭数据库
Conn.Close
end sub

回答2:

vba执行sql实例提供