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

Exception Handling in 3-Tier Architecture

,
Rate me:
Please Sign up or sign in to vote.
4.76/5 (61 votes)
15 Oct 2010CPOL5 min read 275.5K   8.1K   235  
Exception handling in 3-Tier Architecture using Enterprise Library
using System;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.Common;
using System.Configuration;
using System.Collections.Generic;
using Microsoft.Practices.EnterpriseLibrary.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Common.Helpers.ExceptionHandling;

namespace Common.Helpers.DataAccess
{
   public static class SqlHelper
   {
      private static string AppDatabaseName;

      static SqlHelper()
      {
         AppDatabaseName = ConfigurationManager.AppSettings["AppDatabaseName"];
      }

      private static Database GetDatabase(string dbName)
      {
         try
         {
            Database db = null;
            if (dbName == null)
            {
               return db = DatabaseFactory.CreateDatabase();
            }
            else
            {
               return db = DatabaseFactory.CreateDatabase(dbName);
            }
            return db;
         }
         catch (Exception ex)
         {
            throw ex;
         }
      }

      public static DataTable GetDataTable(string spName)
      {
         try
         {
            Database db = GetDatabase(AppDatabaseName);
            DbCommand cmd = db.GetStoredProcCommand(spName);
            DataSet ds = db.ExecuteDataSet(cmd);
            DataTable dt = ds.Tables[0];
            return dt;
         }
         catch (Exception ex)
         {
            bool rethrow = DataAccessExceptionHandler.HandleException(ref ex);
            if (rethrow)
            {
               throw ex;
            }
            return null;
         }
      }

      public static DataTable GetDataTable(string spName, object[] parameters)
      {
         try
         {
            Database db = GetDatabase(AppDatabaseName);
            DbCommand cmd = db.GetStoredProcCommand(spName, parameters);
            DataSet ds = db.ExecuteDataSet(cmd);
            DataTable dt = ds.Tables[0];
            return dt;
         }
         catch (Exception ex)
         {
            bool rethrow = DataAccessExceptionHandler.HandleException(ref ex);
            if (rethrow)
            {
               throw ex;
            }
            return null;
         }
      }

      public static IDataReader GetDataReader(string spName)
      {
         try
         {
            Database db = GetDatabase(AppDatabaseName);
            IDataReader reader = db.ExecuteReader(spName);
            return reader;
         }
         catch (Exception ex)
         {
            bool rethrow = DataAccessExceptionHandler.HandleException(ref ex);
            if (rethrow)
            {
               throw ex;
            }
            return null;
         }
      }

      public static IDataReader GetDataReader(string spName, object[] parameters)
      {
         try
         {
            Database db = GetDatabase(AppDatabaseName);
            IDataReader reader = db.ExecuteReader(spName, parameters);
            return reader;
         }
         catch (Exception ex)
         {
            bool rethrow = DataAccessExceptionHandler.HandleException(ref ex);
            if (rethrow)
            {
               throw ex;
            }
            return null;
         }
      }

      public static object ExecuteNonQuery(string spName, object[] inParameters, bool outParameters)
      {
         try
         {
            Database db = GetDatabase(AppDatabaseName);
            DbCommand cmd = db.GetStoredProcCommand(spName, inParameters);
            db.ExecuteNonQuery(cmd);
            if (outParameters)
            {
               return (object)(cmd.Parameters["Outparam"].Value);
            }
            return null;
         }
         catch (Exception ex)
         {
            bool rethrow = DataAccessExceptionHandler.HandleException(ref ex);
            if (rethrow)
            {
               throw ex;
            }
            return null;
         }
      }
   }
}

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
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions