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