AdoHelper Class

using System;

using System.Data;

using System.Data.SqlClient;

namespace BlackBeltCoder

{
class AdoHelper : IDisposable
{

protected string _connString = null;

protected SqlConnection _conn = null;
protected SqlTransaction _trans = null;
protected bool _disposed = false;
public static string ConnectionString { get; set; }
public SqlTransaction Transaction { get { return _trans; } }

public AdoHelper()
{
_connString = ConnectionString;

Connect();

}

public AdoHelper(string connString)

{

_connString = connString;

Connect();

}

protected void Connect()

{

_conn = new SqlConnection(_connString);

_conn.Open();

}

public SqlCommand CreateCommand(string qry, CommandType type, paramsobject[]

args)
{
SqlCommand cmd = new SqlCommand(qry, _conn);
if (_trans !=  null)
cmd.Transaction = _trans;
cmd.CommandType = type;
for (int i = 0; i < args.Length; i++)
{

if (args[i] is string && i < (args.Length – 1))

{
SqlParameter parm = new SqlParameter();
parm.ParameterName = (string)args[i];
parm.Value = args[++i];
cmd.Parameters.Add(parm);
}
else if
(args[i] is SqlParameter)
{
cmd.Parameters.Add((SqlParameter)args[i]);
}
else throw
new
ArgumentException
(“Invalid number or type of
arguments supplied”
);
}
return cmd;
}

#region
Exec Members
public int ExecNonQuery(string qry, params object[] args)
{
using (SqlCommand cmd =CreateCommand(qry, CommandType.Text, args))
{
return cmd.ExecuteNonQuery();
}
}

public int ExecNonQueryProc(string proc, params object[] args)

{

using (SqlCommand cmd =
CreateCommand(proc, CommandType.StoredProcedure, args))

{

return cmd.ExecuteNonQuery();

}

}

public object ExecScalar(string qry,  params object[] args)

{

using (SqlCommand cmd =
CreateCommand(qry, CommandType.Text, args))

{

return cmd.ExecuteScalar();

}

}

publicobject ExecScalarProc(string qry,paramsobject[] args)

{

using (SqlCommand cmd =
CreateCommand(qry, CommandType.StoredProcedure, args))

{

return cmd.ExecuteScalar();

}

}

public SqlDataReader ExecDataReader(string qry, params

object[] args)

{

using (SqlCommand cmd =
CreateCommand(qry, CommandType.Text, args))

{

return cmd.ExecuteReader();

}

}

public SqlDataReader ExecDataReaderProc(string qry, params

object[] args)

{

using (SqlCommand cmd =
CreateCommand(qry, CommandType.StoredProcedure, args))

{

return cmd.ExecuteReader();

}

}

 public DataSet ExecDataSet(string qry, params

object[] args)

{

using (SqlCommand cmd =
CreateCommand(qry, CommandType.Text, args))

{

SqlDataAdapter adapt = new SqlDataAdapter(cmd);

DataSet ds = new DataSet();

adapt.Fill(ds);

return ds;

}

}

public DataSet ExecDataSetProc(string qry, params

object[] args)

{

using (SqlCommand cmd = CreateCommand(qry, CommandType.StoredProcedure, args))

{

SqlDataAdapter adapt = new SqlDataAdapter(cmd);

DataSet ds = new DataSet();

adapt.Fill(ds);

return ds;

}

}

#endregion   

#region Transaction Members

public SqlTransaction BeginTransaction()

{

Rollback();

_trans = _conn.BeginTransaction();

return Transaction;

}

public void

Commit()

{

if (_trans !=
null
)

{

_trans.Commit();

_trans = null;

}

}

 public void Rollback()

{


if (_trans != null)

{

_trans.Rollback();

_trans = null;

}

}      

#endregion      

#region IDisposable Members

 public void Dispose()

{

Dispose(true);

GC.SuppressFinalize(this);

}

protected virtual void Dispose(bool disposing)

{

if (!_disposed)

{

if (disposing)

{

if (_conn != null)

{

Rollback();

_conn.Dispose();

_conn = null;

}

}

_disposed = true;

}

}    

#endregion

}

}

Advertisements

One thought on “AdoHelper Class

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s