public void Error_Is_Captured(string masterConnectionString)
        {
            using (SqlConnection con = new SqlConnection(masterConnectionString))
            {
                if (con.Manage().IsAzure)
                {
                    Console.WriteLine("Tracing for Azure is not yet implemented");
                    return;
                }
            }

            using (SqlConnection con = new SqlConnection(masterConnectionString))
            {
                Console.WriteLine($"Version of [{masterConnectionString}]: {con.Manage().ShortServerVersion}");
            }


            TraceTetsEnv env = new TraceTetsEnv(masterConnectionString);

            using (env)
            {
                string sql = "Select 42 / 0;";
                sql = sql + sql + sql;
                using (SqlTraceReader reader = new SqlTraceReader())
                {
                    Console.WriteLine($@"
Master Connection: {env.MasterConnectionString}
TraceDir:          {env.TraceDirectory}
TableName:         {env.TableName}");

                    reader.Start(env.MasterConnectionString, env.TraceDirectory,
                                 TraceColumns.Sql | TraceColumns.ClientProcess, TraceRowFilter.CreateByClientProcess(Process.GetCurrentProcess().Id));

                    Exception caught = null;
                    try
                    {
                        using (SqlConnection con = new SqlConnection(masterConnectionString))
                        {
                            con.Open();
                            using (SqlCommand cmd = new SqlCommand(sql, con))
                            {
                                cmd.ExecuteNonQuery();
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        // Console.WriteLine("Divide by zero IS REQUIRED: " + ex);
                        caught = ex;
                    }

                    reader.Stop();
                    var detailsReport = reader.ReadDetailsReport();
                    DumpCounters(detailsReport);
                    Assert.AreEqual(1, detailsReport.Count, "Exactly one statement is expected");
                    Assert.Greater(detailsReport.Count, 0, "At least one sql command should be caught");

                    foreach (SqlStatementCounters report in detailsReport)
                    {
                        if (report.SqlErrorCode != 8134)
                        {
                            Assert.Fail("SQL ERROR 8134 expected. Caught Exception is " + caught);
                        }
                    }
                }
            }
        }
        public void RowCounts_Of_Insert(string masterConnectionString)
        {
            using (SqlConnection con = new SqlConnection(masterConnectionString))
            {
                if (con.Manage().IsAzure)
                {
                    Console.WriteLine("Tracing for Azure is not yet implemented");
                    return;
                }
            }

            using (SqlConnection con = new SqlConnection(masterConnectionString))
            {
                Console.WriteLine($"Version of [{masterConnectionString}]: {con.Manage().ShortServerVersion}");
            }


            var table = $"#T_{Guid.NewGuid().ToString("N")}";

            string[] sqlCommands = new[]
            {
                $"Create Table {table}(id int);",
                $"Insert {table} Values(42); Insert {table} Values(43); Insert {table} Values(44); Insert {table} Values(45);",
            };

            TraceTetsEnv env = new TraceTetsEnv(masterConnectionString);

            using (env)
            {
                using (SqlTraceReader reader = new SqlTraceReader())
                {
                    Console.WriteLine($@"
Master Connection: {env.MasterConnectionString}
TraceDir:          {env.TraceDirectory}
TableName:         {env.TableName}");

                    reader.Start(env.MasterConnectionString, env.TraceDirectory,
                                 TraceColumns.Sql | TraceColumns.ClientProcess,
                                 TraceRowFilter.CreateByClientProcess(Process.GetCurrentProcess().Id));

                    using (SqlConnection con = new SqlConnection(masterConnectionString))
                    {
                        con.Open();
                        foreach (var sql in sqlCommands)
                        {
                            using (SqlCommand cmd = new SqlCommand(sql, con))
                            {
                                cmd.ExecuteNonQuery();
                            }
                        }
                    }

                    Console.WriteLine($"Trace File: {reader.TraceFile}");
                    reader.Stop();
                    var detailsReport = reader.ReadDetailsReport();
                    DumpCounters(detailsReport);
                    Assert.Greater(detailsReport.Count, 0, "At least one sql command should be caught");
                    var rowCountsOfLastStatements = detailsReport.Last().Counters.RowCounts;
                    Assert.AreEqual(4, rowCountsOfLastStatements, "Insert 4x should result RowCounts==4");
                }
            }
        }
        public void Single_SqlBatch_Is_Captured(string masterConnectionString)
        {
            using (SqlConnection con = new SqlConnection(masterConnectionString))
            {
                if (con.Manage().IsAzure)
                {
                    Console.WriteLine("Tracing for Azure is not yet implemented");
                    return;
                }
            }

            using (SqlConnection con = new SqlConnection(masterConnectionString))
            {
                Console.WriteLine($"Version of [{masterConnectionString}]: {con.Manage().ShortServerVersion}");
            }


            TraceTetsEnv env = new TraceTetsEnv(masterConnectionString);

            using (env)
            {
                string sql = "Set NOCOUNT ON; SELECT @@version, 'Hello, World!'; Exec sp_server_info;";
                sql = sql + sql + sql;
                using (SqlTraceReader reader = new SqlTraceReader())
                {
                    Console.WriteLine($@"
Master Connection: {env.MasterConnectionString}
TraceDir:          {env.TraceDirectory}
TableName:         {env.TableName}");

                    reader.Start(env.MasterConnectionString, env.TraceDirectory,
                                 TraceColumns.Sql | TraceColumns.ClientProcess);

                    using (SqlConnection con = new SqlConnection(masterConnectionString))
                    {
                        con.Open();
                        using (SqlCommand cmd = new SqlCommand(sql, con))
                        {
                            cmd.ExecuteNonQuery();
                        }
                    }

                    reader.Stop();
                    var detailsReport = reader.ReadDetailsReport();
                    DumpCounters(detailsReport);
                    Assert.Greater(detailsReport.Count, 0, "At least one sql command should be caught");

                    int idProcess = Process.GetCurrentProcess().Id;
                    foreach (SqlStatementCounters report in detailsReport)
                    {
                        if (report.SqlErrorCode.HasValue)
                        {
                            Assert.Fail("All the statement are successful, but '{0}' produces error {1}", report.Sql,
                                        report.SqlErrorCode);
                        }

                        if (report.Sql == sql && report.ClientProcess == idProcess)
                        {
                            return;
                        }
                    }

                    Assert.Fail("Expected sql statement {0} by process {1}", sql, idProcess);
                }
            }
        }