{"record":{"id":"611db9455bdaf79c","repo":"tursodatabase/turso","slug":"sqliteblob-requires-an-open-connection","errorCode":null,"errorMessage":"SqliteBlob requires an open connection.","messagePattern":"SqliteBlob requires an open connection\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"bindings/dotnet/src/Turso.Data.Sqlite/SqliteBlob.cs","lineNumber":22,"sourceCode":"namespace Turso.Data.Sqlite;\n\npublic class SqliteBlob : Stream\n{\n    private readonly MemoryStream? _stream;\n    private readonly SqliteConnection? _connection;\n    private readonly string? _tableName;\n    private readonly string? _columnName;\n    private readonly long _rowId;\n    private readonly bool _readOnly;\n    private bool _disposed;\n\n    public SqliteBlob(SqliteConnection connection, string tableName, string columnName, long rowId, bool readOnly = false)\n    {\n        ArgumentNullException.ThrowIfNull(connection);\n        ArgumentNullException.ThrowIfNull(tableName);\n        ArgumentNullException.ThrowIfNull(columnName);\n        if (connection.State != ConnectionState.Open)\n            throw new InvalidOperationException(Properties.Resources.SqlBlobRequiresOpenConnection);\n\n        _connection = connection;\n        _tableName = tableName;\n        _columnName = columnName;\n        _rowId = rowId;\n        _readOnly = readOnly;\n        _stream = new MemoryStream(GetBlobValue(connection, tableName, columnName, rowId), writable: true);\n    }\n\n    internal SqliteBlob(byte[] value)\n    {\n        _stream = new MemoryStream(value, writable: false);\n        _readOnly = true;\n    }\n\n    public override bool CanRead => !_disposed && (_stream?.CanRead ?? false);\n\n    public override bool CanSeek => !_disposed && (_stream?.CanSeek ?? false);","sourceCodeStart":4,"sourceCodeEnd":40,"githubUrl":"https://github.com/tursodatabase/turso/blob/244cde92a7df7f9b8b8b7a4075c35a12977e303e/bindings/dotnet/src/Turso.Data.Sqlite/SqliteBlob.cs#L4-L40","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Call connection.Open() before constructing SqliteBlob - this provider never auto-opens.","Construct the blob inside the connection's using/active scope, before Dispose runs.","If using DI, make the registered connection lifetime cover the blob's whole usage - every Write() persists back through that connection (Persist())."],"exampleFix":"// before\nusing var conn = new SqliteConnection(\"Data Source=db.sqlite\");\nvar blob = new SqliteBlob(conn, \"files\", \"data\", rowId); // throws: not open\n\n// after\nusing var conn = new SqliteConnection(\"Data Source=db.sqlite\");\nconn.Open();\nvar blob = new SqliteBlob(conn, \"files\", \"data\", rowId);","handlingStrategy":"validation","validationCode":"if (conn.State != ConnectionState.Open)\n    throw new InvalidOperationException(\"Open the connection before creating a SqliteBlob.\");\nvar blob = new SqliteBlob(conn, tableName, columnName, rowId);","typeGuard":null,"tryCatchPattern":"try {\n    var blob = new SqliteBlob(conn, table, column, rowId);\n} catch (InvalidOperationException) when (conn.State != ConnectionState.Open) {\n    conn.Open(); // open late, then construct\n}","preventionTips":["Open SqliteConnection explicitly; it does not auto-open.","Keep blob usage inside the connection's using scope - SqliteBlob writes back on every Write.","Centralize 'open + assert State == Open' in one helper so no path forgets."],"tags":["dotnet","ado-net","sqlite-blob","connection-state"],"backgroundTag":"connection-not-open","analyzedSha":"244cde92a7df7f9b8b8b7a4075c35a12977e303e","analyzedAt":"2026-08-20T07:02:18.389Z","contentChangedAt":"2026-08-20T07:02:18.389Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}