// Author - Anshu Dutta // email - anshu.dutta@gmail.com using System; using System.Data; using System.Data.Common; using System.Configuration; namespace EventCalendar.DataAccessLayer { public class DatabaseConnection:IDatabaseConnection { private string _providerName = ConfigurationManager.AppSettings["DataAccessLayer.ProviderName"]; private string _connectionString = ConfigurationManager.AppSettings["DataAccessLayer.ConnectionString"]; private DbProviderFactory _providerFactory; private IDbConnection _connection; public DatabaseConnection() { _providerFactory = DbProviderFactories.GetFactory(_providerName); OpenConnection(); } private void OpenConnection() { _connection = _providerFactory.CreateConnection(); _connection.ConnectionString = _connectionString; _connection.Open(); } private void CloseConnection() { _connection.Close(); } System.Data.IDbConnection IDatabaseConnection.GetConnection { get { return _connection; } } System.Data.IDbCommand IDatabaseConnection.CreateCommand(string dynamicSQLExpression, CommandTypes cmdType) { IDbCommand command = _connection.CreateCommand(); command.CommandText = dynamicSQLExpression; switch (cmdType) { case CommandTypes.StoredProcedure: command.CommandType = CommandType.StoredProcedure; break; case CommandTypes.Query: command.CommandType = CommandType.Text; break; default: break; } return command; } IDbDataAdapter IDatabaseConnection.CreateAdapter(IDbCommand command) { IDbDataAdapter da = _providerFactory.CreateDataAdapter(); da.SelectCommand = command; return da; } void IDisposable.Dispose() { CloseConnection(); _connection.Dispose(); } } }
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.
This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)