使用ADO连接
專案-->引用-->Microsoft ActiveX Data object 2.x Library
Dim Conn As New ADODB.Connection
Dim Rs As ADODB.Recordset
Set Rs = New ADODB.Recordset
Dim Strsql As String '定义要查询的sql语句
With Conn
.ConnectionString = "Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;Initial Catalog=Northwind;Data Source=."
.CommandTimeout = 0
.Open
End With
Strsql = "select * from Orders"
Rs.Open Strsql, Conn, adOpenKeyset, adLockReadOnly
If Rs.EOF Then
MsgBox "No Data"
Else
MsgBox "Have Data"
End If
Rs.Close
Set Rs = Nothing
Conn.Close
Set Conn = Nothing