USEFUL SITES :
Write for Us
using System.Data.Common; using System.Data.SqlClient; using System.Data.OleDb; using System.Data.Odbc; using System.Data.OracleClient; using System.Collections.Generic; using System.Text;
namespace DataAccessLayer { internal class DBFactory { private static DbProviderFactory objFactory = null;
public static DbProviderFactory GetProvider(ProviderType provider) { switch (provider) { case ProviderType.SqlServer: objFactory = SqlClientFactory.Instance; break; case ProviderType.OleDb: objFactory = OleDbFactory.Instance; break; case ProviderType.Oracle: objFactory = OracleClientFactory.Instance; break; case ProviderType.ODBC: objFactory = OdbcFactory.Instance; break; } return objFactory; }
public static DbDataAdapter GetDataAdapter(ProviderType providerType) { switch (providerType) { case ProviderType.SqlServer: return new SqlDataAdapter(); case ProviderType.OleDb: return new OleDbDataAdapter(); case ProviderType.ODBC: return new OdbcDataAdapter(); case ProviderType.Oracle: return new OracleDataAdapter(); default: return null; } } } } Note that you have different providers for different databases, i.e., the database providers are all database specific. A DataAdapter as we may recall, is a bridge between the database and the DataSet – a set of disconnected data. Though you have various data readers depending on the type of the data provider you are using, you have only one type of data set. Why? This is because a data set is a disconnected in-memory set of data. The schema of the database defines the schema of the data set. Refer to the code example shown above. Both the methods in the code example shown above check the value of the enum reference instance and accordingly return an appropriate DbDataProvider or DbProviderFactory instance respectively. Such methods are actually called factory methods. Conclusion In this article we have discussed how we can design and implement and generic data access layer, i.e., one that can be used to perform database operations irrespective of the database being used. In the next article in this series, we will discuss the Databasehelper class, i.e., the class that actually performs the database operations.