19PostgreSQL in C#

This example demonstrates the PostgreSQL database in a C# application. If you skipped Chapter 18, which built a similar example in Python, go to that chapter and read the beginning and the first two main sections, which are described in the following list.

  • “Install PostgreSQL” explains how to install the PostgreSQL database.
  • “Run pgAdmin” tells you how to use the pgAdmin tool to build the database that this example uses.

When you reach the section “Create the Program” in Chapter 18, come back here and read the following sections.

CREATE THE PROGRAM

To create a C# program to work with the Brew Crew database, create a new C# Console App (.NET Framework) and then add the code described in the following sections.

Like the program described in Chapter 17, “MariaDB in C#,” this example's main method calls other methods to do the actual work. This time the main method creates the database connection and passes it to the other methods so that they don't need to open their own connections.

Install Npgsql

This example uses the Npgsql data provider to allow your program to communicate with the Postgres engine. To add Npgsql to your project, follow these steps.

  1. Open the Project menu and select Manage NuGet Packages.
  2. Click the Browse tab and in the search box enter Npgsql. This will bring up around 200 packages.
  3. Find the package named Npgsql, click it, and then use the Install button on the right to install it. This shouldn't take too long, but you might still ...

Get Beginning Database Design Solutions, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.