From def7a5cc6f7950fe4ca631fcaf8bc02ef5bccb9b Mon Sep 17 00:00:00 2001 From: bubibubi Date: Tue, 19 Sep 2017 16:02:31 +0200 Subject: [PATCH] Fix to Open StateChange event call --- src/System.Data.Jet/JetConnection.cs | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/src/System.Data.Jet/JetConnection.cs b/src/System.Data.Jet/JetConnection.cs index 65ecc0e..5bd13ff 100644 --- a/src/System.Data.Jet/JetConnection.cs +++ b/src/System.Data.Jet/JetConnection.cs @@ -107,8 +107,8 @@ namespace System.Data.Jet _state = ConnectionState.Closed; if (InnerConnection != null) { - InnerConnectionFactory.Instance.CloseConnection(_ConnectionString, InnerConnection); InnerConnection.StateChange -= WrappedConnection_StateChange; + InnerConnectionFactory.Instance.CloseConnection(_ConnectionString, InnerConnection); } InnerConnection = null; OnStateChange(new StateChangeEventArgs(ConnectionState.Open, ConnectionState.Closed)); @@ -300,20 +300,27 @@ namespace System.Data.Jet public bool TableExists(string tableName) { - if (State != ConnectionState.Open) - throw new InvalidOperationException(Messages.CannotCallMethodInThisConnectionState(nameof(TableExists), ConnectionState.Open, State)); + ConnectionState oldConnectionState = State; + bool tableExists; + + if (oldConnectionState == ConnectionState.Closed) + Open(); try { - string sqlFormat = "select top 1 * from [{0}]"; + string sqlFormat = "select count(*) from [{0}] where 1=2"; CreateCommand(String.Format(sqlFormat, tableName)).ExecuteNonQuery(); - return true; + tableExists = true; } catch { - return false; + tableExists = false; } + if (oldConnectionState == ConnectionState.Closed) + Close(); + + return tableExists; } public DbCommand CreateCommand(string commandText, int? commandTimeout = null)