IsolationLevel 和 Mode 属性范例

该范例使用 Mode 属性打开独占连接,使用 IsolationLevel 属性打开独立于其他事务执行的事务。

Public Sub IsolationLevelX()   Dim cnn1 As ADODB.Connection
   Dim rstTitles As ADODB.Recordset
   Dim strCnn As String   ' 将连接字符串赋给变量。
   strCnn = "Provider=sqloledb;" & _
      "Data Source=srv;Initial Catalog=pubs;User Id=sa;Password=; "   ' 打开连接和标题表。
   Set cnn1 = New ADODB.Connection
   cnn1.Mode = adModeShareExclusive
   cnn1.IsolationLevel = adXactIsolated
   cnn1.Open strCnn
   
   Set rstTitles = New ADODB.Recordset
   rstTitles.CursorType = adOpenDynamic
   rstTitles.LockType = adLockPessimistic
   rstTitles.Open "titles", cnn1, , , adCmdTable
   
   cnn1.BeginTrans   ' 显示连接模式。
   If cnn1.Mode = adModeShareExclusive Then
      MsgBox "Connection mode is exclusive."
   Else
      MsgBox "Connection mode is not exclusive."
   End If   ' 显示独立级别。
   If cnn1.IsolationLevel = adXactIsolated Then
      MsgBox "Transaction is isolated."
   Else
      MsgBox "Transaction is not isolated."
   End If   ' 更改 psychology 标题的类型。
   Do Until rstTitles.EOF
      If Trim(rstTitles!Type) = "psychology" Then
         rstTitles!Type = "self_help"
         rstTitles.Update
      End If
      rstTitles.MoveNext
   Loop   ' 打印记录集中的当前数据。
   rstTitles.Requery
   Do While Not rstTitles.EOF
      Debug.Print rstTitles!Title & " - " & rstTitles!Type
      rstTitles.MoveNext
   Loop   ' 恢复原始数据。
   cnn1.RollbackTrans
   rstTitles.Close
   
   cnn1.CloseEnd Sub
www.holmesian.org