using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Data.Common;
using System.Data;
namespace Sdaleo
{
///
/// Serves as our main Class for interacting with the DB
///
public class DB
{
private IConnectDb _ConnectionStrObj = null;
private DbConnection _DbConnection = null;
///
/// Static DB Object Creator
///
/// pass a valid Connection String Object
/// a new DB Object
public static DB Create(IConnectDb ConnectionStrObj)
{
return new DB(ConnectionStrObj);
}
#region Construction
///
/// Construct out DB Class with a valid Connection String Obj
///
/// pass a valid Connection String Object
public DB(IConnectDb ConnectionStrObj)
{
if(ConnectionStrObj == null || !ConnectionStrObj.IsValid)
throw new Exception("Invalid Connection Object Passed in.");
_ConnectionStrObj = ConnectionStrObj;
}
///
/// Private Constructor * Used for Transaction Processing *
///
/// A valid Connection Object that is in a Transaction State
private DB(DbConnection dbConnection)
{
if (dbConnection == null)
throw new Exception("Invalid Connection Passed in.");
}
#endregion
#region NonQuery
///
/// Use this to Run SQL with the ExecuteNonQuery() Command Object
///
/// SQL Text/Commands to execute
/// the result of the ExecuteNonQuery() operation
public DBRetVal ExecuteNonQuery(string SqlCommandText)
{
return ExecuteNonQuery(SqlCommandText, CommandType.Text);
}
///
/// Use this to Run SQL with the ExecuteNonQuery() Command Object
///
/// SQL Text/Commands to execute
/// the result of the ExecuteNonQuery() operation
public DBRetVal ExecuteNonQuery(string SqlCommandText, CommandType type)
{
DBRetVal retVal = new DBRetVal();
try
{
using (DbConnection conn = DBMS.CreateDbConnection(_ConnectionStrObj.DBType, _ConnectionStrObj.ConnectionString))
{
conn.Open();
using (DbCommand cmd = conn.CreateCommand())
{
cmd.CommandText = SqlCommandText;
cmd.CommandType = type;
if(_ConnectionStrObj.SupportsTimeouts)
cmd.CommandTimeout = (int) _ConnectionStrObj.Timeouts.CommandTimeout;
int nRows = cmd.ExecuteNonQuery();
retVal.SetNonQueryRetVal(nRows);
}
}
}
catch (Exception e)
{
retVal = DBMS.CreateErrorDBRetVal(_ConnectionStrObj.DBType, e);
}
return retVal;
}
///
/// Use this to Run SQL with the ExecuteNonQuery() Command Object
///
/// SQL Text/Commands to execute
/// Allows the use of DBMS Independent parameters
/// the result of the ExecuteNonQuery() operation
public DBRetVal ExecuteNonQuery(string SqlCommandText, DBMSIndParameter[] parameters)
{
return ExecuteNonQuery(SqlCommandText, CommandType.Text);
}
///
/// Use this to Run SQL with the ExecuteNonQuery() Command Object
///
/// SQL Text/Commands to execute
/// Allows the use of DBMS Independent parameters
/// the result of the ExecuteNonQuery() operation
public DBRetVal ExecuteNonQuery(string SqlCommandText, DBMSIndParameter[] parameters, CommandType type)
{
DBRetVal retVal = new DBRetVal();
try
{
using (DbConnection conn = DBMS.CreateDbConnection(_ConnectionStrObj.DBType, _ConnectionStrObj.ConnectionString))
{
conn.Open();
using (DbCommand cmd = conn.CreateCommand())
{
cmd.CommandText = SqlCommandText;
cmd.CommandType = type;
if (_ConnectionStrObj.SupportsTimeouts)
cmd.CommandTimeout = (int)_ConnectionStrObj.Timeouts.CommandTimeout;
// Add DBMS Specific Parameters
foreach (DbParameter parameter in DBMS.CreateDbParameter(_ConnectionStrObj.DBType, parameters))
cmd.Parameters.Add(parameter);
int nRows = cmd.ExecuteNonQuery();
retVal.SetNonQueryRetVal(nRows);
}
}
}
catch (Exception e)
{
retVal = DBMS.CreateErrorDBRetVal(_ConnectionStrObj.DBType, e);
}
return retVal;
}
#endregion
#region ExecuteScalar
///
/// Use this to Run SQL with the ExecuteScalar() Command Object
///
/// SQL Text to execute
/// the result of the ExecuteScalar() operation
public DBRetVal ExecuteScalar(string SqlText)
{
return ExecuteScalar(SqlText, CommandType.Text);
}
///
/// Use this to Run SQL with the ExecuteScalar() Command Object
///
/// SQL Text to execute
/// the result of the ExecuteScalar() operation
public DBRetVal ExecuteScalar(string SqlText, CommandType type)
{
DBRetVal retVal = new DBRetVal();
try
{
using (DbConnection conn = DBMS.CreateDbConnection(_ConnectionStrObj.DBType, _ConnectionStrObj.ConnectionString))
{
conn.Open();
using (DbCommand cmd = conn.CreateCommand())
{
cmd.CommandText = SqlText;
cmd.CommandType = type;
if (_ConnectionStrObj.SupportsTimeouts)
cmd.CommandTimeout = (int)_ConnectionStrObj.Timeouts.CommandTimeout;
object oResult = cmd.ExecuteScalar();
retVal.SetScalarRetVal(oResult);
}
}
}
catch (Exception e)
{
retVal = DBMS.CreateErrorDBRetVal(_ConnectionStrObj.DBType, e);
}
return retVal;
}
///
/// Use this to Run SQL with the ExecuteScalar() Command Object
///
/// SQL Text to execute
/// Allows the use of DBMS Independent parameters
/// the result of the ExecuteScalar() operation
public DBRetVal ExecuteScalar(string SqlText, DBMSIndParameter[] parameters)
{
return ExecuteScalar(SqlText, parameters, CommandType.Text);
}
///
/// Use this to Run SQL with the ExecuteScalar() Command Object
///
/// SQL Text to execute
/// Allows the use of DBMS Independent parameters
/// the result of the ExecuteScalar() operation
public DBRetVal ExecuteScalar(string SqlText, DBMSIndParameter[] parameters, CommandType type)
{
DBRetVal retVal = new DBRetVal();
try
{
using (DbConnection conn = DBMS.CreateDbConnection(_ConnectionStrObj.DBType, _ConnectionStrObj.ConnectionString))
{
conn.Open();
using (DbCommand cmd = conn.CreateCommand())
{
cmd.CommandText = SqlText;
cmd.CommandType = type;
if (_ConnectionStrObj.SupportsTimeouts)
cmd.CommandTimeout = (int)_ConnectionStrObj.Timeouts.CommandTimeout;
// Add DBMS Specific Parameters
foreach (DbParameter parameter in DBMS.CreateDbParameter(_ConnectionStrObj.DBType, parameters))
cmd.Parameters.Add(parameter);
object oResult = cmd.ExecuteScalar();
retVal.SetScalarRetVal(oResult);
}
}
}
catch (Exception e)
{
retVal = DBMS.CreateErrorDBRetVal(_ConnectionStrObj.DBType, e);
}
return retVal;
}
#endregion
#region DataTable
///
/// Use this to Run SQL with the DataAdapter Fill()
///
/// SQL Text to execute
/// the result of the DataAdapter Fill() operation
public DBRetVal FillDataTable(string SqlText)
{
return FillDataTable(SqlText, CommandType.Text);
}
///
/// Use this to Run SQL with the DataAdapter Fill()
///
/// SQL Text to execute
/// the result of the DataAdapter Fill() operation
public DBRetVal FillDataTable(string SqlText, CommandType type)
{
DBRetVal retVal = new DBRetVal();
try
{
using (DbConnection conn = DBMS.CreateDbConnection(_ConnectionStrObj.DBType, _ConnectionStrObj.ConnectionString))
{
conn.Open();
using (DbCommand cmd = conn.CreateCommand())
{
cmd.CommandText = SqlText;
cmd.CommandType = type;
if (_ConnectionStrObj.SupportsTimeouts)
cmd.CommandTimeout = (int)_ConnectionStrObj.Timeouts.CommandTimeout;
using (DbDataAdapter dataAdapter = DBMS.CreateDbDataAdapter(_ConnectionStrObj.DBType, cmd))
{
DataTable dt = new DataTable();
dataAdapter.Fill(dt);
retVal.SetDataTableRetVal(dt);
}
}
}
}
catch (Exception e)
{
retVal = DBMS.CreateErrorDBRetVal(_ConnectionStrObj.DBType, e);
}
return retVal;
}
///
/// Use this to Run SQL with the DataAdapter Fill()
///
/// SQL Text to execute
/// Allows the use of DBMS Independent parameters
/// the result of the DataAdapter Fill() operation
public DBRetVal FillDataTable(string SqlText, DBMSIndParameter[] parameters)
{
return FillDataTable(SqlText, parameters, CommandType.Text);
}
///
/// Use this to Run SQL with the DataAdapter Fill()
///
/// SQL Text to execute
/// Allows the use of DBMS Independent parameters
/// the result of the DataAdapter Fill() operation
public DBRetVal FillDataTable(string SqlText, DBMSIndParameter[] parameters, CommandType type)
{
DBRetVal retVal = new DBRetVal();
try
{
using (DbConnection conn = DBMS.CreateDbConnection(_ConnectionStrObj.DBType, _ConnectionStrObj.ConnectionString))
{
conn.Open();
using (DbCommand cmd = conn.CreateCommand())
{
cmd.CommandText = SqlText;
cmd.CommandType = type;
if (_ConnectionStrObj.SupportsTimeouts)
cmd.CommandTimeout = (int)_ConnectionStrObj.Timeouts.CommandTimeout;
// Add DBMS Specific Parameters
foreach (DbParameter parameter in DBMS.CreateDbParameter(_ConnectionStrObj.DBType, parameters))
cmd.Parameters.Add(parameter);
using (DbDataAdapter dataAdapter = DBMS.CreateDbDataAdapter(_ConnectionStrObj.DBType, cmd))
{
DataTable dt = new DataTable();
dataAdapter.Fill(dt);
retVal.SetDataTableRetVal(dt);
}
}
}
}
catch (Exception e)
{
retVal = DBMS.CreateErrorDBRetVal(_ConnectionStrObj.DBType, e);
}
return retVal;
}
#endregion
#region Transaction
///
/// Use this to run multiple DB Steps and group them into a Single Transaction
///
/// an array of Transaction Steps to execute
/// the result of Transaction
public DBRetVal StartTransaction(TransactionStep[] transactionSteps)
{
DBRetVal retVal = new DBRetVal();
try
{
DB db = new DB(_ConnectionStrObj);
// Enter Transaction
foreach (TransactionStep step in transactionSteps)
{
// Iterate thru each Transaction Step-By-Step
retVal = step(retVal, db);
// Stop The Transaction
if (retVal.ErrorOccured) // Have to Leave the Transaction as well
break;
}
// Leave Transaction
}
catch (Exception e)
{
retVal = DBMS.CreateErrorDBRetVal(_ConnectionStrObj.DBType, e);
}
return retVal;
}
#endregion
}
}