tursodatabase/turso · error · InvalidOperationException

Connection must be set before executing a command.

Error message

Connection must be set before executing a command.

What it means

The synchronous Execute path requires a connection: it either delegates to the remote client or calls Prepare() against the native handle (TursoCommand.cs:268-271). A null _connection at entry throws InvalidOperationException before any SQL is touched.

Source

Thrown at bindings/dotnet/src/Turso.Data/TursoCommand.cs:277

        }

        return sql;
    }

    private static bool ParsePragmaEnabled(string value)
    {
        value = value.Trim('\'', '"');
        return long.TryParse(value, out var number)
            ? number != 0
            : value.Equals("ON", StringComparison.OrdinalIgnoreCase)
              || value.Equals("TRUE", StringComparison.OrdinalIgnoreCase)
              || value.Equals("YES", StringComparison.OrdinalIgnoreCase);
    }

    private DbDataReader Execute(CommandBehavior behavior = CommandBehavior.Default)
    {
        if (_connection is null)
            throw new InvalidOperationException("Connection must be set before executing a command.");

        if (_connection.IsRemote)
            return ExecuteRemoteAsync(behavior, CancellationToken.None).GetAwaiter().GetResult();

        IDisposable? syncOperation = _connection.EnterSyncOperation();
        try
        {
            PrepareCore();

            var statement = _statement ?? throw new InvalidOperationException("Command was not prepared.");
            _statement = null;
            try
            {
                var reader = new TursoDataReader(this, statement, behavior, syncOperation);
                syncOperation = null;
                return reader;
            }
            catch

View on GitHub (pinned to c1e5928725)

Solutions

  1. Assign the connection before executing: cmd.Connection = conn; (after conn.Open()).
  2. Create commands via the connection: using var cmd = conn.CreateCommand();
  3. In shared helpers, null-check cmd.Connection and fail with your own clearer error message.

Example fix

// before
var cmd = new TursoCommand("SELECT 1");
using var reader = cmd.ExecuteReader(); // no Connection -> throws

// after
using var conn = new TursoConnection(cs);
conn.Open();
using var cmd = conn.CreateCommand();
cmd.CommandText = "SELECT 1";
using var reader = cmd.ExecuteReader();
Defensive patterns

Strategy: validation

Validate before calling

if (cmd.Connection is not TursoConnection)
    throw new InvalidOperationException("Bind an open TursoConnection to the command before executing.");

Prevention

When it happens

Trigger: var cmd = new TursoCommand("SELECT 1"); cmd.ExecuteReader(); — ExecuteReader/ExecuteNonQuery/ExecuteScalar on a command whose Connection was never assigned or was set to null.

Common situations: Forgetting conn.CreateCommand() and newing the command directly; a command created in one scope and reused after its connection was unbound; unit tests exercising commands without a live connection.

Related errors


AI-assisted analysis of tursodatabase/turso@c1e5928725 (2026-08-20). Data as JSON: /api/errors/5d3ede63386dbf5b. Report an issue: GitHub.