# C# / .NET

Connect to ParticleDB from C# and .NET, run parameterized queries, handle transactions and errors.

Source: https://particledb.ai/sdks/csharp/

Use the configured address for your client. See [connection addresses](/installation/ports/) and [security](/guides/security/). The examples use the SQL client address unless they explicitly select the HTTP API or ParticleDB JDBC driver.

A native ParticleDB package for .NET is not published yet. Use
[Npgsql](https://www.npgsql.org/) over the SQL client interface (port 5432).

## Install

```bash
dotnet add package Npgsql
```

## Required connection setting

Add `Server Compatibility Mode=NoTypeLoading` to the connection string. Without it, opening
the first connection fails while Npgsql loads type information, with
`42883: operator does not exist: integer = text`.

## Quick start

The example connects as `postgres` with the password `secret`, for a server started with
`particledb start --auth-method scram-sha-256 --pg-password secret`. With the server's default
`--auth-method password`, add `SSL Mode=Require`; see
[TLS and authentication](/connectivity/tls/#authentication).

```csharp
using Npgsql;

var connStr = "Host=localhost;Port=5432;Username=postgres;Password=secret;" +
              "Database=particledb;Server Compatibility Mode=NoTypeLoading";
await using var dataSource = NpgsqlDataSource.Create(connStr);

await using (var cmd = dataSource.CreateCommand(
    "CREATE TABLE users (id BIGINT PRIMARY KEY, name TEXT)"))
{
    await cmd.ExecuteNonQueryAsync();
}

await using (var cmd = dataSource.CreateCommand(
    "INSERT INTO users (id, name) VALUES ($1, $2)"))
{
    cmd.Parameters.Add(new NpgsqlParameter { Value = 1L });
    cmd.Parameters.Add(new NpgsqlParameter { Value = "Alice" });
    Console.WriteLine(await cmd.ExecuteNonQueryAsync());   // 1
}

// Transaction
await using (var conn = await dataSource.OpenConnectionAsync())
await using (var tx = await conn.BeginTransactionAsync())
{
    await using var cmd = new NpgsqlCommand(
        "INSERT INTO users (id, name) VALUES ($1, $2)", conn, tx);
    cmd.Parameters.Add(new NpgsqlParameter { Value = 2L });
    cmd.Parameters.Add(new NpgsqlParameter { Value = "Bob" });
    await cmd.ExecuteNonQueryAsync();
    await tx.CommitAsync();
}

await using (var cmd = dataSource.CreateCommand(
    "SELECT id, name FROM users WHERE id >= $1 ORDER BY id"))
{
    cmd.Parameters.Add(new NpgsqlParameter { Value = 1L });
    await using var reader = await cmd.ExecuteReaderAsync();
    while (await reader.ReadAsync())
        Console.WriteLine($"{reader.GetInt64(0)} {reader.GetString(1)}");
}
```

Output:

```
1
1 Alice
2 Bob
```

Key-value operations and vector search are SQL, so you run them with `NpgsqlCommand` like any
other statement, for example `SELECT KV_GET($1)`. See [Vector Search](/sql/vector-search/).

## Next steps

- [SQL Reference](/sql/overview/). SQL syntax and examples
- [ORM compatibility](/connectivity/orm-compatibility/). Entity Framework Core and other ORMs
- [SQL client connections](/connectivity/pg-wire/). connection settings and compatibility
