Dim conn As New ADODB.Connection
Dim rs As New ADODB.Recordset
Dim strsql As Stringconn.Open "Driver={MySQL ODBC 3.51 Driver};uid=root;DATABASE=mydb;Server=localhost;"
strsql = "select distinct ID from myTable order by ID asc"
rs.Open strsql, conn, adOpenStatic, adLockReadOnly执行操作后,rs中有多条记录存在,可是rs.RecordCount的值却是-1 这是怎么回事呢?
如何能得到记录的条数呢?

解决方案 »

  1.   


    在rs.open的上面加一句Conn.CursorLocation = adUseClient
    再看下结果
      

  2.   

    Dim conn As New ADODB.Connection
    Dim rs As New ADODB.Recordset
    Dim strsql As Stringconn.Open "Driver={MySQL ODBC 3.51 Driver};uid=root;DATABASE=mydb;Server=localhost;"
    strsql = "select distinct ID from myTable order by ID asc"
    Set rs.ActiveConnection = conn
    rs.LockType=adLockReadOnly
    rs.CursorLocation=adUseClient
    rs.CursorType=adOpenStatic
    rs.Open strsql
      

  3.   

    在VB6里像你这样调用是这样的,
    因为没有定义RS的CursorLocation,
    只有这个定义后,才能对RS使用COUNT    Dim rs As recordset
        Set rs = New recordset
        '开启recordset
        rs.CursorLocation = adUseClient
        rs.CursorType = adOpenDynamic
        rs.Open cSQL, connDB, adOpenDynamic, adLockOptimistic
      

  4.   

    我记得以前做过测试,好像只加CursorLocation就可以了,
    CursorType和LockType好像可以不用加就能用COUNT,有些记不清了。不过,如果三个都加一定是可以用的。
      

  5.   

    Conn.CursorLocation = adUseClient