diff --git a/DuckDB.NET.Data/DuckDBCommand.cs b/DuckDB.NET.Data/DuckDBCommand.cs index 548c5747..48fa3720 100644 --- a/DuckDB.NET.Data/DuckDBCommand.cs +++ b/DuckDB.NET.Data/DuckDBCommand.cs @@ -164,7 +164,11 @@ public async IAsyncEnumerable ExecuteArrowBatchesAsync([EnumeratorC } } - public override void Prepare() { } + /// + /// Statements are prepared when the command executes, so this only checks what IDbCommand.Prepare documents: + /// InvalidOperationException when "The Connection is not set. -or- The Connection is not Open()." + /// + public override void Prepare() => EnsureConnectionOpen(); protected override DbParameter CreateDbParameter() => new DuckDBParameter(); diff --git a/DuckDB.NET.Test/DuckDBCommandTests.cs b/DuckDB.NET.Test/DuckDBCommandTests.cs index 1b58d831..1b1b31c2 100644 --- a/DuckDB.NET.Test/DuckDBCommandTests.cs +++ b/DuckDB.NET.Test/DuckDBCommandTests.cs @@ -24,4 +24,17 @@ public void CommandTypeDefaultsToText() { new DuckDBCommand().CommandType.Should().Be(CommandType.Text); } + + // https://learn.microsoft.com/dotnet/api/system.data.idbcommand.prepare, Exceptions: InvalidOperationException, + // "The Connection is not set. -or- The Connection is not Open()." + [Fact] + public void PrepareThrowsWithoutAnOpenConnection() + { + new DuckDBCommand("SELECT 1").Invoking(c => c.Prepare()).Should().Throw(); + + using var closed = new DuckDBConnection(DuckDBConnectionStringBuilder.InMemoryConnectionString); + new DuckDBCommand("SELECT 1", closed).Invoking(c => c.Prepare()).Should().Throw(); + + new DuckDBCommand("SELECT 1", Connection).Invoking(c => c.Prepare()).Should().NotThrow(); + } }