Summary: in this tutorial, you will learn how to perform a transaction in PostgreSQL using C#.
First, open a terminal and connect to the elearning
database using the ed
user using psql
program:
Second, create a new table called invoices
:
Third, insert five rows into the courses
table:
Output:
Finally, exit the psql program:
The following program illustrates how to enroll a student in a course and create an invoice for the enrollment within a transaction:
First, open a terminal and connect to the elearning
database using the ed
user:
It’ll prompt you to enter a password for the ed
user. Input the valid password and press Enter to connect to the PostgreSQL.
Second, retrieve data from the enrollments
table:
Output:
Third, retrieve data from the invoices
table:
Output:
- Call the
BeginTransactionAsync()
method of the NpgsqlConnection
object to start a transaction.
- Call the
CommitAsync()
method of the NpgsqlTransaction
object to apply the changes since the transaction started to the database permanently.
- Call the
RollbackAsync()
method of the NpgsqlTransaction
object to roll back the changes.