Using MiniProfiler with SqlDataSource ASP.NET WebForms Control
Aug 8, 2012 • Chris Pietschmann • ASP.NET • C#I recently implemented MiniProfiler into an existing ASP.NET WebForms application that makes use of data binding to the SqlDataSource
control. Since the SqlDataSource
uses a DbProviderFactory
internally, it is fairly simple to extend the control to utilize MiniProfiler
through inheritance and overriding a single method of the SqlDataSource
.
Here’s a very simple class that inherits from the SqlDataSource
control and injects MiniProfiler
support to be able to profile the SQL query used by the control:
public class ProfiledSqlDataSource : SqlDataSource
{
protected override DbProviderFactory GetDbProviderFactory()
{
// get the "base" DbProviderFactory
var baseDbProviderFactory = base.GetDbProviderFactory();
// Return a ProfiledDbProviderFactory from MiniProfiler
// that wraps the "base" DbProviderFactory
return new ProfiledDbProviderFactory(
MiniProfiler.Current,
baseDbProviderFactory
);
}
}

Chris Pietschmann
DevOps & AI Architect | Microsoft MVP | HashiCorp Ambassador | MCT | Developer | Author
I am a DevOps & AI Architect, developer, trainer and author. I have nearly 25 years of experience in the Software Development industry that includes working as a Consultant and Trainer in a wide array of different industries.