private void SelectTables() { var profiler = MiniProfiler.Current; var bareFactory = DbProviderFactories.GetFactory("System.Data.SqlClient"); var providerFactory = new ProfiledDbProviderFactory(profiler, bareFactory); var connStr = ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString; var bareConnection = new SqlConnection(connStr); var profiledConnection = new ProfiledDbConnection(bareConnection, profiler); var profiledCommand = providerFactory.CreateCommand(); profiledCommand.Connection = profiledConnection; profiledCommand.CommandText = "SELECT * FROM sys.tables"; using (profiler.Step("Open Connection")) { profiledConnection.Open(); } using (profiler.Step("ExecuteNonQuery")) { profiledCommand.ExecuteNonQuery(); } }
public IDbConnection GetConnection() { var connection = new SqlConnection(_connectionString); var profiledConnection = new ProfiledDbConnection(connection, MiniProfiler.Current); profiledConnection.Open(); return profiledConnection; }
/// <summary> /// Gets an open READ UNCOMMITTED connection using the specified connection string, optionally timing out on the initial connect /// </summary> /// <param name="connectionString">The connection string to use for the connection</param> /// <param name="connectionTimeout">Milliseconds to wait to connect, optional</param> /// <returns>A READ UNCOMMITTED connection to the specified connection string</returns> public static DbConnection GetOpen(string connectionString, int? connectionTimeout = null) { var conn = new ProfiledDbConnection(new MySqlConnection(connectionString), MiniProfiler.Current); Action<DbConnection> setReadUncommitted = c => { using (var cmd = c.CreateCommand()) { cmd.CommandText = "SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED"; cmd.ExecuteNonQueryAsync(); } }; if (connectionTimeout.GetValueOrDefault(0) == 0) { conn.OpenAsync(); setReadUncommitted(conn); } else { // In the case of remote monitoring, the timeout will be at the NIC level, not responding to traffic, // in that scenario, connection timeouts don't really do much, because they're never reached, the timeout happens // before their timer starts. Because of that, we need to spin up our own overall timeout using (MiniProfiler.Current.Step("Opening Connection, Timeout: " + conn.ConnectionTimeout)) try { conn.Open(); } catch (SqlException e) { var csb = new MySqlConnectionStringBuilder(connectionString); var sqlException = string.Format("Error opening connection to {0} at {1} timeout was: {2} ms", csb.Server, csb.Database, connectionTimeout.ToString()); throw new Exception(sqlException, e); } setReadUncommitted(conn); if (conn.State == ConnectionState.Connecting) { var b = new MySqlConnectionStringBuilder { ConnectionString = connectionString }; throw new TimeoutException("Timeout expired connecting to " + b.Server + " on " + b.Database + " on in the alloted " + connectionTimeout.Value.ToString() + " ms"); } } return conn; }
protected System.Data.Common.DbCommand CreateDbCommand() { var connectionString = Constants.AdventureWorksConnectionString; var sqlConnection = new SqlConnection(connectionString); var connection = new StackExchange.Profiling.Data.ProfiledDbConnection (sqlConnection, MiniProfiler.Current); Request.RegisterForDispose(connection); var command = connection.CreateCommand(); Request.RegisterForDispose(command); Logger.Log(new LogEventInfo(LogLevel.Error,"Controller","Connection Returned.")); connection.Open(); return command; }
/// <summary> /// The EF code first. /// </summary> /// <returns>the entity framework code first view.</returns> public ActionResult EFCodeFirst() { int count; int? newCount = null; EFContext context = null; using (MiniProfiler.Current.Step("EF Stuff")) { try { using (MiniProfiler.Current.Step("Create Context")) context = new EFContext(); // this is not correct, as the count from this assignment is never actually used using (MiniProfiler.Current.Step("First count")) count = context.People.Count(); using (MiniProfiler.Current.Step("Insertion")) { var p = new Person { Name = "sam" }; context.People.Add(p); context.SaveChanges(); } // this count is actually used. using (MiniProfiler.Current.Step("Second count")) count = context.People.Count(); const string sql = "Select count(*) from People"; using (MiniProfiler.Current.Step("Get Count from SqlQuery Method - no sql recorded")) { newCount = context.Database.SqlQuery<int>(sql).Single(); } using (MiniProfiler.Current.Step("Get Count using ProfiledConnection - sql recorded")) { using (var conn = new ProfiledDbConnection(context.Database.Connection, MiniProfiler.Current)) { conn.Open(); newCount = conn.Query<int>(sql).Single(); conn.Close(); } } } finally { if (context != null) { context.Dispose(); } } } return Content(string.Format("EF Code First complete - count: {0}, sqlQuery count {1}", count, newCount)); }