Click here to Skip to main content
15,895,370 members
Articles / Web Development / ASP.NET

Code without Code Behind

Rate me:
Please Sign up or sign in to vote.
4.50/5 (2 votes)
4 Dec 2010CPOL13 min read 24.4K   420   16  
Developing a web based event calendar in ASP.NET using MVP Design Pattern
// 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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
Australia Australia
I am a Senior Software Developer / Technical Consultant in a leading software company.

Comments and Discussions