public async Task ExecuteScalarAsync_works_on_closed_or_opened_connection()
 {
     using( var cmd = new SqlCommand( "select count(*) from sys.objects" ) )
     using( var ctx = new SqlStandardCallContext() )
     {
         ISqlConnectionController c = ctx[TestHelper.GetConnectionString()];
         c.Connection.State.Should().Be( ConnectionState.Closed );
         using( c.ExplicitOpen() )
         {
             c.Connection.State.Should().Be( ConnectionState.Open );
             ((int)c.ExecuteScalar( cmd )).Should().BeGreaterThan( 0 );
         }
         c.Connection.State.Should().Be( ConnectionState.Closed );
         ((int)await c.ExecuteScalarAsync( cmd )).Should().BeGreaterThan( 0 );
         c.Connection.State.Should().Be( ConnectionState.Closed );
         cmd.CommandText = "select count(*) from sys.objects where name='no-object-here'";
         using( await c.ExplicitOpenAsync() )
         {
             c.Connection.State.Should().Be( ConnectionState.Open );
             ((int)await c.ExecuteScalarAsync( cmd )).Should().Be( 0 );
         }
         c.Connection.State.Should().Be( ConnectionState.Closed );
         ((int)await c.ExecuteScalarAsync( cmd )).Should().Be( 0 );
         c.Connection.State.Should().Be( ConnectionState.Closed );
         cmd.CommandText = "select name from sys.tables where name='no-object-here'";
         using( await c.ExplicitOpenAsync() )
         {
             (await c.ExecuteScalarAsync( cmd )).Should().BeNull();
         }
         c.Connection.State.Should().Be( ConnectionState.Closed );
     }
 }
        public static int ExecuteNonQuery(this ISqlConnectionController @this, SqlCommand cmd)
        {
            var ctx = @this.SqlCallContext;

            using (@this.ExplicitOpen())
            {
                return(ctx.Executor.ExecuteQuery(ctx.Monitor, @this.Connection, @this.Transaction, cmd, c => c.ExecuteNonQuery()));
            }
        }
        public static T ExecuteQuery <T>(this ISqlConnectionController @this, SqlCommand cmd, Func <SqlCommand, T> innerExecutor)
        {
            var ctx = @this.SqlCallContext;

            using (@this.ExplicitOpen())
            {
                return(ctx.Executor.ExecuteQuery(ctx.Monitor, @this.Connection, @this.Transaction, cmd, innerExecutor));
            }
        }
 public void ExplicitOpen_and_Dispose_are_order_independent()
 {
     using (var ctx = new SqlStandardCallContext(TestHelper.Monitor))
     {
         ISqlConnectionController c = ctx[TestHelper.GetConnectionString()];
         c.Connection.State.Should().Be(ConnectionState.Closed);
         var d1 = c.ExplicitOpen();
         c.Connection.State.Should().Be(ConnectionState.Open);
         var d2 = c.ExplicitOpen();
         var d3 = c.ExplicitOpen();
         d1.Should().NotBeNull();
         d2.Should().NotBeNull();
         d3.Should().NotBeNull();
         c.Connection.State.Should().Be(ConnectionState.Open);
         d1.Dispose();
         d1.Dispose();
         d2.Dispose();
         c.Connection.State.Should().Be(ConnectionState.Open);
         d3.Dispose();
         c.Connection.State.Should().Be(ConnectionState.Closed);
     }
 }
            static async Task Do200Prints(ThreadSafeTraceCounter t)
            {
                var m = new ActivityMonitor(false);

                m.Output.RegisterClient(t);
                using (var ctx = new SqlStandardCallContext(m))
                {
                    ISqlConnectionController c = ctx[TestHelper.GetConnectionString()];
                    c.ExplicitOpen();
                    var cmd = c.Connection.CreateCommand();
                    cmd.CommandText = "print 'Here I am: a print.';";
                    for (int i = 0; i < 200; ++i)
                    {
                        await cmd.ExecuteNonQueryAsync();

                        Thread.Sleep(5);
                    }
                }
            }
        public void InfoMessage_are_monitored_when_SqlHelper_LogSqlServerInfoMessage_is_set_to_true()
        {
            SqlHelper.LogSqlServerInfoMessage = true;
            IReadOnlyList <ActivityMonitorSimpleCollector.Entry> entries = null;
            var m = new ActivityMonitor(false);

            // The messages are Traced but the monitor level is temporarily set to LogFilter.Trace:
            // Even if the monitor should not catch them, info messages traces will be emitted.
            m.MinimalFilter = LogFilter.Release;
            using (m.CollectEntries(logs => entries = logs, LogLevelFilter.Debug))
                using (m.Output.CreateBridgeTo(TestHelper.Monitor.Output.BridgeTarget))
                    using (var ctx = new SqlStandardCallContext(m))
                    {
                        ISqlConnectionController c = ctx[TestHelper.GetConnectionString()];
                        c.ExplicitOpen();
                        var cmd = c.Connection.CreateCommand();
                        cmd.CommandText = "print 'Here I am: a print.';";
                        cmd.ExecuteNonQuery();
                    }
            entries.Any(e => e.Text.Contains("Here I am: a print.")).Should().BeTrue();
            SqlHelper.LogSqlServerInfoMessage = false;
        }
        public void Direct_open_close_of_the_connection_is_possible()
        {
            void DoSomething(SqlConnection con)
            {
                bool wasClosed = con.State == ConnectionState.Closed;

                if (wasClosed)
                {
                    con.Open();
                }
                var cmd = con.CreateCommand();

                cmd.CommandText = "select 1;";
                cmd.ExecuteScalar().Should().Be(1);
                if (wasClosed)
                {
                    con.Close();
                }
            }

            using (var ctx = new SqlStandardCallContext(TestHelper.Monitor))
            {
                ISqlConnectionController c = ctx[TestHelper.GetConnectionString()];

                // Closed.
                DoSomething(c.Connection);
                c.Connection.State.Should().Be(ConnectionState.Closed);

                using (c.ExplicitOpen())
                {
                    c.Connection.State.Should().Be(ConnectionState.Open);
                    DoSomething(c.Connection);
                    c.Connection.State.Should().Be(ConnectionState.Open);
                }
                c.Connection.State.Should().Be(ConnectionState.Closed);
            }
        }
 public static void PreOpen(this ISqlConnectionController ctx)
 {
     ctx.ExplicitOpen();
 }