Nik's Technology Blog

Travels through programming, networks, and computers

Enumerate Available Database Providers in ASP.NET

Using the DbProviderFactories class in ADO.NET you can retrieve a list of available database factories using the GetFactoryClasses method. This can be useful if you host your ASP.NET site on a shared server and don't have access to the web server and machine.config file.

The output of the GetFactoryClasses method is a DataTable of available factory classes, these are the factory classes that the .NET runtime will have access to.

Here's the kind of output you'll get...

NameDescriptionInvariantNameAssemblyQualifiedName
Odbc Data Provider .Net Framework Data Provider for Odbc System.Data.Odbc System.Data.Odbc.OdbcFactory, System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
OleDb Data Provider .Net Framework Data Provider for OleDb System.Data.OleDb System.Data.OleDb.OleDbFactory, System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
OracleClient Data Provider .Net Framework Data Provider for Oracle System.Data.OracleClient System.Data.OracleClient.OracleClientFactory, System.Data.OracleClient, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
SqlClient Data Provider .Net Framework Data Provider for SqlServer System.Data.SqlClient System.Data.SqlClient.SqlClientFactory, System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
Here's the code you'll need in your code behind file.

DataTable providersList = null;
providersList = System.Data.Common.DbProviderFactories.GetFactoryClasses();
GridView providerGridView = new GridView();
providerGridView.DataSource = providersList;
providerGridView.DataBind();
form1.Controls.Add(providerGridView);
Comments are closed