Click here to view and discuss this page in DocCommentXchange. In the future, you will be sent there automatically.

SQL Anywhere 11.0.1 (中文) » SQL Anywhere 服务器 - 编程 » SQL Anywhere 数据访问 API » SQL Anywhere OLE DB 和 ADO 开发 » 利用 SQL Anywhere 进行 ADO 编程

 

通过游标更新数据

SQL Anywhere OLE DB 提供程序允许通过游标更新结果集。该功能不能通过 MSDASQL 提供程序实现。

更新记录集

可以通过 Recordset 来更新数据库。

Private Sub cmdUpdateThroughCursor_Click( _
        ByVal eventSender As System.Object, _
        ByVal eventArgs As System.EventArgs) _
        Handles cmdUpdateThroughCursor.Click

    ' Declare variables
    Dim i As Integer
    Dim myConn As New ADODB.Connection
    Dim myRS As New ADODB.Recordset
    Dim SQLString As String

    On Error GoTo HandleError

    ' Connect
    myConn.Provider = "SAOLEDB"
    myConn.ConnectionString = _
        "Data Source=SQL Anywhere 11 Demo"
    myConn.Open()
    myConn.BeginTrans()
    SQLString = "SELECT * FROM Customers"
    myRS.Open(SQLString, myConn, _
        ADODB.CursorTypeEnum.adOpenDynamic, _
        ADODB.LockTypeEnum.adLockBatchOptimistic)

    If myRS.BOF And myRS.EOF Then
        MsgBox("Recordset is empty!", 16, "Empty Recordset")
    Else
        MsgBox("Cursor type: " & CStr(myRS.CursorType), _
            MsgBoxStyle.Information)
        myRS.MoveFirst()
        For i = 1 To 3
            MsgBox("Row: " & CStr(myRS.Fields("ID").Value), _
                MsgBoxStyle.Information)
            If i = 2 Then
                myRS.Update("City", "Toronto")
                myRS.UpdateBatch()
            End If
            myRS.MoveNext()
        Next i
        myRS.Close()
    End If
    myConn.CommitTrans()
    myConn.Close()
    Exit Sub

HandleError:
    MsgBox(ErrorToString(Err.Number))
    Exit Sub

End Sub
注意

如果对 Recordset 使用了 adLockBatchOptimistic 设置,则 myRS.Update 方法不会对数据库本身做任何更改。而是会更新 Recordset 的本地副本。

myRS.UpdateBatch 方法对数据库服务器进行更新,但是不提交更改,因为它位于事务内部。如果 UpdateBatch 方法是在事务外部调用的,则更改将被提交。

myConn.CommitTrans 方法提交所做的更改。Recordset 对象在此之前已被关闭,因此不存在数据的本地副本是否发生更改的问题。