'sql完整例子
Sub testSql()
'定义连接对象
Dim cnn As New ADODB.Connection
Dim rs As New ADODB.Recordset
'定义连接字符串
Dim conStr As String
Dim sqlstr As String
'连接字符串-以下是连接MSSQL数据库
conStr = "Provider=sqloledb; " _
& "Server=192.168.1.121; " _
& "Database=DATABASENAME;Uid=admin;Pwd=admin;"
cnn.Open conStr
sqlstr = "SELECT * from tablename"
rs.Open sqlstr, cnn
Range("a2").CopyFromRecordset rs
rs.Close
cnn.Close
End Sub
引用ADO来实现