Rollback Method (SQLiteConnection)
Rolls back a transaction from a pending state.
The following example demonstrates how to use
Commit and
Rollback methods.
public void RunSQLiteTransaction(string myConnString)
{
SQLiteConnection sqConnection = new SQLiteConnection(myConnString);
sqConnection.Open();
SQLiteCommand sqCommand = new SQLiteCommand();
sqConnection.BeginTransaction();
try
{
sqCommand.CommandText = "INSERT INTO Dept(DeptNo, DName) Values(50, 'DEVELOPMENT')";
sqCommand.ExecuteNonQuery();
sqCommand.CommandText = "INSERT INTO Dept(DeptNo, DName) Values(60, 'PRODUCTION')";
sqCommand.ExecuteNonQuery();
sqConnection.Commit();
Console.WriteLine("Both records are written to database.");
}
catch(Exception e)
{
sqConnection.Rollback();
Console.WriteLine(e.ToString());
Console.WriteLine("Neither record was written to database.");
}
finally
{
sqConnection.Close();
}
}
Public Sub RunSQLiteTransaction(myConnString As String)
Dim sqConnection As New SQLiteConnection(myConnString)
sqConnection.Open()
Dim sqCommand As New SQLiteCommand()
sqConnection.BeginTransaction()
Try
sqCommand.CommandText = "INSERT INTO Dept(DeptNo, DName) Values(50, 'DEVELOPMENT')"
sqCommand.ExecuteNonQuery()
sqCommand.CommandText = "INSERT INTO Dept(DeptNo, DName) Values(60, 'PRODUCTION')"
sqCommand.ExecuteNonQuery()
sqConnection.Commit()
Console.WriteLine("Both records are written to database.")
Catch e As Exception
sqConnection.Rollback()
Console.WriteLine(e.ToString())
Console.WriteLine("Neither record was written to database.")
Finally
sqConnection.Close()
End Try
End Sub
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