excel vba中执行sql select的一个问题

2024-11-25 23:12:14
推荐回答(1个)
回答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