Ninja Database Pro
BeginTransaction Method
Example 



KellermanSoftware.NinjaDatabasePro Namespace > NinjaDbPro Class : BeginTransaction Method
Start a transaction to wrap a set of actions. Do a CommitTransaction to permanantly save the actions. Do a RollbackTranaction to undo all the actions in the transaction. If the Phone or PC loses power during a transaction it will automatically be rolled back on OpenDatabase.
Syntax
Public Sub BeginTransaction() 
Dim instance As NinjaDbPro
 
instance.BeginTransaction()
public void BeginTransaction()
public:
void BeginTransaction(); 
Example
NinjaDbPro db = new NinjaDbPro("MyDatabaseDirectory", "MyDatabaseName");
             
            //Licensed Mode
            //db.UserName = "John Smith 101224";
            //db.LicenseKey = "aousdf832jasf==";
             
            //Set before OpenDatabase. Default storage is IsolatedStorageDatabase. Other options are:
            //db.Storage = new MemoryDatabase(); //In memory database
            //db.Storage = new FileDatabase();  
             
            Person person1 = new Person();
            person1.Name = "John";
             
            Person person2 = new Person();
            person2.Name = "Jane";
             
            db.OpenDatabase();
             
            //These two persons will be saved as a group
            try
            {
                db.BeginTransaction();
                db.Save(person1);
                db.Save(person2);
                db.CommitTransaction();
            }
            catch (Exception)
            {
                //Rollback when there is an error
                db.RollbackTransaction();
                throw;
            }
             
            //This will load a person since it was committed
            Person person1Copy = db.Load<Person>(person1.PersonId);
             
            db.CloseDatabase();
Dim db As New NinjaDbPro("MyDatabaseDirectory", "MyDatabaseName")
             
            'Licensed Mode
            'db.UserName = "John Smith 101224";
            'db.LicenseKey = "aousdf832jasf==";
             
            'Set before OpenDatabase. Default storage is IsolatedStorageDatabase. Other options are:
            'db.Storage = new MemoryDatabase(); //In memory database
            'db.Storage = new FileDatabase();  
             
            Dim person1 As New Person()
            person1.Name = "John"
             
            Dim person2 As New Person()
            person2.Name = "Jane"
             
            db.OpenDatabase()
             
            'These two persons will be saved as a group
            Try
                db.BeginTransaction()
                db.Save(person1)
                db.Save(person2)
                db.CommitTransaction()
            Catch e1 As Exception
                'Rollback when there is an error
                db.RollbackTransaction()
                Throw
            End Try
             
            'This will load a person since it was committed
            Dim person1Copy As Person = db.Load(Of Person)(person1.PersonId)
             
            db.CloseDatabase()
Requirements

Target Platforms: Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

See Also

Reference

NinjaDbPro Class
NinjaDbPro Members