Database

Transactions

BeginTransaction starts a transaction on the current connection. Subsequent operations (Create, Update, Delete) participate in the transaction. Commit permanently applies all changes made since BeginTransaction. Rollback undoes all changes since the last BeginTransaction. Use these when you need to execute multiple statements atomically - if any step fails, roll back to leave the database unchanged.

Examples

GPAL Fluent: High-level fluent C# API

//BeginTransaction, Commit, and Rollback are properties that execute immediately when accessed. Store the database object with ToGPALObject() when you need to reference it across multiple statement calls.

// Wrap multiple inserts in a transaction

var db = GPAL.Database

.WithConnectionString(connStr)

.ToGPALObject();


db.WithSQLCommand("INSERT INTO Orders (CustomerId, Total) VALUES (@cid, @total)");


db.BeginTransaction

.WithParameter(customerId)

.WithParameter(orderTotal)

.Execute(out int inserted);


// If all steps succeed:

_ = db.Commit;

// On error:

// db.Rollback;

💬 Ask GPAL