using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.IO;
namespace BusinessLayer.Helpers
{
public class Database : IDisposable
{
private String strConn;
private String strSQL;
private SqlConnection conn = null;
private SqlCommand cmd = null;
private SqlDataAdapter da = null;
private List lstParams = null;
//Constructors
public Database()
{
strConn = ConfigurationManager.ConnectionStrings["AppConnectionString"].ConnectionString.ToString();
}
public Database(String SQL)
{
strConn = ConfigurationManager.ConnectionStrings["AppConnectionString"].ConnectionString.ToString();
strSQL = SQL;
}
//AddParam
public void AddParam(String paramName, String paramValue)
{
if (lstParams == null)
{
lstParams = new List();
}
try
{
if (paramName.Substring(0, 1) != "@")
{
paramName = "@" + paramName;
}
lstParams.Add(new SqlParameter(paramName, paramValue));
}
catch (Exception ex)
{
Email.sendErrorEmail(ex, "Error Adding Parameter to Database Object");
throw new ArgumentException();
}
}
//ResetParams
public void ResetParams()
{
lstParams = null;
}
//Data_Table - Stored Procedure
public DataTable StoredProcedure_DataTable(String StoredProcedureName, out Boolean succeed)
{
succeed = false;
DataTable dt = new DataTable();
try
{
using (conn = new SqlConnection(strConn))
{
using (cmd = new SqlCommand(StoredProcedureName, conn))
{
cmd.CommandType = CommandType.StoredProcedure;
foreach (SqlParameter param in lstParams)
{
cmd.Parameters.Add(param);
}
cmd.Connection.Open();
using (SqlDataReader sdr = cmd.ExecuteReader())
{
dt.Load(sdr);
succeed = true;
}
}
}
}
catch (Exception ex)
{
String sql = StoredProcedureName + " ";
foreach(SqlParameter param in lstParams)
{
sql += param.ParameterName + ": " + param.Value.ToString() + " ";
}
Email.sendErrorEmail(ex, "Stored Procedure: DataTable - Error", "SQL Passed", sql);
throw new Exception();
}
finally
{
cmd.Connection.Close();
Dispose();
}
return dt;
}
public DataTable StoredProcedure_DataTable(String StoredProcedureName)
{
Boolean Silencer;
return StoredProcedure_DataTable(StoredProcedureName, out Silencer);
}
//DataReader - Stored Procedure
//? TODO:
//?
//?
//?
//Scalar - Stored Procedure
public String StoredProcedure_Scalar(String StoredProcedureName, out Boolean succeed)
{
succeed = false;
String strScalar = "";
try
{
using (conn = new SqlConnection(strConn))
{
using (cmd = new SqlCommand(StoredProcedureName, conn))
{
cmd.CommandType = CommandType.StoredProcedure;
foreach (SqlParameter param in lstParams)
{
cmd.Parameters.Add(param);
}
cmd.Connection.Open();
strScalar = cmd.ExecuteScalar().ToString();
succeed = true;
}
}
}
catch (Exception ex)
{
String sql = StoredProcedureName + " ";
foreach (SqlParameter param in lstParams)
{
sql += param.ParameterName + ": " + param.Value.ToString() + " ";
}
Email.sendErrorEmail(ex, "Stored Procedure: Scalar - Error", "SQL Passed", sql);
throw new Exception();
}
finally
{
cmd.Connection.Close();
Dispose();
}
return strScalar;
}
public String StoredProcedure_Scalar(String StoredProcedureName)
{
Boolean Silencer;
return StoredProcedure_Scalar(StoredProcedureName, out Silencer);
}
//Non Query - Stored Procedure
public void StoredProcedure_NonQuery(String StoredProcedureName, out Boolean succeed)
{
succeed = false;
try
{
using (conn = new SqlConnection(strConn))
{
using (cmd = new SqlCommand(StoredProcedureName, conn))
{
cmd.CommandType = CommandType.StoredProcedure;
foreach (SqlParameter param in lstParams)
{
cmd.Parameters.Add(param);
}
cmd.Connection.Open();
cmd.ExecuteNonQuery();
succeed = true;
}
}
}
catch (Exception ex)
{
String sql = StoredProcedureName + " ";
foreach (SqlParameter param in lstParams)
{
sql += param.ParameterName + ": " + param.Value.ToString() + " ";
}
Email.sendErrorEmail(ex, "Stored Procedure: Non Query - Error", "SQL Passed", sql);
throw new Exception();
}
finally
{
cmd.Connection.Close();
Dispose();
}
}
public void StoredProcedure_NonQuery(String StoredProcedureName)
{
Boolean Silencer;
StoredProcedure_NonQuery(StoredProcedureName, out Silencer);
}
//IDisposable method
public void Dispose()
{
if (da != null)
{
da.Dispose();
da = null;
}
if (cmd != null)
{
cmd.Connection.Close();
cmd.Dispose();
cmd = null;
}
if (conn != null)
{
conn.Dispose();
conn = null;
}
ResetParams();
}
} //End of Database Class
} //End of Helpers NameSpace
|