- using System;
- using System.Data;
- using System.Configuration;
- public class DB {
- public static IDbConnection GetConnection() {
- Type cnType = Type.GetType(ConfigurationSettings.AppSettings("connectionType"));
- DbConnection cn = (IDbConnection) Activator.CreateInstance(cnType);
- cn.ConnectionString = ConfigurationSettings.AppSettings("connectionString");
- return cn;
- }
- }
Any application which uses above class must provide following entries in its .config file.
- <appSettings>
- <add key="connectionType" value="System.Data.SqlClient.SqlConnection, System.Data, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
- <add key="connectionString" value="server=your_host;database=your_db;uid=your_uid;pwd=your_pwd" />
- </appSettings>
This approach is flexible because no recompilation is needed if a new provider is to be utilized.