using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Transactions;
using System.Configuration;
using System.Data.SqlTypes;
using DumbCompany.Diagnostics.Exceptions.Services;
using DumbCompany.Diagnostics.Logging;
namespace DumbCompany.DataSource.Base
{
///
/// This class serves as the base for all data controller objects, providing
/// access to the defined data sources.
///
public abstract class BaseDataSourceController
{
#region Enumerations
///
/// Specify the execution type of the database function passed.
///
protected enum ExecutionType
{
///
/// The command to be executed is a Read command.
///
Read,
///
/// The command to be executed is an Insert command.
///
Insert,
///
/// The command to be executed is an Update command.
///
Update,
///
/// The command to be executed is a Delete command.
///
Delete
}
#endregion
#region Constants
///
/// This is the name of the connection string configuration settings
/// for connecting to a Sql database.
///
protected const string CONNECTION_STRING_SQL = "SqlDatabaseConnection";
///
/// The name of the table containing the primary results of the executed
/// command.
///
protected const string TABLE_PRIMARY = "PrimaryResults";
#endregion
#region Protected Members
protected string _sqlDatabaseConnectionString = string.Empty;
///
/// The connection to the Sql database.
///
protected SqlConnection _sqlConnection = null;
#endregion
#region Constructor
///
/// Default constructor.
///
public BaseDataSourceController()
{
//call method to setup the connection string(s)
setupConnectionStrings();
}
///
/// This method will retreive all strings from the configuration file.
///
private void setupConnectionStrings()
{
ConnectionStringSettings settings = null;
//Sql connection string
settings = ConfigurationManager.ConnectionStrings[CONNECTION_STRING_SQL];
if (settings != null)
{
_sqlDatabaseConnectionString = settings.ConnectionString;
}
}
#endregion
#region Sql Execution
///
/// Execute a Sql Command against the Sql Database.
///
///
/// The command to be executed.
///
/// The type of command being executed.
///
/// Populated dataset with results of the command's execution.
protected DataSet sqlCommandExecute(SqlCommand sqlCommand, ExecutionType executionType)
{
//write the command data for logging
Information.WriteCommandInfo(sqlCommand);
//TEST: throw exception
//Error.Write(new Exception("Testing error logging to event log."));
//throw new ApplicationException("Testing thrown exception logging.");
//instantiate return object for filling
DataSet dataSet = new DataSet();
dataSet.Tables.Add(TABLE_PRIMARY);
//configure the dataset for performance
dataSet.EnforceConstraints = false;
try
{
//create connection and assign it to the command
_sqlConnection = new SqlConnection(_sqlDatabaseConnectionString);
sqlCommand.Connection = _sqlConnection;
//check type of command
if (executionType == ExecutionType.Read)
{
//configure the table for performance - begin load
dataSet.Tables[0].BeginLoadData();
//create adapter and fill the dataset
SqlDataAdapter sqlAdapter = new SqlDataAdapter(sqlCommand);
sqlAdapter.Fill(dataSet.Tables[0]);
//end load of data in table
dataSet.Tables[0].EndLoadData();
}
else if (executionType == ExecutionType.Update ||
executionType == ExecutionType.Insert ||
executionType == ExecutionType.Delete)
{
//open the connection
_sqlConnection.Open();
//execute the function
sqlCommand.ExecuteNonQuery();
//TODO: implement functionality to return updated or inserted data
//into dataset for resulting object population
}
}
catch (Exception ex)
{
//throw a data source exception with the command details
throw new DataSourceException(DataSourceException.ParseFailedCommand(sqlCommand, ex), ex);
}
finally
{
//close and dispose of the connection
if (_sqlConnection != null)
{
_sqlConnection.Close();
_sqlConnection.Dispose();
}
}
//return the completed data set
return dataSet;
}
///
/// Execute a Sql Command against the Sql Database which is expected to return
/// XML data..
///
///
/// The command to be executed.
///
/// The type of command being executed.
///
/// Populated SqlXml object with results of the command's execution.
protected SqlXml sqlXmlCommandExecute(SqlCommand sqlCommand, ExecutionType executionType)
{
//write the command data for logging
Information.WriteCommandInfo(sqlCommand);
//instantiate return object
SqlXml xmlData = null;
try
{
//create connection and assign it to the command
_sqlConnection = new SqlConnection(_sqlDatabaseConnectionString);
sqlCommand.Connection = _sqlConnection;
//check type of command
if (executionType == ExecutionType.Read)
{
//xml data is to be returned - open the connection and execute it
_sqlConnection.Open();
SqlDataReader dataReader = sqlCommand.ExecuteReader();
if (dataReader.Read())
{
if (!dataReader.IsDBNull(0))
xmlData = dataReader.GetSqlXml(0);
}
}
else if (executionType == ExecutionType.Update ||
executionType == ExecutionType.Insert ||
executionType == ExecutionType.Delete)
{
//TODO: implement when necessary
throw new NotImplementedException("XmlCommand execution for Update, Insert and Delete is not implemented.");
}
}
catch (Exception ex)
{
//throw a data source exception with the command details
throw new DataSourceException(DataSourceException.ParseFailedCommand(sqlCommand, ex), ex);
}
finally
{
//close and dispose of the connection
if (_sqlConnection != null)
{
_sqlConnection.Close();
_sqlConnection.Dispose();
}
}
//return the completed data set
return xmlData;
}
#endregion
}
}