Excel 请问如何在VBA中执行SQL语句
2015-10-03
展开全部
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
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
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询