Represents a SQL transaction to be made in the SQLite database.
The following example creates a
SQLiteConnection and a
SQLiteTransaction. It also demonstrates how to use the
SQLiteConnection.BeginTransaction,
Commit, and
Rollback methods.
|
|---|
public static void RunSQLiteTransaction(string myConnString) {
using (SQLiteConnection sqLiteConnection = new SQLiteConnection(myConnString)) {
sqLiteConnection.Open();
// Start a local transaction
SQLiteTransaction myTrans = sqLiteConnection.BeginTransaction(System.Data.IsolationLevel.ReadCommitted);
SQLiteCommand sqLiteCommand = sqLiteConnection.CreateCommand();
try {
sqLiteCommand.CommandText = "INSERT INTO Dept(DeptNo, DName) Values(52, 'DEVELOPMENT')";
sqLiteCommand.ExecuteNonQuery();
sqLiteCommand.CommandText = "INSERT INTO Dept(DeptNo, DName) Values(62, 'PRODUCTION')";
sqLiteCommand.ExecuteNonQuery();
myTrans.Commit();
Console.WriteLine("Both records are written to database.");
}
catch (Exception e) {
myTrans.Rollback();
Console.WriteLine(e.ToString());
Console.WriteLine("Neither record was written to database.");
}
finally {
sqLiteCommand.Dispose();
myTrans.Dispose();
}
}
} |
|
|---|
Public Shared Sub RunSQLiteTransaction(myConnString As String)
Using sqLiteConnection As New SQLiteConnection(myConnString)
sqLiteConnection.Open()
' Start a local transaction
Dim myTrans As SQLiteTransaction = sqLiteConnection.BeginTransaction(System.Data.IsolationLevel.ReadCommitted)
Dim sqLiteCommand As SQLiteCommand = sqLiteConnection.CreateCommand()
Try
sqLiteCommand.CommandText = "INSERT INTO Dept(DeptNo, DName) Values(52, 'DEVELOPMENT')"
sqLiteCommand.ExecuteNonQuery()
sqLiteCommand.CommandText = "INSERT INTO Dept(DeptNo, DName) Values(62, 'PRODUCTION')"
sqLiteCommand.ExecuteNonQuery()
myTrans.Commit()
Console.WriteLine("Both records are written to database.")
Catch e As Exception
myTrans.Rollback()
Console.WriteLine(e.ToString())
Console.WriteLine("Neither record was written to database.")
Finally
sqLiteCommand.Dispose()
myTrans.Dispose()
End Try
End Using
End Sub |