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

如题所述

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
温馨提示:答案为网友推荐,仅供参考

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网