Knight Data Access Layer
Transactions
Basic Tasks C# > Saving Data > Transactions

Transactions are handled as expected through ADO.NET.  Begin, Commit, and Rollback are supported.  Below is an example of a transaction.  The Unit of Work pattern is also supported.  See the Patterns section.

 

//Get the current instance of the helper

IDataHelper db = DataHelper.SessionFactory();

 

//Begin a transaction to save two people at once

db.BeginTransaction();

 

//Create a new Person to save to the database

Person person1 = new Person();

person1.FirstName = "John";

person1.LastName = "Smith";

 

//Save it to the database (see the SQL in the console)

db.Save(person1);

 

//Create another new Person to save to the database

Person person2 = new Person();

person2.FirstName = "Jane";

person2.LastName = "Smith";

 

//Save it to the database (see the SQL in the console)

db.Save(person2);

 

//Get the current transaction and print out some info

Console.WriteLine(db.Transaction.Connection.ConnectionString);

 

//Commit the transaction

db.CommitTransaction();