ADO(트랜잭션 제어)

ADO는 Connection 개체에서 BeginTrans, CommitTransRollbackTrans 메서드의 도움으로 연결 내에서 트랜잭션 처리를 지원합니다. ADO에서 트랜잭션 처리를 구현하는 일반적인 개념은 다음과 같은 간단한 코드 조각에 설명되어 있습니다.

Const DS = "MySqlServer"  
Const DB = "Northwind"  
Const DP = "SQLOLEDB"  
  
Dim oConn As ADODB.Connection  
Dim oRs As ADODB.Recordset  
Dim sConn As String  
  
sConn = "Provider=" & DP & _  
          ";Data Source=" & DS & _  
          ";Initial Catalog=" & DB & _  
          ";Integrated Security=SSPI;"  
  
sSQL = "SELECT ProductID, ProductName FROM Products"  
  
Set oConn = New ADODB.Connection  
oConn.Open sConn  
  
' Create and Open the Recordset object.  
Set oRs = New ADODB.Recordset  
oRs.Open sSQL, oConn, adOpenStatic, adLockOptimistic, adCmdText  
  
If oRs.RecordCount > 1 Then  
  
    oRs.MoveFirst  
    Id1 = oRs("ProductID")  
    Name1 = oRs("ProductName")  
    oRs.MoveNext  
    Id2 = oRs("ProductID")  
    Name2 = oRs("ProductName")  
  
    q = "Switch ID's of " & Name1 & " and " & Name2 & "?"  
    If MsgBox(q, vbYesNo) = vbYes Then  
        oRs.MoveFirst  
        oRs("ProductName") = Name2  
        oRs.Update  
  
        oRs.MoveNext  
        oRs("ProductName") = Name1  
        oRs.Update  
  
        If MsgBox("Save changes?", vbYesNo) = vbYes Then  
  
        Else  
  
        End If  
    End If  
  
End If  
  
oRs.Close  
oConn.Close  

여기서 트랜잭션 처리는 두 레코드가 하나의 작업 단위로 업데이트되고 두 제품 이름이 서로 교환되거나 전혀 변경되지 않도록 하는 데 사용됩니다.

트랜잭션 처리에 대한 자세한 내용은 업데이트 및 데이터 유지참조하세요.