Ways to implement Nested Transactions in Nested Methods - c#

assuming i have a class like below, how can i implement something that would make all the called Methods inside MasterMethod (MethodA, MethodB, and MethodC) inherit the MySqlTransaction Object that was instantiated from MasterMethod?
private MySqlConnection OpenConnection() {
try {
MySqlConnection DbConn = new MySqlConnection("~connectionstring");
DbConn.Open();
return DbConn;
} catch(Exception Ex) {
throw Ex;
}
}
public void MasterMethod() {
using(MySqlConnection DbConn = OpenConnection()) {
using(MySqlTransaction DbTrans = DbConn.BeginTransaction()) {
try {
MethodA();
MethodB(Param1);
MethodC(Param1, Param2);
DbTrans.Commit();
} catch(Exception Ex) {
DbTrans.Rollback();
throw Ex;
}
}
}
}
public void MethodA() {
using(MySqlConnection DbConn = OpenConnection()) {
using(MySqlTransaction DbTrans = DbConn.BeginTransaction()) {
try {
// Lots and Lots of things to do
DbTrans.Commit();
} catch(Exception Ex) {
DbTrans.Rollback();
throw Ex;
}
}
}
}
public void MethodB(int Param1) {
using(MySqlConnection DbConn = OpenConnection()) {
using(MySqlTransaction DbTrans = DbConn.BeginTransaction()) {
try {
// Lots and Lots of things to do
DbTrans.Commit();
} catch(Exception Ex) {
DbTrans.Rollback();
throw Ex;
}
}
}
}
public void MethodC(string Param1, string Param2) {
using(MySqlConnection DbConn = OpenConnection()) {
using(MySqlTransaction DbTrans = DbConn.BeginTransaction()) {
try {
// Lots and Lots of things to do
DbTrans.Commit();
} catch(Exception Ex) {
DbTrans.Rollback();
throw Ex;
}
}
}
}
With my current architecture, queries are immediately committed right after a specific Method's block has reached its end instead of waiting for the DbTrans.Commit() on the bottom most part of MasterMethod's try block.
How can i make it (the MySqlTransaction Object) to behave in such a way that it must wait for all 3 nested/called Methods (MethodA, MethodB, MethodC) before committing (or rolling back) the changes to the Database ?

Hope the below code would help you:
You have to use 2 different connection object in order to maintain states (as per your requirement). I have posted a general DB connection code and you can use it on your own way.
public class ConnectionClass
{
public string ConnectionString = ConfigurationManager.ConnectionStrings["xyz"].ConnectionString;
public SqlConnection conTrans = new SqlConnection();
public SqlTransaction dbTrans;
public SqlConnection sqlCon = new SqlConnection(ConfigurationManager.ConnectionStrings["xyz"].ConnectionString);
public SqlTransaction sqlTrans;
public bool BeginConTrans()
{
try
{
conTrans.ConnectionString = ConnectionString;
conTrans.Open();
dbTrans = conTrans.BeginTransaction();
return true;
}
catch (Exception ex)
{
return false;
}
}
public bool CommitConTrans()
{
try
{
dbTrans.Commit();
conTrans.Close();
return true;
}
catch (Exception ex)
{
return false;
}
}
public bool RollbackConTrans()
{
try
{
dbTrans.Rollback();
conTrans.Close();
return true;
}
catch (Exception ex)
{
return false;
}
}
public bool ExecuteNonQueryTrans(string proc, SqlParameter[] par)
{
SqlCommand cmd = new SqlCommand();
try
{
if (par != null)
{
for (int i = 0; i <= par.Length - 1; i++)
{
cmd.Parameters.Add(par[i]);
}
}
cmd.Connection = conTrans;
cmd.Transaction = dbTrans;
cmd.CommandText = proc;
cmd.CommandType = CommandType.StoredProcedure;
cmd.ExecuteNonQuery();
return true;
}
catch (Exception ex)
{
return false;
}
}
}
EDIT -how to use it in your logic
public void MasterMethod()
{
try
{
BeginConTrans();
// your methods A,B,C
//must use ExecuteNonQueryTrans for your get/put data
}
catch (Exception ex)
{
//if any method fails handle exception and rollback the transcation
RollbackConTrans();
}
CommitConTrans(); // if success ,commit the transcation
}

