Represents a SQL transaction to be made in the PostgreSQL database.
The following example creates a
PgSqlConnection and a
PgSqlTransaction. It also demonstrates how to use the
PgSqlConnection.BeginTransaction,
Commit, and
Rollback methods.
public static void RunPgSqlTransaction(string myConnString) {
using (PgSqlConnection pgConnection = new PgSqlConnection(myConnString)) {
pgConnection.Open();
// Start a local transaction
PgSqlTransaction myTrans = pgConnection.BeginTransaction(System.Data.IsolationLevel.ReadCommitted);
PgSqlCommand pgCommand = pgConnection.CreateCommand();
try {
pgCommand.CommandText = "INSERT INTO Dept(DeptNo, DName) Values(52, 'DEVELOPMENT')";
pgCommand.ExecuteNonQuery();
pgCommand.CommandText = "INSERT INTO Dept(DeptNo, DName) Values(62, 'PRODUCTION')";
pgCommand.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 {
pgCommand.Dispose();
myTrans.Dispose();
}
}
}
Public Shared Sub RunPgSqlTransaction(myConnString As String)
Using pgConnection As New PgSqlConnection(myConnString)
pgConnection.Open()
' Start a local transaction
Dim myTrans As PgSqlTransaction = pgConnection.BeginTransaction(System.Data.IsolationLevel.ReadCommitted)
Dim pgCommand As PgSqlCommand = pgConnection.CreateCommand()
Try
pgCommand.CommandText = "INSERT INTO Dept(DeptNo, DName) Values(52, 'DEVELOPMENT')"
pgCommand.ExecuteNonQuery()
pgCommand.CommandText = "INSERT INTO Dept(DeptNo, DName) Values(62, 'PRODUCTION')"
pgCommand.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
pgCommand.Dispose()
myTrans.Dispose()
End Try
End Using
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