|
using System;
using System.Data;
using System.Data.Common;
using System.Data.Sql;
using System.Data.SqlTypes;
using System.Data.SqlClient;
using System.Configuration;
using System.Web;
using System.Web.Configuration;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
/// <summary>
/// Top Layer For Accessing Data
/// </summary>
public sealed class DataAccess
{
#region Data Members And Objects Declaration
readonly string connectionString;
readonly string provider;
FactoryGoverner governer;
SQLFactory sqlFactory = new SQLFactory();
ProviderAgnosticFactory pAFactory = new ProviderAgnosticFactory();
SqlDataReader dR;
DbDataReader dbDR;
#endregion
#region Constructor For DataAccess
public DataAccess(string configSectionName)
{
connectionString = WebConfigurationManager.ConnectionStrings[configSectionName].ToString();
provider = WebConfigurationManager.ConnectionStrings[configSectionName].ProviderName;
}
public DataAccess(string conStr, string providerName)
{
connectionString = conStr;
provider = providerName;
}
#endregion
#region Private Methods
private void PrepareEnvironment(QueryType queryType)
{
try
{
switch (provider)
{
case "System.Data.SqlClient":
governer = sqlFactory;
governer.InvokeFactory(FactoryCodes.SQLClient, connectionString, queryType);
break;
case "System.Data.OracleClient":
governer = pAFactory;
governer.InvokeFactory(FactoryCodes.OracleClient, connectionString, queryType);
break;
case "System.Data.OleDb":
governer = pAFactory;
governer.InvokeFactory(FactoryCodes.OleDB, connectionString, queryType);
break;
case "System.Data.Odbc":
governer = pAFactory;
governer.InvokeFactory(FactoryCodes.ODBC, connectionString, queryType);
break;
default:
throw new UnsupportedFactoryException("Database Is Unsupported");
}
}
catch (NullReferenceException ex)
{
}
catch (UnsupportedFactoryException ex)
{
}
}
#endregion
#region Data Access Public Methods
public DataSet GetDataSet(QueryType queryType, string query, params AgnosticParameter[] argumentsIn)
{
PrepareEnvironment(queryType);
return governer.GetDataSet(query, argumentsIn);
}
public SqlDataReader GetSQLReader(QueryType queryType, string query, params AgnosticParameter[] argumentsIn)
{
PrepareEnvironment(queryType);
try
{
if (provider != "System.Data.SqlClient")
throw new UnsupportedFactoryException("Method Not For Non SQL Client");
else
return (SqlDataReader)governer.GetReader(query, argumentsIn);
}
catch (InvalidCastException ex)
{
}
return dR;
}
public DbDataReader GetReader(QueryType queryType, string query, params AgnosticParameter[] argumentsIn)
{
PrepareEnvironment(queryType);
try
{
if (provider == "System.Data.SqlClient")
throw new UnsupportedFactoryException("Method Not For SQL Client");
else
return (DbDataReader)governer.GetReader(query, argumentsIn);
}
catch (InvalidCastException ex)
{
}
return dbDR;
}
public object GetScalarResult(QueryType queryType, string query, params AgnosticParameter[] argumentsIn)
{
PrepareEnvironment(queryType);
return governer.GetScalarResult(query, argumentsIn);
}
public int GetNonQueryResult(QueryType queryType, string query, params AgnosticParameter[] argumentsIn)
{
PrepareEnvironment(queryType);
return governer.GetNonQueryResult(query, argumentsIn);
}
public DataSet GetOutParameters(QueryType queryType, string sp_Name, ref AgnosticParameter[] argumentsOut, params AgnosticParameter[] argumentsIn)
{
PrepareEnvironment(queryType);
return governer.GetOutParameters(sp_Name, ref argumentsOut, argumentsIn);
}
#endregion
}
|
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.
Rahul has worked on several technologies in Microsoft's technology stack. He has been in field of software developemnt for two years. He started with ASP.net and graduated to Windows application develeopment. His technology experience includes C#, LINQ, T-SQL,T-SQL, XML, PL\SQL, C, C++, VB, WebServices..
Apart from all these he is working on some of the most recent technology offerings such as Windows Azure, SQL Azure, SQL Azure Sync framework, MS Dallas, Powershell etc.
His goal of knowledge is to possess knowledge that is sky high and earth deep.