# C# (Dapper - Npgsql)

> For the complete documentation index, see [llms.txt](https://docs.redpanda.com/llms.txt). Component-specific: [cloud-data-platform-full.txt](https://docs.redpanda.com/cloud-data-platform-full.txt)

---
title: C# (Dapper - Npgsql)
latest-operator-version: v26.1.4
latest-console-tag: v3.7.3
latest-connect-version: 4.93.0
latest-redpanda-tag: v26.1.9
docname: connect-to-sql/language-clients/dotnet-dapper
page-component-name: cloud-data-platform
page-version: master
page-component-version: master
page-component-title: Cloud
page-relative-src-path: connect-to-sql/language-clients/dotnet-dapper.adoc
page-edit-url: https://github.com/redpanda-data/cloud-docs/edit/main/modules/sql/pages/connect-to-sql/language-clients/dotnet-dapper.adoc
description: Connect to Redpanda SQL from C# using the Dapper ORM and the Npgsql PostgreSQL provider.
page-topic-type: how-to
page-git-created-date: "2026-05-26"
page-git-modified-date: "2026-05-26"
---

<!-- Source: https://docs.redpanda.com/cloud-data-platform/sql/connect-to-sql/language-clients/dotnet-dapper.md -->

[Dapper](https://dappertutorial.net/) is an object-relational mapper (ORM) for .NET that runs SQL directly and maps results to C# objects with minimal setup. This page describes how to use Dapper with [Npgsql](https://www.npgsql.org/), a PostgreSQL data provider for .NET, to connect to Redpanda SQL.

## [](#establishing-connection)Establishing connection

There are two ways to establish a connection through [Npgsql](https://www.npgsql.org/):

-   Npgsql’s DataSource class

    ```csharp
    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

    ```csharp
    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](https://www.npgsql.org/doc/connection-string-parameters.html).

## [](#example-usage)Example usage

This example shows basic query execution for the following C# class, once the connection has been established:

```csharp
public class Customer
{
    public int ClientId { get; set; }
    public double Height { get; set; }
    public string FirstName { get; set; }
}
```

```csharp
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.");
}
```

> ⚠️ **WARNING**
>
> The `INSERT INTO Customer VALUES (@ClientId, @Height, @FirstName)` syntax uses prepared statements internally. Redpanda SQL does not support prepared statements. It translates incoming binary input back into a string, so prepared statements provide no security or performance benefits.

## [](#unsupported-functions-and-structures)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 for `DELETE`, `UPDATE`, `INSERT INTO …​ (SELECT)` and `COPY` statements

-   `connection.BeginTransaction` - [Transactions](https://www.npgsql.org/doc/basic-usage.html#transactions)

-   `CommandType.StoredProcedure` - [Stored Procedures](https://www.npgsql.org/doc/basic-usage.html#stored-functions-and-procedures)

-   [Function in/out parameters](https://www.npgsql.org/doc/basic-usage.html#function-inout-parameters)