C# (Dapper - Npgsql)
Establishing connection
There are two ways to establish a connection through Npgsql:
-
Npgsql’s DataSource class
var connectionString = "Server=127.0.0.1:5432;Username=user;Password=password;Database=db;"; var dataSource = NpgsqlDataSource.Create(connectionString); var connection = dataSource.OpenConnection(); -
Creating connection directly
var connectionString = "Server=127.0.0.1:5432;Username=user;Password=password;Database=db;"; var connection = new NpgsqlConnection(connectionString); connection.Open();
For more details on connection string options, including SSL configuration, see the Npgsql docs.
Example usage
This example shows basic query execution for the following C# class, once the connection has been established:
public class Customer
{
public int ClientId { get; set; }
public double Height { get; set; }
public string FirstName { get; set; }
}
connection.Execute("CREATE TABLE Customer (ClientId INTEGER, Height DOUBLE, FirstName TEXT)");
var customer = new Customer{ClientId = 1, Height = 3.14, FirstName = "John"};
connection.Execute("INSERT INTO Customer VALUES (@ClientId, @Height, @FirstName)", customer);
var customers = connection.Query<Customer>("SELECT * FROM Customer");
foreach(var c in customers)
{
Console.WriteLine($"Customer #{c.ClientId}: {c.FirstName} is {c.Height} tall.");
}
|
The |
Unsupported functions and structures
The following functions and potentially related structures are either not supported or work incorrectly when combining Redpanda SQL and Dapper-Npgsql:
-
connection.Execute- returns improper number of rows forDELETE,UPDATE,INSERT INTO … (SELECT)andCOPYstatements -
connection.BeginTransaction- Transactions -
CommandType.StoredProcedure- Stored Procedures