To perform transactional DB operations:
BeginTransaction
method.
For example:
using(MyDatabase db = new
MyDatabase())
{
// Begin the transaction
db.BeginTransaction();
try
{
// Your DB code.
UserRow user =
db.UserTable.GetByPrimaryKey("John", "Smith");
user.City = "Dallas";
db.UserTable.Update(user);
user =
db.UserTable.GetByPrimaryKey("Amy", "Smith");or
user.City = "Dallas";
db.UserTable.Update(user);
// Commit the
transaction
db.CommitTransaction();
// Both records are
updated
}
catch (Exception)
{
// Rollback the
transaction
db.RollbackTransaction();
// Neither record
was updated
}
}
MyDatabase db = new MyDatabase();
try
{
// Begin the transaction
db.BeginTransaction();
try
{
// Your DB code.
UserRow user =
db.UserTable.GetByPrimaryKey("John", "Smith");
user.City = "Dallas";
db.UserTable.Update(user);
user =
db.UserTable.GetByPrimaryKey("Amy", "Smith"); catch (Exception)
user.City = "Dallas";
db.UserTable.Update(user);
// Commit the
transaction
db.CommitTransaction();
// Both records are
updated
}
{
// Rollback the
transaction
db.RollbackTransaction();
// Neither record
was updated
}
}
finally
{
db.Close();
}
Dim db As New MyDatabase()
Try
' Begin the transaction
db.BeginTransaction()
Try
' Your DB code.
Dim user As UserRow
user =
db.UserTable.GetByPrimaryKey("John", "Smith")
user.City = "Dallas"
db.UserTable.Update(user)
user =
db.UserTable.GetByPrimaryKey("Amy", "Smith") ' Both
records are updated
user.City = "Dallas"
db.UserTable.Update(user)
' Commit the
transaction
db.CommitTransaction()
Catch e As
Exception
' Rollback the
transaction
db.RollbackTransaction()
' Neither
record was updated
End Try
Finally
db.Close()
End Try