Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
.NET Framework 3.5

  Switch on low bandwidth view
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

Other versions are also available for the following:
.NET Framework Class Library
OleDbTransaction Class

Represents an SQL transaction to be made at a data source. This class cannot be inherited.

Namespace:  System.Data.OleDb
Assembly:  System.Data (in System.Data.dll)
Visual Basic (Declaration)
Public NotInheritable Class OleDbTransaction _
    Inherits DbTransaction
Visual Basic (Usage)
Dim instance As OleDbTransaction
C#
public sealed class OleDbTransaction : DbTransaction
Visual C++
public ref class OleDbTransaction sealed : public DbTransaction
JScript
public final class OleDbTransaction extends DbTransaction

The application creates an OleDbTransaction object by calling BeginTransaction on the OleDbConnection object. All subsequent operations associated with the transaction (for example, committing or aborting the transaction), are performed on the OleDbTransaction object.

System..::.Object
  System..::.MarshalByRefObject
    System.Data.Common..::.DbTransaction
      System.Data.OleDb..::.OleDbTransaction
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
A quick example      ometecuhtli2001   |   Edit   |   Show History
The BeginTransaction articles also have examples, but I noticed some inefficiencies in the code (like assigning the connection object to the command object twice). I just tested this quick hack on an XP SP2 system with Office 2000 installed. It ran fine against an Access 97 format database:

Module Module1

Sub Main()
Dim dbsrc As New OleDb.OleDbConnection(String.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Jet OLEDB:Database Password=foo", "filename.mdb")
Dim cmd As OleDb.OleDbCommand
Dim trans As OleDb.OleDbTransaction
Dim act As String = ""
Dim lcv As Integer

Try
act = "opening database"
dbsrc.Open()

act = "create command object"
cmd = dbsrc.CreateCommand

act = "create transaction"
trans = dbsrc.BeginTransaction
act = "assign transaction to command"
cmd.Transaction = trans

act = "add some data"
For lcv = 1000 To 1010
cmd.CommandType = CommandType.Text
cmd.CommandText = String.Format("INSERT INTO comArea (Area) VALUES ('{0}')", lcv)
cmd.ExecuteNonQuery()
Next

act = "roll back transaction"
trans.Rollback()

act = "dispose trans"
trans.Dispose()
act = "dispose cmd"
cmd.Dispose()

act = "closing database"
dbsrc.Close()
dbsrc.Dispose()
Catch ex As Exception
Console.WriteLine("Error " & act & vbCrLf & ex.ToString)
End Try

Console.ReadKey() 'So you can see the screen to read messages
End Sub

End Module


To use this example, create a new project and select a console application. Then replace all the code in the module1.vb file with the above code. Change the connection string to something appropriate (i.e., the database path and filename) and change the SQL statement's table and field name. Then run it. By default, this will roll back the transaction so it does not actually make any changes to the target table. Use the strings assigned to the act variable as a guide to what the code is doing (as opposed to comments in the code). If you change the rollback() to a commit() you will get ten extra records in the specified table.
Tags What's this?: Add a tag
Flag as ContentBug
A quick transaction example      ometecuhtli2001   |   Edit   |   Show History

The BeginTransaction articles also have examples, but I noticed some inefficiencies in the code (like assigning the connection object to the command object twice). I just tested this quick hack on an XP SP2 system with Office 2000 installed. It ran fine against an Access 97 format database:

Module Module1

Sub Main()
Dim dbsrc As New OleDb.OleDbConnection(String.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Jet OLEDB:Database Password=foo", "filename.mdb")
Dim cmd As OleDb.OleDbCommand
Dim trans As OleDb.OleDbTransaction
Dim act As String = ""
Dim lcv As Integer

Try
act = "opening database"
dbsrc.Open()

act = "create command object"
cmd = dbsrc.CreateCommand

act = "create transaction"
trans = dbsrc.BeginTransaction
act = "assign transaction to command"
cmd.Transaction = trans

act = "add some data"
For lcv = 1000 To 1010
cmd.CommandType = CommandType.Text
cmd.CommandText = String.Format("INSERT INTO comArea (Area) VALUES ('{0}')", lcv)
cmd.ExecuteNonQuery()
Next

act = "roll back transaction"
trans.Rollback()

act = "dispose trans"
trans.Dispose()
act = "dispose cmd"
cmd.Dispose()

act = "closing database"
dbsrc.Close()
dbsrc.Dispose()
Catch ex As Exception
Console.WriteLine("Error " & act & vbCrLf & ex.ToString)
End Try

Console.ReadKey() 'So you can see the screen to read messages
End Sub

End Module


To use this example, create a new project and select a console application. Then replace all the code in the module1.vb file with the above code. Change the connection string to something appropriate (i.e., the database path and filename) and change the SQL statement's table and field name. Then run it. By default, this will roll back the transaction so it does not actually make any changes to the target table. Use the strings assigned to the act variable as a guide to what the code is doing (as opposed to comments in the code). If you change the rollback() to a commit() you will get ten extra records in the specified table.

Tags What's this?: Add a tag
Flag as ContentBug
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker