工程,引用,Microsoft ActiveX Data Object 2.8 Library打上勾
下面的服务器名,数据库名,表名改成你自己的
Dim cn As ADODB.Connection
Dim rs As ADODB.Recordset
Private Sub cmd_search_Click()
Dim sql As String
Set cn = New ADODB.Connection
cn.Open "Provider=SQLOLEDB;Data Source=服务器名;Initial Catalog=数据库名;User ID=sa;Password=;"
Set rs = New ADODB.Recordset
rs.CursorLocation = adUseClient
rs.Properties("Initial Fetch Size") = 2
rs.Properties("Background Fetch Size") = 4
sql = "select * from 表名"
rs.Open sql, cn, adOpenStatic, adLockOptimistic, adCmdText
Set DataGrid1.DataSource = rs
End Sub
温馨提示:答案为网友推荐,仅供参考