Back to Turso

Turso .NET

bindings/dotnet/Readme.md

0.7.0-mikael3.9 KB
Original Source

Turso .NET

ADO.NET bindings for Turso local databases.

The managed Turso package exposes System.Data.Common types such as DbConnection, DbCommand, DbDataReader, DbParameter, DbTransaction, and DbProviderFactory. The Turso.Raw package contains the native SDK-kit interop layer and runtime assets used by the provider.

Install

bash
dotnet add package Turso

Turso depends on Turso.Raw, so application code usually only needs to reference Turso.

Getting started

C#
using Turso;

using var connection = new TursoConnection("Data Source=:memory:");
connection.Open();

connection.ExecuteNonQuery("CREATE TABLE t(a, b)");
var rowsAffected = connection.ExecuteNonQuery("INSERT INTO t(a, b) VALUES (1, 2), (3, 4)");
Console.WriteLine($"RowsAffected: {rowsAffected}");

using var command = connection.CreateCommand();
command.CommandText = "SELECT * FROM t";
using var reader = command.ExecuteReader();
while (reader.Read())
{
    var a = reader.GetInt32(0);
    var b = reader.GetInt32(1);
    Console.WriteLine($"Value1: {a}, Value2: {b}");
}

ADO.NET usage

Code written against DbConnection can use TursoConnection directly:

C#
using System.Data.Common;
using Turso;

await using DbConnection connection = new TursoConnection("Data Source=app.db");
connection.Open();

await using var command = connection.CreateCommand();
command.CommandText = "SELECT $value";
var parameter = command.CreateParameter();
parameter.ParameterName = "$value";
parameter.Value = 42;
command.Parameters.Add(parameter);

var value = command.ExecuteScalar();

Provider factories are available through TursoFactory.Instance:

C#
DbProviderFactory factory = TursoFactory.Instance;
using var connection = factory.CreateConnection();
connection!.ConnectionString = "Data Source=:memory:";
connection.Open();

Migrating from Microsoft.Data.Sqlite

For common embedded SQLite usage, Turso.Data.Sqlite exposes a SQLite-compatible facade over the Turso engine:

diff
- using Microsoft.Data.Sqlite;
+ using Turso.Data.Sqlite;

- using var connection = new SqliteConnection("Data Source=app.db");
+ using var connection = new SqliteConnection("Data Source=app.db");

Supported common connection string keywords include:

KeywordNotes
Data SourceDatabase path or :memory:. Aliases include DataSource and Filename.
ModeParsed and preserved for compatibility.
CacheParsed and preserved for compatibility.
Foreign KeysParsed and preserved for compatibility.
Recursive TriggersParsed and preserved for compatibility.
Default TimeoutUsed as the default command timeout. Aliases include Command Timeout.
PoolingParsed and preserved for compatibility.
VfsParsed and preserved for compatibility.
Encryption CipherTurso local encryption cipher.
Encryption KeyHex-encoded encryption key used with Encryption Cipher.

SQLite-compatible facade coverage

  • Turso.Data.Sqlite is the migration-oriented facade. It includes SQLite-style connection strings, commands, readers, schema metadata, transactions and savepoints, backup, SQL-backed blob streams, scalar and aggregate UDFs, custom collations, and disabled-by-default extension loading.
  • Raw SQLitePCL sqlite3* handle interop is intentionally unsupported. SqliteConnection.Handle returns null rather than exposing a fake SQLite handle.
  • PRAGMA read_uncommitted is tracked as connection-local state for API compatibility, but Turso does not currently implement SQLite shared-cache dirty reads.
  • SqliteBlob preserves fixed-length blob stream behavior through SQL reads and writes. It is not yet backed by a native incremental-blob storage handle.
  • SQLite virtual-table modules such as FTS3/FTS5 are not built in unless provided by a Turso extension/module.
  • Async methods currently use the base ADO.NET behavior rather than a dedicated async native path.