当我使用服务器端游标,游标类型为adOpenKeyset打开一个表时它自动转换为了动态游标。而有的表就不会自动转换,这是怎么回事?(注:我都建了主键)

解决方案 »

  1.   

    如果将 CursorLocation 属性设置为 adUseClient 则只支持 adUseStatic 的设置。如果设置了不支持的值,不会导致错误,将使用最接近支持的 CursorType。如果提供者不支持所请求的游标类型,提供者可能会返回其他游标类型。打开 Recordset 对象时,将更改 CursorType 属性使之与实际使用的游标匹配。要验证返回游标的指定功能,请使用 Supports 方法。关闭 Recordset 后,CursorType 属性将恢复为最初的设置。
      

  2.   

    该范例使用 Supports 方法,显示用不同游标类型打开的记录集所支持的选项。运行该过程需要 DisplaySupport 过程。Public Sub SupportsX()   Dim aintCursorType(4) As Integer
       Dim rstTitles As ADODB.Recordset
       Dim strCnn As String
       Dim intIndex As Integer   ' 打开连接。
          strCnn = "Provider=sqloledb;" & _
          "Data Source=srv;Initial Catalog=pubs;User Id=sa;Password=; "   ' 使用 CursorType 常量填充数组。
       aintCursorType(0) = adOpenForwardOnly
       aintCursorType(1) = adOpenKeyset
       aintCursorType(2) = adOpenDynamic
       aintCursorType(3) = adOpenStatic
       
       ' 使用每个 CursorType 和优化锁定打开记录集,
       ' 然后调用 DisplaySupport 过程显示所支持的选项。
       For intIndex = 0 To 3
          Set rstTitles = New ADODB.Recordset
          rstTitles.CursorType = aintCursorType(intIndex)
          rstTitles.LockType = adLockOptimistic
          rstTitles.Open "titles", strCnn, , , adCmdTable
          
          Select Case aintCursorType(intIndex)
             Case adOpenForwardOnly
                Debug.Print "ForwardOnly cursor supports:"
             Case adOpenKeyset
                Debug.Print "Keyset cursor supports:"
             Case adOpenDynamic
                Debug.Print "Dynamic cursor supports:"
             Case adOpenStatic
                Debug.Print "Static cursor supports:"
          End Select      DisplaySupport rstTitles
          rstTitles.Close
       Next intIndexEnd SubPublic Sub DisplaySupport(rstTemp As ADODB.Recordset)   Dim alngConstants(11) As Long
       Dim booSupports As Boolean
       Dim intIndex As Integer   ' 用游标选项常量填充数组。
       alngConstants(0) = adAddNew
       alngConstants(1) = adApproxPosition
       alngConstants(2) = adBook
       alngConstants(3) = adDelete
       alngConstants(4) = adFind
       alngConstants(5) = adHoldRecords
       alngConstants(6) = adMovePrevious
       alngConstants(7) = adNotify
       alngConstants(8) = adResync
       alngConstants(9) = adUpdate
       alngConstants(10) = adUpdateBatch
       
       For intIndex = 0 To 10
          booSupports = _
             rstTemp.Supports(alngConstants(intIndex))
          If booSupports Then
             Select Case alngConstants(intIndex)
                Case adAddNew
                   Debug.Print "   AddNew"
                Case adApproxPosition
                   Debug.Print "   AbsolutePosition and AbsolutePage"
                Case adBook
                   Debug.Print "   Book"
                Case adDelete
                   Debug.Print "   Delete"
                Case adFind
                   Debug.Print "   Find"
                Case adHoldRecords
                   Debug.Print "   Holding Records"
                Case adMovePrevious
                   Debug.Print "   MovePrevious and Move"
                Case adNotify
                   Debug.Print "   Notifications"
                Case adResync
                   Debug.Print "   Resyncing data"
                Case adUpdate
                   Debug.Print "   Update"
                Case adUpdateBatch
                   Debug.Print "   batch updating"
             End Select
          End If
       Next intIndexEnd Sub