tursodatabase/turso · error · InvalidOperationException

SqliteBlob requires an open connection.

Error message

SqliteBlob requires an open connection.

What it means

SqliteBlob wraps a BLOB column value as an in-memory Stream, fetched with an immediate SELECT at construction (GetBlobValue). The constructor requires the SqliteConnection to be in ConnectionState.Open because it executes that query right away; a closed or never-opened connection has nothing to execute on, so it throws InvalidOperationException before any field is assigned.

Solutions

  1. Call connection.Open() before constructing SqliteBlob - this provider never auto-opens.
  2. Construct the blob inside the connection's using/active scope, before Dispose runs.
  3. If using DI, make the registered connection lifetime cover the blob's whole usage - every Write() persists back through that connection (Persist()).

Example fix

// before
using var conn = new SqliteConnection("Data Source=db.sqlite");
var blob = new SqliteBlob(conn, "files", "data", rowId); // throws: not open

// after
using var conn = new SqliteConnection("Data Source=db.sqlite");
conn.Open();
var blob = new SqliteBlob(conn, "files", "data", rowId);
Defensive patterns

Strategy: validation

Validate before calling

if (conn.State != ConnectionState.Open)
    throw new InvalidOperationException("Open the connection before creating a SqliteBlob.");
var blob = new SqliteBlob(conn, tableName, columnName, rowId);

Try / catch

try {
    var blob = new SqliteBlob(conn, table, column, rowId);
} catch (InvalidOperationException) when (conn.State != ConnectionState.Open) {
    conn.Open(); // open late, then construct
}

Prevention

When it happens

Trigger: new SqliteBlob(conn, ...) where conn was never Open()ed; constructing the blob after the connection was Closed()/disposed (e.g., outside its using block); code that assumes SqliteConnection auto-opens like some other ADO.NET providers.

Common situations: Copy-pasted ADO.NET snippets that open the connection after first use; DI lifetimes that close a shared connection before the blob is created; refactors that move SqliteBlob construction out of the using (conn) scope; async flows where another path closes the connection between Open and construction.

Related errors


AI-assisted analysis of tursodatabase/turso@244cde92a7 (2026-08-20). Data as JSON: /api/errors/611db9455bdaf79c. Report an issue: GitHub.

Appendix: source

Thrown at bindings/dotnet/src/Turso.Data.Sqlite/SqliteBlob.cs:22

namespace Turso.Data.Sqlite;

public class SqliteBlob : Stream
{
    private readonly MemoryStream? _stream;
    private readonly SqliteConnection? _connection;
    private readonly string? _tableName;
    private readonly string? _columnName;
    private readonly long _rowId;
    private readonly bool _readOnly;
    private bool _disposed;

    public SqliteBlob(SqliteConnection connection, string tableName, string columnName, long rowId, bool readOnly = false)
    {
        ArgumentNullException.ThrowIfNull(connection);
        ArgumentNullException.ThrowIfNull(tableName);
        ArgumentNullException.ThrowIfNull(columnName);
        if (connection.State != ConnectionState.Open)
            throw new InvalidOperationException(Properties.Resources.SqlBlobRequiresOpenConnection);

        _connection = connection;
        _tableName = tableName;
        _columnName = columnName;
        _rowId = rowId;
        _readOnly = readOnly;
        _stream = new MemoryStream(GetBlobValue(connection, tableName, columnName, rowId), writable: true);
    }

    internal SqliteBlob(byte[] value)
    {
        _stream = new MemoryStream(value, writable: false);
        _readOnly = true;
    }

    public override bool CanRead => !_disposed && (_stream?.CanRead ?? false);

    public override bool CanSeek => !_disposed && (_stream?.CanSeek ?? false);

View on GitHub (pinned to 244cde92a7)