Programming C#

Postgre SQL

Database

Create Database

CREATE DATABASE EStore;

Create Table

CREATE TABLE Category(
	CategoryId SERIAL NOT NULL PRIMARY KEY,
	CategoryName VARCHAR(128) NOT NULL
);

Insert Data

INSERT INTO Category (CategoryName) VALUES ('Laptop'), ('Mouse');

Code C#

Add Package from nuget Npgsql

dotnet add package Npgsql

Connect C# to Postgres SQL

IDbConnection connection = new NpgsqlConnection(connectionString);
IDbCommand command = connection.CreateCommand();
command.CommandText = "SELECT * FROM Category";
connection.Open();
IDataReader reader = command.ExecuteReader();
while(reader.Read()){
    System.Console.WriteLine("{0}, {1}", reader["CategoryId"], reader["CategoryName"]);
}
reader.Dispose();
command.Dispose();
connection.Dispose();