Posted by jeffreyabecker on Thu 2 Jul 14:17
report abuse | View followups from jeffreyabecker and jeffreyabecker | download | new post
- /*
- ** 02/06/2009
- **
- ** The author disclaims copyright to this source code. In place of
- ** a legal notice, here is a blessing:
- **
- ** May you do good and not evil.
- ** May you find forgiveness for yourself and forgive others.
- ** May you share freely, never taking more than you give.
- **
- **
- */
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Data;
- using System.Data.SqlClient;
- namespace JB.DbExample
- {
- public class DbParameter
- {
- public string Name { get; set; }
- public object Value { get; set; }
- }
- public interface IDbManager : IDisposable
- {
- /// <summary>
- /// Executes a procedure returning the results as a DataSet
- /// </summary>
- /// <param name="procName">The name of the stored procedure</param>
- /// <param name="args">the argument name/value pairs for the procedure</param>
- /// <returns>the resuts as a data set</returns>
- DataSet Exec(string procName, params DbParameter[] args);
- /// <summary>
- /// Executes some sql returning the results as a DataSet
- /// </summary>
- /// <param name="sql">the sql to be executed</param>
- /// <param name="args">the argument name/value pairs for the sql</param>
- /// <returns>the resuts as a data set</returns>
- DataSet ExecDynamicSql(string sql, params DbParameter[] args);
- /// <summary>
- /// Executes a stored procedure and returns a single result
- /// </summary>
- /// <typeparam name="T">The type of the result</typeparam>
- /// <param name="procName">the name of hte procedure</param>
- /// <param name="args">the argument name/value pairs for the procedure</param>
- /// <returns></returns>
- T Scalar<T>(string procName, params DbParameter[] args);
- /// <summary>
- /// Executes some sql returning a single result
- /// </summary>
- /// <typeparam name="T">The type of the result</typeparam>
- /// <param name="sql">the sql to be executed</param>
- /// <param name="args">the argument name/value pairs for the sql</param>
- /// <returns></returns>
- T ScalarDynamicSql<T>(string sql, params DbParameter[] args);
- /// <summary>
- /// Begins a transaction.
- /// </summary>
- void Begin();
- /// <summary>
- /// Commits a transaction.
- /// </summary>
- void Commit();
- /// <summary>
- /// Rolls a transaction back.
- /// </summary>
- void Rollback();
- }
- public static class DbManagerExtensions
- {
- private static DbParameter[] GetArgsFromObject(object obj)
- {
- foreach (var propertyInfo in obj.GetType().GetProperties())
- {
- parameters.Add(new DbParameter() { Name = propertyInfo.Name, Value = propertyInfo.GetValue(obj, null) });
- }
- return parameters.ToArray();
- }
- public static DataSet Exec(this IDbManager db, string procName, object argsDict)
- {
- return db.Exec(procName, GetArgsFromObject(argsDict));
- }
- public static DataSet ExecDynamicSql(this IDbManager db, string sql, object argsDict)
- {
- return db.ExecDynamicSql(sql, GetArgsFromObject(argsDict));
- }
- public static object Scalar(this IDbManager db, string procName, object argsDict)
- {
- return db.Scalar(procName, GetArgsFromObject(argsDict));
- }
- public static object ScalarDynamicSql(this IDbManager db, string sql, object argsDict)
- {
- return db.ScalarDynamicSql(sql, GetArgsFromObject(argsDict));
- }
- }
- public class DbManagerFactory
- {
- public static IDbManager GetDbManager(string connectionStringName)
- {
- return new SqlDbManager(System.Configuration.ConfigurationManager.ConnectionStrings[connectionStringName]);
- }
- // this is the actual implementation of IDbManager. It should never be instantiated outside the factory class
- private class SqlDbManager : IDbManager
- {
- string _connectionString;
- SqlConnection _conn;
- SqlTransaction _trans;
- public SqlDbManager(string connectionString)
- {
- _connectionString = connectionString;
- }
- private SqlConnection GetConnection()
- {
- if (_conn == null)
- {
- _conn.Open();
- }
- return _conn;
- }
- public DataSet Exec(string procName, params DbParameter[] args)
- {
- cmd.CommandType = CommandType.StoredProcedure;
- foreach (DbParameter p in args)
- {
- cmd.Parameters.AddWithValue(p.Name, p.Value);
- }
- adp.Fill(ds);
- return ds;
- }
- public DataSet ExecDynamicSql(string sql, params DbParameter[] args)
- {
- cmd.CommandType = CommandType.Text;
- foreach (DbParameter p in args)
- {
- cmd.Parameters.AddWithValue(p.Name, p.Value);
- }
- adp.Fill(ds);
- return ds;
- }
- public T Scalar<T>(string procName, params DbParameter[] args)
- {
- DataSet ds = Exec(procName, args);
- return ds.Tables[0].Rows[0].Field<T>(0);
- }
- public T ScalarDynamicSql<T>(string sql, params DbParameter[] args)
- {
- DataSet ds = ExecDynamicSql(sql, args);
- return ds.Tables[0].Rows[0].Field<T>(0);
- }
- public void Begin()
- {
- if (_trans != null)
- {
- }
- SqlConnection conn = GetConnection();
- _trans = conn.BeginTransaction();
- }
- public void Commit()
- {
- if (_trans == null)
- _trans.Commit();
- _trans.Dispose();
- _trans = null;
- }
- public void Rollback()
- {
- if (_trans == null)
- _trans.Rollback();
- _trans.Dispose();
- _trans = null;
- }
- public void Dispose()
- {
- if (_trans != null)
- {
- _trans.Rollback();
- _trans.Dispose();
- throw new DataException("Disposed a connection with a pending transaction. Make sure to explicitly commit or rollback your transactions");
- }
- if (_conn != null)
- {
- _conn.Dispose();
- }
- }
- }
- }
- }
Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.