用ADO对象(非ADO控件绑定)做的数据库查询,如果我想把查询结果显示在DataGrid之类的控件上,那么相应控件的DataSource属性该怎么设置?以下是我写的测试程序,请大虾指教。
Option Explicit
Dim conTest As New ADODB.Connection
Dim cmdSci As New ADODB.Command
Dim rsSci As New ADODB.Recordset
Private Sub Form_Load()
  Dim DBpath As String
  Dim strConn As String
  Dim strSQL As String
  DBpath = App.Path
  If Right(DBpath, 1) <> "\" Then DBpath = DBpath & "\"
  DBpath = DBpath & "Test.mdb"
  strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & DBpath & ";Persist Security Info=False"
  conTest.ConnectionString = strConn
  conTest.Open
  cmdSci.ActiveConnection = conTest
  strSQL = "select * from Scientist where Country = 'China'"
  cmdSci.CommandText = strSQL
  rsSci.Open cmdSci, , adOpenDynamic, adLockBatchOptimistic
  dgTest.DataSource = rsSci             '执行到此处提示说.DataSource成员找不到
  dgTest.Refresh
End Sub
以上dgTest是一个DataGrid控件,若改用ADODC绑定数据库则结果可以在DataGrid上正确显示。

解决方案 »

  1.   

    你首先要在VB菜单中:
    “工程”-->“引用”-->“Microsoft AxtiveX Data Objects 2.X Library”
    注:2.X为版本号,如果你机子上有高版本的就用高版本的,如:2.5或2.6的。
    "工程"-->“部件”-->“MicroSoft DataGrid Control 6.0”
    示例:
    Private Sub Command1_click()
      Dim cn As New ADODB.Connection
      Dim rs As New ADODB.Recordset
      Dim cnstr As String
      cnstr = oConn.Open "Driver={Microsoft dBASE Driver (*.dbf)};" & _
               "DriverID=277;" & _
               "Dbq=" & app.path & "\data"
      cn.Open cnstr
      rs.CursorLocation = adUseClient'使用客户端游标
      rs.Open "select * from XXX.DBF", cn, adOpenKeyset, adLockBatchOptimistic
      set datagrid1.datasource=rs
      datagrid1.refresh  
    End Sub以上示例程序的作用是将XXX.dbf表中的记录显示在datagrid控件中。
      

  2.   

    Option Explicit
    dim  conTest As New ADODB.Connection
    dim cmdSci As New ADODB.Command
    dim  rsSci As New ADODB.Recordset
    Private Sub Form_Load()
      Dim DBpath As String
      Dim strConn As String
      Dim strSQL As String
      DBpath = App.Path
      If Right(DBpath, 1) <> "\" Then DBpath = DBpath & "\"
      DBpath = DBpath & "Test.mdb"
      strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & DBpath & ";Persist Security Info=False"
      conTest.ConnectionString = strConn
      conTest.Open
      cmdSci.ActiveConnection = conTest
      strSQL = "select * from Scientist where Country = 'China'"
      cmdSci.CommandText = strSQL
      rsSci.CursorLocation = adUseClient'注意:使用客户端游标
      rsSci.Open cmdSci, , adOpenDynamic, adLockBatchOptimistic
      set dgTest.DataSource = rsSci    '执行到此处提示说.DataSource成员找不到,你少写了一个set
      dgTest.Refresh
    End Sub