DBHelper.cs
上传用户:jxqhsy
上传日期:2020-12-31
资源大小:1793k
文件大小:10k
源码类别:

SilverLight

开发平台:

HTML/CSS

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Data;
  5. using System.Data.Common;
  6. using System.Configuration;
  7. namespace DAL
  8. {
  9.     public class DBHelper:IDisposable
  10.     {        
  11.         private static string dbProviderName = ConfigurationManager.AppSettings["DbHelperProvider"];
  12.         private static string dbConnectionString = ConfigurationManager.AppSettings["DbHelperConnectionString"];
  13.         private DbConnection connection;
  14.         public DBHelper()
  15.         {
  16.             this.connection = CreateConnection(DBHelper.dbConnectionString);
  17.         }
  18.         public DBHelper(string connectionString)
  19.         {
  20.             this.connection = CreateConnection(connectionString);
  21.         }
  22.         public static DbConnection CreateConnection()
  23.         {
  24.             DbProviderFactory dbfactory = DbProviderFactories.GetFactory(DBHelper.dbProviderName);
  25.             DbConnection dbconn = dbfactory.CreateConnection();
  26.             dbconn.ConnectionString = DBHelper.dbConnectionString;
  27.             return dbconn;
  28.         }
  29.         public static DbConnection CreateConnection(string connectionString)
  30.         {
  31.             DbProviderFactory dbfactory = DbProviderFactories.GetFactory(DBHelper.dbProviderName);
  32.             DbConnection dbconn = dbfactory.CreateConnection();
  33.             dbconn.ConnectionString = connectionString;
  34.             return dbconn;
  35.         }
  36.         public void Close()
  37.         {
  38.             try
  39.             {
  40.                 if (null != this.connection)
  41.                 {
  42.                     this.connection.Close();
  43.                 }
  44.             }
  45.             catch (System.Exception ex)
  46.             {
  47.                 throw ex;
  48.             }            
  49.         }
  50.         public DbCommand GetStoredProcCommond(string storedProcedure)
  51.         {
  52.             DbCommand dbCommand = connection.CreateCommand();
  53.             dbCommand.CommandText = storedProcedure;
  54.             dbCommand.CommandType = CommandType.StoredProcedure;
  55.             return dbCommand;
  56.         }
  57.         public DbCommand GetSqlStringCommond(string sqlQuery)
  58.         {
  59.             DbCommand dbCommand = connection.CreateCommand();
  60.             dbCommand.CommandText = sqlQuery;
  61.             dbCommand.CommandType = CommandType.Text;
  62.             return dbCommand;
  63.         }
  64.         #region 增加参数
  65.         public void AddParameterCollection(DbCommand cmd, DbParameterCollection dbParameterCollection)
  66.         {
  67.             foreach (DbParameter dbParameter in dbParameterCollection)
  68.             {
  69.                 cmd.Parameters.Add(dbParameter);
  70.             }
  71.         }
  72.         public void AddParameter(DbCommand cmd, string parameterName, DbType dbType, int size, object value)
  73.         {
  74.             DbParameter dbParameter = cmd.CreateParameter();
  75.             dbParameter.DbType = dbType;
  76.             dbParameter.ParameterName = parameterName;
  77.             dbParameter.Size = size;
  78.             dbParameter.Value = value;
  79.             cmd.Parameters.Add(dbParameter);
  80.         }
  81.         public void AddParameter(DbCommand cmd, string parameterName, DbType dbType,  object value)
  82.         {
  83.             DbParameter dbParameter = cmd.CreateParameter();
  84.             dbParameter.DbType = dbType;
  85.             dbParameter.ParameterName = parameterName;
  86.             dbParameter.Value = value;
  87.             cmd.Parameters.Add(dbParameter);
  88.         }
  89.         public void AddParameter(DbCommand cmd, string parameterName, object value)
  90.         {
  91.             DbParameter dbParameter = cmd.CreateParameter();
  92.             dbParameter.DbType = DbType.String;
  93.             dbParameter.ParameterName = parameterName;
  94.             dbParameter.Value = value;
  95.             cmd.Parameters.Add(dbParameter);
  96.         }
  97.         public void AddOutParameter(DbCommand cmd, string parameterName, DbType dbType, int size)
  98.         {
  99.             DbParameter dbParameter = cmd.CreateParameter();
  100.             dbParameter.DbType = dbType;
  101.             dbParameter.ParameterName = parameterName;
  102.             dbParameter.Size = size;
  103.             dbParameter.Direction = ParameterDirection.Output;
  104.             cmd.Parameters.Add(dbParameter);
  105.         }
  106.         public void AddInParameter(DbCommand cmd, string parameterName, DbType dbType, object value)
  107.         {
  108.             DbParameter dbParameter = cmd.CreateParameter();
  109.             dbParameter.DbType = dbType;
  110.             dbParameter.ParameterName = parameterName;
  111.             dbParameter.Value = value;
  112.             dbParameter.Direction = ParameterDirection.Input;
  113.             cmd.Parameters.Add(dbParameter);
  114.         }
  115.         public void AddReturnParameter(DbCommand cmd, string parameterName, DbType dbType)
  116.         {
  117.             DbParameter dbParameter = cmd.CreateParameter();
  118.             dbParameter.DbType = dbType;
  119.             dbParameter.ParameterName = parameterName;
  120.             dbParameter.Direction = ParameterDirection.ReturnValue;
  121.             cmd.Parameters.Add(dbParameter);
  122.         }
  123.         public DbParameter GetParameter(DbCommand cmd, string parameterName)
  124.         {
  125.             return cmd.Parameters[parameterName];
  126.         }
  127.         #endregion
  128.         #region 执行
  129.         public DataSet ExecuteDataSet(DbCommand cmd)
  130.         {
  131.             DbProviderFactory dbfactory = DbProviderFactories.GetFactory(DBHelper.dbProviderName);
  132.             DbDataAdapter dbDataAdapter = dbfactory.CreateDataAdapter();
  133.             dbDataAdapter.SelectCommand = cmd;
  134.             DataSet ds = new DataSet();
  135.             dbDataAdapter.Fill(ds);
  136.             return ds;
  137.         }
  138.         public DataTable ExecuteDataTable(DbCommand cmd)
  139.         {
  140.             DbProviderFactory dbfactory = DbProviderFactories.GetFactory(DBHelper.dbProviderName);
  141.             DbDataAdapter dbDataAdapter = dbfactory.CreateDataAdapter();
  142.             dbDataAdapter.SelectCommand = cmd;
  143.             DataTable dataTable = new DataTable();
  144.             dbDataAdapter.Fill(dataTable);
  145.             return dataTable;
  146.         }
  147.         public DbDataReader ExecuteReader(DbCommand cmd)
  148.         {
  149.             cmd.Connection.Open();
  150.             DbDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);            
  151.             return reader;
  152.         }
  153.         public int ExecuteNonQuery(DbCommand cmd)
  154.         {
  155.             cmd.Connection.Open();
  156.             int ret = cmd.ExecuteNonQuery();
  157.             cmd.Connection.Close();
  158.             return ret;
  159.         }
  160.         public object ExecuteScalar(DbCommand cmd)
  161.         {
  162.             cmd.Connection.Open();
  163.             object ret = cmd.ExecuteScalar();
  164.             cmd.Connection.Close();
  165.             
  166.             return ret;
  167.         }
  168.         #endregion        
  169.         #region 执行事务
  170.         public DataSet ExecuteDataSet(DbCommand cmd,Trans t)
  171.         {
  172.             cmd.Connection = t.DbConnection;
  173.             cmd.Transaction = t.DbTrans;
  174.             DbProviderFactory dbfactory = DbProviderFactories.GetFactory(DBHelper.dbProviderName);
  175.             DbDataAdapter dbDataAdapter = dbfactory.CreateDataAdapter();
  176.             dbDataAdapter.SelectCommand = cmd;
  177.             DataSet ds = new DataSet();
  178.             dbDataAdapter.Fill(ds);
  179.             return ds;
  180.         }
  181.         public DataTable ExecuteDataTable(DbCommand cmd, Trans t)
  182.         {
  183.             cmd.Connection = t.DbConnection;
  184.             cmd.Transaction = t.DbTrans;
  185.             DbProviderFactory dbfactory = DbProviderFactories.GetFactory(DBHelper.dbProviderName);
  186.             DbDataAdapter dbDataAdapter = dbfactory.CreateDataAdapter();
  187.             dbDataAdapter.SelectCommand = cmd;
  188.             DataTable dataTable = new DataTable();
  189.             dbDataAdapter.Fill(dataTable);
  190.             return dataTable;
  191.         }
  192.         public DbDataReader ExecuteReader(DbCommand cmd, Trans t)
  193.         {
  194.             cmd.Connection.Close();
  195.             cmd.Connection = t.DbConnection;
  196.             cmd.Transaction = t.DbTrans;            
  197.             DbDataReader reader = cmd.ExecuteReader();
  198.             DataTable dt = new DataTable();            
  199.             return reader;
  200.         }
  201.         public int ExecuteNonQuery(DbCommand cmd, Trans t)
  202.         {
  203.             cmd.Connection.Close();
  204.             cmd.Connection = t.DbConnection;
  205.             cmd.Transaction = t.DbTrans;  
  206.             int ret = cmd.ExecuteNonQuery();            
  207.             return ret;
  208.         }
  209.         public object ExecuteScalar(DbCommand cmd, Trans t)
  210.         {
  211.             cmd.Connection.Close();
  212.             cmd.Connection = t.DbConnection;
  213.             cmd.Transaction = t.DbTrans;  
  214.             object ret = cmd.ExecuteScalar();            
  215.             return ret;
  216.         }
  217.         #endregion
  218.         #region IDisposable 成员
  219.         void IDisposable.Dispose()
  220.         {
  221.             throw new Exception("The method or operation is not implemented.");
  222.         }
  223.         #endregion
  224.     }
  225.     public class Trans : IDisposable
  226.     {
  227.         private DbConnection conn;
  228.         private DbTransaction dbTrans;
  229.         public DbConnection DbConnection
  230.         {
  231.             get { return this.conn; }
  232.         }
  233.         public DbTransaction DbTrans
  234.         {
  235.             get { return this.dbTrans; }
  236.         }
  237.         public Trans()
  238.         {
  239.             conn = DBHelper.CreateConnection();
  240.             conn.Open();
  241.             dbTrans = conn.BeginTransaction();
  242.         }
  243.         public Trans(string connectionString)
  244.         {
  245.             conn = DBHelper.CreateConnection(connectionString);
  246.             conn.Open();
  247.             dbTrans = conn.BeginTransaction();
  248.         }
  249.         public void Commit()
  250.         {
  251.             dbTrans.Commit();
  252.             this.Close();
  253.         }
  254.         public void RollBack()
  255.         {
  256.             dbTrans.Rollback();
  257.             this.Close();
  258.         }
  259.         public void Dispose()
  260.         {
  261.             this.Close();
  262.         }
  263.         public void Close()
  264.         {
  265.             if (conn.State == System.Data.ConnectionState.Open)
  266.             {
  267.                 conn.Close();
  268.             }
  269.         }
  270.     }
  271. }