Related

System.ArgumentNullException: 'Value cannot be null. Parameter name: providerInvariantName'

I'm trying to create a c# application using N- Tier architecture.
This is my business layer method to insert a record in database.
public static bool AddEmployeeBL(Employee newEmployee)
{
bool employeeAdded = false;
try
{
if(ValidateEmployee(newEmployee))
{
EmployeeDAL employeeDAL = new EmployeeDAL();
employeeAdded = employeeDAL.AddEmployeeDAL(newEmployee);
}
}
catch (MyException ex)
{
throw ex;
}
catch (Exception ex)
{
throw ex;
}
return employeeAdded;
}
The exception occurs at the throw ex statement in last catch block.
The DAL class method is as follows, here's the method to add data.
public bool AddEmployeeDAL(Employee newEmployee)
{
bool employeeAdded = false;
try
{
DbCommand command = DataConnection.CreateCommand();
command.CommandText = "uspAddEmployee";
DbParameter param = command.CreateParameter();
param.ParameterName = "#UserId";
param.DbType = DbType.Int32;
param.Value = newEmployee.UserId;
command.Parameters.Add(param);
param = command.CreateParameter();
param.ParameterName = "#Name";
param.DbType = DbType.String;
param.Value = newEmployee.Name;
command.Parameters.Add(param);
int affectedRows = DataConnection.ExecuteNonQueryCommand(command);
if (affectedRows > 0)
{
employeeAdded = true;
}
}
catch (DbException ex)
{
string errorMessage = string.Empty;
errorMessage = ex.Message;
throw new MyException(errorMessage);
}
return employeeAdded;
}
The configuration class:
public class MyConfiguration
static string providerName;
private static string connectionString;
public static string ConnectionString { get => connectionString; set => connectionString = value; }
public static string ProviderName { get => providerName; set => providerName = value; }
public MyConfiguration()
{
providerName = ConfigurationManager.ConnectionStrings["Connection"].ProviderName;
connectionString = ConfigurationManager.ConnectionStrings["Connection"].ConnectionString;
}
}
Just change the public constructor to static, thus it'll fetch the connection details.
static TravelBookingConfiguration()
{
providerName = ConfigurationManager.ConnectionStrings["travelBookingConnection"].ProviderName;
connectionString = ConfigurationManager.ConnectionStrings["travelBookingConnection"].ConnectionString;
}

How do i return error message in different return type function?

