C# / .NET
Use the configured address for your client. See connection addresses and 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 over the SQL client interface (port 5432).
Install
Section titled “Install”dotnet add package NpgsqlRequired connection setting
Section titled “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
Section titled “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.
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}
// Transactionawait 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:
11 Alice2 BobKey-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.
Next steps
Section titled “Next steps”- SQL Reference. SQL syntax and examples
- ORM compatibility. Entity Framework Core and other ORMs
- SQL client connections. connection settings and compatibility
This site uses Google Analytics cookies to count visits. Privacy · License