From ed258551fde3017243f39701b2ef1f114d24ad95 Mon Sep 17 00:00:00 2001 From: Thijs Koot Date: Thu, 12 Dec 2019 19:36:12 +0100 Subject: [PATCH] fix and new test for #180 --- Snowflake.Data.Tests/SFDbCommandIT.cs | 16 +++++++++++++++ Snowflake.Data/Client/SnowflakeDbCommand.cs | 22 +++++++++++++++++---- 2 files changed, 34 insertions(+), 4 deletions(-) diff --git a/Snowflake.Data.Tests/SFDbCommandIT.cs b/Snowflake.Data.Tests/SFDbCommandIT.cs index 8e241ee14..06a314ace 100755 --- a/Snowflake.Data.Tests/SFDbCommandIT.cs +++ b/Snowflake.Data.Tests/SFDbCommandIT.cs @@ -483,5 +483,21 @@ public void TestExecuteScalarNull() conn.Close(); } } + + [Test] + public void TestCreateCommandBeforeOpeningConnection() + { + using(var conn = new SnowflakeDbConnection()) + { + conn.ConnectionString = ConnectionString; + + using(var command = conn.CreateCommand()) + { + conn.Open(); + command.CommandText = "select 1"; + Assert.DoesNotThrow(() => command.ExecuteNonQuery()); + } + } + } } } diff --git a/Snowflake.Data/Client/SnowflakeDbCommand.cs b/Snowflake.Data/Client/SnowflakeDbCommand.cs index 59c18dfcc..14ac79950 100755 --- a/Snowflake.Data/Client/SnowflakeDbCommand.cs +++ b/Snowflake.Data/Client/SnowflakeDbCommand.cs @@ -37,7 +37,6 @@ public SnowflakeDbCommand(SnowflakeDbConnection connection) { logger.Debug("Constucting SnowflakeDbCommand class"); this.connection = connection; - this.sfStatement = new SFStatement(connection.SfSession); // by default, no query timeout this.CommandTimeout = 0; parameterCollection = new SnowflakeDbParameterCollection(); @@ -45,7 +44,7 @@ public SnowflakeDbCommand(SnowflakeDbConnection connection) public override string CommandText { - get; set; + get; set; } public override int CommandTimeout @@ -150,9 +149,10 @@ protected override DbTransaction DbTransaction public override void Cancel() { - sfStatement.Cancel(); + // doesn't throw exception when sfStatement is null + sfStatement?.Cancel(); } - + public override int ExecuteNonQuery() { logger.Debug($"ExecuteNonQuery, command: {CommandText}"); @@ -282,13 +282,27 @@ private static Dictionary convertToBindList(List ExecuteInternalAsync(CancellationToken cancellationToken, bool describeOnly = false) { + SetStatement(); return sfStatement.ExecuteAsync(CommandTimeout, CommandText, convertToBindList(parameterCollection.parameterList), describeOnly, cancellationToken); } }