The Database Manager Class represents to establish a connection to the SQL database and to perfrom CRUD(Create, Read, Update and Delete) operations. The "GetSQLConnection" method gives you the SQL connection instance and "CreateSQLDataParameter" creates data parameter. The "GetFilledDataSet" method gives you the filled dataset. The DB Manager class uses custom Sql exception. You can see the code below.
DBManager.cs:
using System; using System.Data; using System.Data.SqlClient; public class DBManager { private DBManager() { } public static IDbConnection GetSQLConnection(string connectionString) { try { IDbConnection dbConnection = null; dbConnection = new SqlConnection(connectionString); try { dbConnection.Open(); } catch (SqlException sqlEx) { CustomSqlException exCustSql = new CustomSqlException(string.Concat("Error occurred
while connecting to the SQL database. Server Name: ", ((SqlConnection)dbConnection).DataSource,
". Exception Message : ", sqlEx.Message)); throw exCustSql; } return dbConnection; } catch (Exception ex) { throw; } } public static IDbDataParameter CreateSQLDataParameter(IDbCommand idbCommand,
string parameterName, object parameterValue, DbType dbType) { return CreateSQLDataParameter(idbCommand, parameterName, parameterValue, dbType, -1); } public static IDbDataParameter CreateSQLDataParameter(IDbCommand idbCommand, string parameterName,
object parameterValue, DbType dbType, int size) { IDbDataParameter param = idbCommand.CreateParameter(); param.ParameterName = parameterName; param.DbType = dbType; if (size != -1) { param.Size = size; } param.Value = parameterValue; return param; } public static DataSet GetFilledDataSet(IDbCommand idbCommand) { IDataAdapter iDataAdapter = DBManager.CreateSQLDataAdapter(idbCommand); DataSet dataset = new DataSet(); iDataAdapter.Fill(dataset); return dataset; } public static IDataAdapter CreateSQLDataAdapter(IDbCommand idbCommand) { if (idbCommand is SqlCommand) { return new SqlDataAdapter((SqlCommand)idbCommand); } else { throw new NotSupportedException("Command Type " + idbCommand.GetType().FullName +
" are not supported!");
}
}
}
CustomSqlException.cs:
using System; public class CustomSqlException : Exception { public CustomSqlException() : base() { } public CustomSqlException(string message) : base(message) { } public CustomSqlException(string message, Exception innerException) : base(message, innerException) { } }
DatabaseSettings.cs:
using System; using System.Web.Configuration; public class DatabaseSettings { public static String ConnectionString { get { return WebConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString; } } }