First things first, let's talk about the Connection Strings. The connection string format in the configuration file is the following:
<connectionStrings> <add name="DataConnectionString" connectionString="Provider=Microsoft.Jet.OLEDB.4.0; Data Source=|DataDirectory|\AdventureWorks.mdb" providerName="System.Data.OleDb" /> </connectionStrings>
The parts of the System.Configuration.ConnectionStringSettings object are:
NOTE: Before demonstrating the data provider factory connection method, it's important to note that if you are never going to switch between providers (Oracle, MS SQL Server, MySQL, OleDB), then the provider factory method can be skipped and you can simply use the connection string with its corresponding provider (ex: new SqlConnection(ConnectionString)).
The following code illustrates how to create a DB connection using the data provider classes. Notice that this connection method provides an approach to getting a connection from an already created DbProviderFactory. The idea is to create a collection of already initialized providers, then all you are calling at runtime is the GetConnection() method of the DataManager class. Additionally, IMO, I believe the returned connection should be stored in a local variable and reused rather than disposed.
using System; using System.Collections.Generic; using System.Configuration; using System.Data.Common; public static class DataManager { public static Dictionary<string, DataConnection> _dbConnections; static DataManager() { // initialize the db factories _dbConnections = new Dictionary<string, DataConnection>( ConfigurationManager.ConnectionStrings.Count); foreach (ConnectionStringSettings cstring in ConfigurationManager.ConnectionStrings) { // initialize a new DbProviderFactory DbProviderFactory factory = DbProviderFactories.GetFactory(cstring.ProviderName); // Initialize a new DataConnection object DataConnection connection = new DataConnection(cstring.ConnectionString, factory); // add the DataConnection to the dictionary // names will be unique because they are unique for // the ConnectionStringSettingsCollection object _dbConnections.Add(cstrings.Name.ToLower(), connection); } } /// <summary> /// Gets a connection using the configuration /// connection string name. /// </summary> /// <param name="connectionStringConfigName"> /// The name of the connection in the configuration file. /// </param> /// <returns> /// An instantiated DbConnection with the applied connection string. /// </returns> public static DbConnection GetConnection( string connectionStringConfigName) { if (!_dbConnections.ContainsKey( connectionStringConfigName.ToLower())) throw new ConfigurationException( "The data provider could not be initialized. "+ "Please recheck the configuration file."); DataConnection cn = _dbConnections[ connectionStringConfigName.ToLower()]; return cn.CreateConnection(); } private class DataConnection { string _connectionString; DbProviderFactory _dbFactory; internal DataConnection( string connectionString, DbProviderFactory factory) { _connectionString = connectionString; _dbFactory = factory; } public DbConnection CreateConnection() { DbConnection connection = _dbFactory.CreateConnection(); connection.ConnectionString = ConnectionString; return connection; } } }
}
If you like this approach, I am going to go on about data connections in a future "Part 2" blog entry. I will go over the idea of creating a data provider for multiple data sources using an abstract factory provider.
this is exactly the idea i was thinking about. however, the code shown above gets an "Inconsistent Accessibility" error.