gis
gis
管理员
管理员
  • 注册日期2003-07-16
  • 发帖数15945
  • QQ554730525
  • 铜币25337枚
  • 威望15352点
  • 贡献值0点
  • 银元0个
  • GIS帝国居民
  • 帝国沙发管家
  • GIS帝国明星
  • GIS帝国铁杆
阅读:1538回复:0

关于如何使用ADO连接数据库

楼主#
更多 发布于:2003-10-10 09:28
使用ADO连接数据库,通过绑定datagrid控件来显示数据库中符合条件的内容。

Option Explicit

Private Sub Command1_Click()
Dim cn As New ADODB.Connection
Dim Cmd As New ADODB.Command
Dim rs As New ADODB.Recordset
Dim strConnect As String
Set cn = New ADODB.Connection
strConnect = "Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;Initial Catalog=database;Data Source=servername"  '指定连接字符串
cn.ConnectionString = strConnect
cn.Open
With Cmd
  .ActiveConnection = cn
  .CommandType = adCmdStoredProc        '有四种可选择的类型
  .CommandText = "SP_Get_Name"           '存储过程名称
  .Parameters.Refresh
  .Parameters("@dt").Value = date()           '存储过程中参数的值(存储过程中只有一个参数)
End With

With rs
.CursorLocation = adUseClient
.CursorType = adOpenStatic
.LockType = adLockReadOnly
.Open Cmd
End With
Set DataGrid1.DataSource = rs

End Sub

要在DataGrid控件中显示数据CursorLocation = adUseClient和Set DataGrid1.DataSource = rs两个语句不可缺少。
喜欢0 评分0
游客

返回顶部