This function connects to postgres database and returns Dataset.
Two things i want to understant
If i get an error how can i return it ?
Is this the best way to return Dataset ?
string strODBCDriverName = "DSN=Postgres_32";
public DataSet SelectDataSet(string sql, bool isProcedure, Dictionary<string, object> parameters = null) {
using (OdbcConnection odbcConnection = new OdbcConnection(strODBCDriverName))
{
odbcConnection.Open();
using (OdbcCommand odbcCommand = new OdbcCommand(sql, odbcConnection))
{
if (isProcedure) odbcCommand.CommandType = CommandType.StoredProcedure;
else odbcCommand.CommandType = CommandType.Text;
if (parameters != null)
foreach (KeyValuePair<string, object> parameter in parameters)
odbcCommand.Parameters.AddWithValue(parameter.Key, parameter.Value);
using (OdbcDataAdapter adapter = new OdbcDataAdapter(odbcCommand))
{
using (DataSet ds = new DataSet())
{
try
{
adapter.Fill(ds); return ds;
}
catch (Exception ex)
{
throw (ex);
}
finally
{
}
}
}
}
}
}
I like having generic Result class that can be reused:
internal class Result
{
internal bool IsFailure => !IsSuccess;
internal bool IsSuccess { get; }
internal string Error { get; }
protected Result(bool isSuccess, string error) {
IsSuccess = isSuccess;
Error = error;
}
private Result(bool isSuccess) : this(isSuccess, null) { }
internal static Result Fail(string error) => new Result(false, error);
internal static Result<T> Fail<T>(string error) =>
new Result<T>(default(T), false, error);
internal static Result Ok() => new Result(true);
internal static Result<T> Ok<T>(T value) => new Result<T>(value, true);
}
internal sealed class Result<T> : Result
{
internal T Value { get; }
internal Result(T value, bool isSuccess) : this(value, isSuccess, null) { }
internal Result(T value, bool isSuccess, string error) : base(isSuccess, error) {
Value = value;
}
This can be used not only DataSet, but any type.
In your case return would be Result<DataSet> and returns can become:
returns ds --> new Result.Ok(d)
throw ex --> new Result.Fail<DataSet>(ex.Message)
I think it would be great if you return null; If you need return some customized message as well means you can use out parameters for this. So that the return value will be null if any Exception occurs in this case out parameter will holds the exception details. if Dataset is populated well means outParameter will have a values "Success" or something like that. So the method signature will be changed as like the following
public static DataSet SelectDataSet(string sql, bool isProcedure, out string message, Dictionary<string, object> parameters = null)
{
// Rest of codes here
try
{
message = "Success";
adapter.Fill(ds);
return ds;
}
catch (Exception ex)
{
message = ex.Message;
return null;
}
}
And you can call this method like this:
string message = String.Empty;
DataSet resultDataset = SelectDataSet("query here", false, out message);
if (resultDataset != null)
{
Console.WriteLine(message);
// proceed with resultDataset
}
else
{
Console.WriteLine(message);
}
Here resultDataset will be null in case of any exception otherwise you can proceed with its value.
Create a class:
class DataSetWithError: DataSet
{
public Exception msg { get; set; }
}
Save error during query:
using (OdbcDataAdapter adapter = new OdbcDataAdapter(odbcCommand))
{
DataSetWithError ds = new DataSetWithError();
try
{
adapter.Fill(ds);
}
catch (Exception ex)
{
ds.msg = ex;
}
finally
{
adapter.Close();
}
return ds;
}
And result:
DataSetWithError dataSetWithError = SelectDataSet();
if (dataSetWithError.msg == null)
{
// Show data
}
else
{
MessageBox.Show(dataSetWithError.msg.ToString());
}

How to start DB transaction, run multiple queries on MySQL then commit or rollback using c#?

I have a generic class that is allowing me to run queries on MySQL database.
The only issue that I am seeing with this class is that it open a connections run a query and close the connection.
Now, I need to be able to
START TRANSACTION;
SELECT ...
INSERT INTO table1...
INSERT INTO table2...
INSERT INTO table3...
if all the above queries worked with no error COMMIT; otherwise ROLLBACK;
Yes, I need a transaction and I need to make sure all queries run 100% or I need to rollback and correct the error prior tarting again.
How can I modify my class to allow me to handle the steps above?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using MySql.Data.MySqlClient;
using System.Windows.Forms;
namespace RM
{
public class dbConnetion
{
//private OdbcConnection conn;
private static readonly string mServer = "localhost";
private static readonly string mDatabase = "my_db_name";
private static readonly string mUid = "my_db_user";
private static readonly string mPassword = "my_user_password";
private static readonly string mPort = "3306";
private string conn_string = String.Format("server={0};user={1};database={2};port={3};password={4};", mServer, mUid, mDatabase, mPort, mPassword);
public string SYSTEM_NAME { get; set; }
public dbConnetion()
{
Initilize_System_Settings();
}
// query the data base
public IEnumerable<T> getData<T>(string query, List<MySqlParameter> pars, Func<IDataRecord, T> transform)
{
using (var conn = new MySqlConnection(conn_string))
using (var cmd = new MySqlCommand(query, conn))
{
if (pars != null)
{
foreach (MySqlParameter p in pars)
{
cmd.Parameters.Add(p);
}
}
conn.Open();
using (var rdr = cmd.ExecuteReader())
{
while (rdr.Read())
{
yield return transform(rdr);
}
}
conn.Close();
}
}
// query the data base
public T getValue<T>(string query, List<MySqlParameter> pars)
{
T value;
using (var conn = new MySqlConnection(conn_string))
using (var cmd = new MySqlCommand(query, conn))
{
if (pars != null)
{
foreach (MySqlParameter p in pars)
{
cmd.Parameters.Add(p);
}
}
try
{
conn.Open();
object rawValue = cmd.ExecuteScalar();
if (rawValue != null)
{
value = (T)Convert.ChangeType(rawValue, typeof(T));
}
else
{
value = default(T);
}
}
catch (Exception ex)
{
Common.Alert(ex.ToString(), "SQL Error");
value = default(T);
}
finally
{
conn.Close();
}
}
return value;
}
public bool processQuery(string strSQL, List<MySqlParameter> pars)
{
bool toReturn = true;
using (var conn = new MySqlConnection(this.conn_string))
using (var cmd = new MySqlCommand(strSQL, conn))
{
foreach (MySqlParameter param in pars)
{
cmd.Parameters.Add(param);
}
try
{
conn.Open();
cmd.ExecuteNonQuery();
}
catch (MySqlException ex)
{
Common.Alert(ex.ToString(), "SQL Error");
toReturn = false;
}
finally
{
conn.Close();
}
}
return toReturn;
}
}
}
My first thoughts was to add a new parameter to the getData method to allow me to not open/close connection if there is an open transaction, like this
// query the data base
public IEnumerable<T> getData<T>(string query, List<MySqlParameter> pars, Func<IDataRecord, T> transform, bool inTransaction = false)
{
using (var conn = new MySqlConnection(conn_string))
using (var cmd = new MySqlCommand(query, conn))
{
if (pars != null)
{
foreach (MySqlParameter p in pars)
{
cmd.Parameters.Add(p);
}
}
if(! inTransaction){
conn.Open();
}
using (var rdr = cmd.ExecuteReader())
{
while (rdr.Read())
{
yield return transform(rdr);
}
}
if(! inTransaction){
conn.Close();
}
}
}
but I think this is not going to work because of the using statement
trans = Conn.BeginTransaction();
trans.Commit();
trans.Rollback();
Cannot access SqlTransaction object to rollback in catch block
You have to determine where to start and end the transaction then,
using (var conn = new MySqlConnection(conn_string))
{
var tx = conn.BeginTransaction();
try
{
using (var cmd1 = new MySqlCommand(query1, conn))
{
cmd1.Transaction = tx;
//do cmd 1 stuff here
}
using (var cmd2 = new MySqlCommand(query2, conn))
{
cmd2.Transaction = tx;
//do cmd 1 stuff here
}
//do other commands....
tx.Commit(); //or Rollback() based on results
}
catch (Exception ex)
{
tx.Rollback();
throw ex;
}
}

How to check for database availability

I have the following code to test DB connection, it runs periodically to test for DB availability:
private bool CheckDbConn()
{
SqlConnection conn = null;
bool result = true;
try
{
conn = DBConnection.getNewCon();
ConnectionState conState = conn.State;
if (conState == ConnectionState.Closed || conState == ConnectionState.Broken)
{
logger.Warn(LogTopicEnum.Agent, "Connection failed in DB connection test on CheckDBConnection");
return false;
}
}
catch (Exception ex)
{
logger.Warn(LogTopicEnum.Agent, "Error in DB connection test on CheckDBConnection", ex);
return false; // any error is considered as db connection error for now
}
finally
{
try
{
if (conn != null)
{
conn.Close();
}
}
catch (Exception ex)
{
logger.Warn(LogTopicEnum.Agent, "Error closing connection on CheckDBConnection", ex);
result = false;
}
}
return result;
}
And:
static public SqlConnection getNewCon()
{
SqlConnection newCon = new SqlConnection();
newCon.ConnectionString = DBConnection.ConnectionString; // m_con.ConnectionString;
newCon.Open();
return newCon;
}
My question is: will this work as expected?
Specifically, I'm concerned about the test of the ConnectionState. Is it possible that the state will be: connecting (since Open() is synchronous)?
What should I do in that case?
You can try like this.
public bool IsServerConnected()
{
using (var l_oConnection = new SqlConnection(DBConnection.ConnectionString))
{
try
{
l_oConnection.Open();
return true;
}
catch (SqlException)
{
return false;
}
}
}
SqlConnection will throw a SqlException when it cannot connect to the server.
public static class SqlExtensions
{
public static bool IsAvailable(this SqlConnection connection)
{
try
{
connection.Open();
connection.Close();
}
catch(SqlException)
{
return false;
}
return true;
}
}
Usage:
using(SqlConnection connection = GetConnection())
{
if(connection.IsAvailable())
{
// Success
}
}
Your code seems fine, but you really need to use the IDisposable pattern, and some naming convention too:
private bool CheckDbConnection(string connectionString)
{
try
{
using(var connection = new SqlConnection(connectionString))
{
connection.Open();
return true;
}
}
catch (Exception ex)
{
logger.Warn(LogTopicEnum.Agent, "Error in DB connection test on CheckDBConnection", ex);
return false; // any error is considered as db connection error for now
}
}
And connection.Close() is not supposed to throw. Just use the using block and your are fine.
No need to test the Close state, since you have just opened it.
More about the Broken state:
Broken The connection to the data source is broken. This can occur
only after the connection has been opened. A connection in this state
may be closed and then re-opened. (This value is reserved for future
versions of the product.)
So really, no need to test that.
The Connecting state could be catch if you are in a multithread context and your instance of connection is shared. But it is not your case here.
This code does not block a UI if called.
public static class DatabaseExtensions
{
public static async Task<bool> IsConnectionViable(this string connectionStr)
{
await using var sqlConn = new SqlConnection(connectionStr);
return await sqlConn.IsConnectionViable();
}
public static async Task<bool> IsConnectionViable(this SqlConnection connection)
{
var isConnected = false;
try
{
await connection.OpenAsync();
isConnected = (connection.State == ConnectionState.Open);
}
catch (Exception)
{
// ignored
}
return isConnected;
}
}
actually, in visual studio, connection class has sonnectionstate property.
when connection state changes, connections statechange event is been trigerred.
you might want to check this article.
https://msdn.microsoft.com/en-us/library/aa326268(v=vs.71).aspx
I was using #Ramesh Durai's solution but found that on my setup at least (the app calling/testing periodically after the app had started; using .Net 3.5 with Sql Server 2012 database) that the first call to IsConnected() after taking the database offline was returning true. However, it was throwing the expected exception on the ExecuteScalar() line below:
public bool IsConnected() {
using (var conn = new SqlConnection(DBConnection.ConnectionString)) {
using (var cmd = New SqlCommand("SELECT 1", conn)) {
try {
conn.Open();
cmd.ExecuteScalar();
return true;
} catch (SqlException) {
return false;
}
}
}
}
This code is for Mysql.
public class Program
{
string connection = "SERVER=localhost; user id=root; password=; database=dbname";
private void Form1_Load(object sender, System.EventArgs e)
{
checkifconnected();
}
private void checkifconnected()
{
MySqlConnection connect = new MySqlConnection(connection);
try{
connect.Open();
MessageBox.Show("Database connected");
}
catch
{
MessageBox.Show("you are not connected to database");
}
}
public static void Main()
{
}
}

Use of BAL in 3 tier architecture?How to call methods from DAL to BAL

I am a newbie to 3 tier architecture and below is my DAL code
public static int Insert(string firstname, string lastname, DateTime dob, string gender,string email, string password)
{
// bool flag = false;
SqlParameter pid;
SqlParameter result;
SqlConnection con = Generic.DBConnection.OpenConnection();
try
{
SqlCommand cmd1 = new SqlCommand("Insertreg", con);
cmd1.CommandType = CommandType.StoredProcedure;
cmd1.Parameters.AddWithValue("#FirstName", firstname);
cmd1.Parameters.AddWithValue("#LastName", lastname);
cmd1.Parameters.AddWithValue("#Dob", dob);
cmd1.Parameters.AddWithValue("#Gender", gender);
cmd1.Parameters.AddWithValue("#EmailId", email);
cmd1.Parameters.AddWithValue("#Password", password);
result = cmd1.Parameters.Add("#result", System.Data.SqlDbType.Int);
result.Direction = System.Data.ParameterDirection.Output;
pid = cmd1.Parameters.Add("#id", System.Data.SqlDbType.Int);
pid.Direction = System.Data.ParameterDirection.Output;
return cmd1.ExecuteNonQuery();
con.Close();
}
catch (Exception ex)
{
throw ex;
}
}
this in BAL
public int insert(string firstname,string lastname,DateTime dob,string gender,string email,string password)
{
ProfileMasterDAL dal=new ProfileMasterDAL();
try
{
return ProfileMasterDAL.Insert(firstname, lastname, dob, gender,email, password);
}
catch (Exception ex)
{
throw ex;
}
finally
{
dal = null;
}
}
I my UI
ProfileMasterBLL pmBLL = new ProfileMasterBLL();
pmBLL.insert(firstname, lastname, dob, gender, mobile, country, state, email, password);
Is this the correct way to code in 3 tier??I mean how to call methods from DAL to BAL and into UI?If not suggest me some good way.Thanks.
Normally I do the following:
Define a Business Layer (BL, you call it BAL). This contains the definitions of you business entities. It also defines interfaces to retrieve/save/delete data for whatever patterns you use (repository, context, etc).
Define a Data Access Layer (DAL). This contains the actual implementation for the retrieve/save/delete interfaces.
Define a UI layer. This contains UI elements (forms, controls, models, controllers, etc), which can use the BL to load data.
The references are the following:
The BL doesn't know the DAL or the UI.
The DAL knows the BL. The DAL does not know the UI.
THe UI knows the BL. The UI does not know the DAL.
The big question for you probably is, how does the BL retrieve/save/delete data when it doesn't know the DAL, and therefore cannot create an instance of a class in the DAL. Well, this is where a little Dependency Injection comes in handy. All you have to wire up is the injection of the DAL-class to the BL-interface.
Hope this makes sense. I use it as my standard 3-tier implementation, and it works absolutely without problems. Specifically, I use Entity Framework with POCO for entities, and the DI I use is a custom one, but any of the ones out there will do.
UPDATE
The BL does not know the DAL.
The BL defines an interface (lets call it IRepository) which it can use to do what it needs to do.
The DAL defines a class (Repository) which implements the interface IRepository. So the actual implementation of the repository is in the DAL.
Obviously the BL cannot create an instance of the repository directly. This is where dependency injection comes in, this allows the developer to create an instance of a class where it normally cannot be done. A simple crude version of this, is to use reflection.
I hope this makes more sense.
It might help you to see some actual code. I suggest you download NetTiers, run it against your db schema and see the outputted code for the implementation details.
using System;
using System.Data;
using System.Configuration;
using System.Collections.Generic;
using System.Text;
using System.Data.Common;
using System.Data.SqlClient;
using System.Data.OleDb;
using System.Data.Odbc;
using System.IO;
using System.ComponentModel;
namespace dal
{
/// <summary>
/// Summary description for Data Access Layer
/// </summary>
public class DataAccess
{
public string strConnectionString;
private DbConnection objConnection;
private DbCommand objCommand;
private DbProviderFactory objFactory = null;
private bool boolHandleErrors=false;
private string strLastError;
private bool boolLogError=false;
private string strLogFile;
public DataAccess()
{
//strConnectionString = ;
strConnectionString = objCommon.GetConnectionString;
objFactory = OleDbFactory.Instance;
objConnection = objFactory.CreateConnection();
objCommand = objFactory.CreateCommand();
objConnection.ConnectionString = strConnectionString;
objCommand.Connection = objConnection;
}
public bool HandleErrors
{
get
{
return boolHandleErrors;
}
set
{
boolHandleErrors = value;
}
}
public string LastError
{
get
{
return strLastError;
}
}
public bool LogErrors
{
get
- {
return boolLogError;
}
set
{
boolLogError = value;
}
}
public string LogFile
{
get
{
return strLogFile;
}
set
{
strLogFile = value;
}
}
public int AddParameter(string name, object value)
{
DbParameter p = objFactory.CreateParameter();
p.ParameterName = name;
p.Value = value;
return objCommand.Parameters.Add(p);
}
public int AddParameter(string name, object value, ParameterDirection direction)
{
DbParameter p = objFactory.CreateParameter();
p.ParameterName = name;
p.Value = value;
p.Direction = direction;
return objCommand.Parameters.Add(p);
}
public int AddParameter(string name, object value, DbType type)
{
DbParameter p = objFactory.CreateParameter();
p.ParameterName = name;
p.Value = value;
p.DbType = type;
return objCommand.Parameters.Add(p);
}
public int AddParameter(DbParameter parameter)
{
return objCommand.Parameters.Add(parameter);
}
public DbCommand Command
{
get
{
return objCommand;
}
}
public void BeginTransaction()
{
try
{
if (objConnection.State == System.Data.ConnectionState.Closed)
{
objConnection.Open();
}
objCommand.Transaction = objConnection.BeginTransaction();
}
catch (Exception Ex)
{
HandleExceptions(Ex);
}
}
public void CommitTransaction()
{
objCommand.Transaction.Commit();
objConnection.Close();
}
public void RollbackTransaction()
{
objCommand.Transaction.Rollback();
objConnection.Close();
}
public int ExecuteNonQuery(string query)
{
return ExecuteNonQuery(query, CommandType.Text, ConnectionState.CloseOnExit);
}
public int ExecuteNonQuery(string query, CommandType commandtype)
{
return ExecuteNonQuery(query, commandtype, ConnectionState.CloseOnExit);
}
public int ExecuteNonQuery(string query, ConnectionState connectionstate)
{
return ExecuteNonQuery(query, CommandType.Text, connectionstate);
}
public int ExecuteNonQuery(string query, CommandType commandtype, ConnectionState connectionstate)
{
objCommand.CommandText = query;
objCommand.CommandType = commandtype;
int i = -1;
try
{
if (objConnection.State == System.Data.ConnectionState.Closed)
{
objConnection.Open();
}
i = objCommand.ExecuteNonQuery();
}
catch (Exception ex)
{
HandleExceptions(ex);
}
finally
{
objCommand.Parameters.Clear();
if (connectionstate == ConnectionState.CloseOnExit)
{
objConnection.Close();
}
}
return i;
}
public object ExecuteScalar(string query)
{
return ExecuteScalar(query, CommandType.Text, ConnectionState.CloseOnExit);
}
public object ExecuteScalar(string query, CommandType commandtype)
{
return ExecuteScalar(query, commandtype, ConnectionState.CloseOnExit);
}
public object ExecuteScalar(string query, ConnectionState connectionstate)
{
return ExecuteScalar(query, CommandType.Text, connectionstate);
}
public object ExecuteScalar(string query, CommandType commandtype, ConnectionState connectionstate)
{
objCommand.CommandText = query;
objCommand.CommandType = commandtype;
object o = null;
try
{
if (objConnection.State == System.Data.ConnectionState.Closed)
{
objConnection.Open();
}
o = objCommand.ExecuteScalar();
}
catch (Exception ex)
{
HandleExceptions(ex);
}
finally
{
objCommand.Parameters.Clear();
if (connectionstate == ConnectionState.CloseOnExit)
{
objConnection.Close();
}
}
return o;
}
public DbDataReader ExecuteReader(string query)
{
return ExecuteReader(query, CommandType.Text, ConnectionState.CloseOnExit);
}
public DbDataReader ExecuteReader(string query, CommandType commandtype)
{
return ExecuteReader(query, commandtype, ConnectionState.CloseOnExit);
}
public DbDataReader ExecuteReader(string query, ConnectionState connectionstate)
{
return ExecuteReader(query, CommandType.Text, connectionstate);
}
public DbDataReader ExecuteReader(string query, CommandType commandtype, ConnectionState connectionstate)
{
objCommand.CommandText = query;
objCommand.CommandType = commandtype;
DbDataReader reader = null;
try
{
if (objConnection.State == System.Data.ConnectionState.Closed)
{
objConnection.Open();
}
if (connectionstate == ConnectionState.CloseOnExit)
{
reader = objCommand.ExecuteReader(CommandBehavior.CloseConnection);
}
else
{
reader = objCommand.ExecuteReader();
}
}
catch (Exception ex)
{
HandleExceptions(ex);
}
finally
{
objCommand.Parameters.Clear();
}
return reader;
}
public DataSet ExecuteDataSet(string query)
{
return ExecuteDataSet(query, CommandType.Text, ConnectionState.CloseOnExit);
}
public DataSet ExecuteDataSet(string query, CommandType commandtype)
{
return ExecuteDataSet(query, commandtype, ConnectionState.CloseOnExit);
}
public DataSet ExecuteDataSet(string query, ConnectionState connectionstate)
{
return ExecuteDataSet(query, CommandType.Text, connectionstate);
}
public DataSet ExecuteDataSet(string query, CommandType commandtype, ConnectionState connectionstate)
{
DbDataAdapter adapter = objFactory.CreateDataAdapter();
objCommand.CommandText = query;
objCommand.CommandType = commandtype;
adapter.SelectCommand = objCommand;
DataSet ds = new DataSet();
try
{
adapter.Fill(ds);
}
catch (Exception ex)
{
HandleExceptions(ex);
}
finally
{
objCommand.Parameters.Clear();
if (connectionstate == ConnectionState.CloseOnExit)
{
if (objConnection.State == System.Data.ConnectionState.Open)
{
objConnection.Close();
}
}
}
return ds;
}
private void HandleExceptions(Exception ex)
{
throw ex;
}
private void WriteToLog(string msg)
{
StreamWriter writer = File.AppendText(LogFile);
writer.WriteLine(DateTime.Now.ToString() + " - " + msg);
writer.Close();
}
public void Dispose()
{
objConnection.Close();
objConnection.Dispose();
objCommand.Dispose();
}
public enum Providers
{
SqlServer, OleDb, Oracle, ODBC, ConfigDefined
}
public enum ConnectionState
{
KeepOpen, CloseOnExit
}
public interface ILoadFromDataRow
{
bool LoadFromDataRow(DataRow row);
}
}
}
You can you the following sample code for 3 tier architecture :-
CLASS - BAL.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Collections;
public class BAL
{
DAL objDAL;
public BAL()
{
}
public string _Name;
public string Name
{
get { return _Name; }
set { _Name = value; }
}
public int insert()
{
objDAL = new DAL();
int val = 0;
try
{
Hashtable objHash = new Hashtable();
objHash.Add("#Name", Convert.ToString(_Name));
val = objDAL.Insert("Your SP Name", objHash);
}
catch (Exception ex)
{
throw ex;
}
finally
{
objDAL = null;
}
return val;
}
}
CLASS - DAL.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Collections;
public class DAL : IDisposable
{
SqlConnection con;
public DAL()
{
con = new SqlConnection("Connection String");
}
public int Insert(string CMD, Hashtable objHash)
{
int val = 0;
try
{
SqlCommand cmd1 = new SqlCommand(CMD, con);
cmd1.CommandType = CommandType.StoredProcedure;
foreach (DictionaryEntry de in objHash)
{
cmd1.Parameters.AddWithValue(Convert.ToString(de.Key), Convert.ToString(de.Value));
}
con.Open();
val = cmd1.ExecuteNonQuery();
}
catch (Exception ex)
{
throw ex;
}
finally
{
con.Close();
}
return val;
}
#region IDisposable Members
public void Dispose()
{
throw new NotImplementedException();
}
#endregion
}
UI:-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _Default : System.Web.UI.Page
{
BAL objBAL;
protected void Page_Load(object sender, EventArgs e)
{
Insert();
}
public void Insert()
{
int val = 0;
objBAL = new BAL();
objBAL.Name = "stackoverflow";
try
{
val = objBAL.insert();
}
catch { }
finally
{
objBAL = null;
}
if (val != 0)
{
//Insert sucessful
}
else
{
//Error in Insert.
}
}
}

Categories

Resources