DBHelper.cs
上传用户:jxqhsy
上传日期:2020-12-31
资源大小:1793k
文件大小:10k
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Data;
- using System.Data.Common;
- using System.Configuration;
- namespace DAL
- {
- public class DBHelper:IDisposable
- {
- private static string dbProviderName = ConfigurationManager.AppSettings["DbHelperProvider"];
- private static string dbConnectionString = ConfigurationManager.AppSettings["DbHelperConnectionString"];
- private DbConnection connection;
- public DBHelper()
- {
- this.connection = CreateConnection(DBHelper.dbConnectionString);
- }
- public DBHelper(string connectionString)
- {
- this.connection = CreateConnection(connectionString);
- }
- public static DbConnection CreateConnection()
- {
- DbProviderFactory dbfactory = DbProviderFactories.GetFactory(DBHelper.dbProviderName);
- DbConnection dbconn = dbfactory.CreateConnection();
- dbconn.ConnectionString = DBHelper.dbConnectionString;
- return dbconn;
- }
- public static DbConnection CreateConnection(string connectionString)
- {
- DbProviderFactory dbfactory = DbProviderFactories.GetFactory(DBHelper.dbProviderName);
- DbConnection dbconn = dbfactory.CreateConnection();
- dbconn.ConnectionString = connectionString;
- return dbconn;
- }
- public void Close()
- {
- try
- {
- if (null != this.connection)
- {
- this.connection.Close();
- }
- }
- catch (System.Exception ex)
- {
- throw ex;
- }
- }
- public DbCommand GetStoredProcCommond(string storedProcedure)
- {
- DbCommand dbCommand = connection.CreateCommand();
- dbCommand.CommandText = storedProcedure;
- dbCommand.CommandType = CommandType.StoredProcedure;
- return dbCommand;
- }
- public DbCommand GetSqlStringCommond(string sqlQuery)
- {
- DbCommand dbCommand = connection.CreateCommand();
- dbCommand.CommandText = sqlQuery;
- dbCommand.CommandType = CommandType.Text;
- return dbCommand;
- }
- #region 增加参数
- public void AddParameterCollection(DbCommand cmd, DbParameterCollection dbParameterCollection)
- {
- foreach (DbParameter dbParameter in dbParameterCollection)
- {
- cmd.Parameters.Add(dbParameter);
- }
- }
- public void AddParameter(DbCommand cmd, string parameterName, DbType dbType, int size, object value)
- {
- DbParameter dbParameter = cmd.CreateParameter();
- dbParameter.DbType = dbType;
- dbParameter.ParameterName = parameterName;
- dbParameter.Size = size;
- dbParameter.Value = value;
- cmd.Parameters.Add(dbParameter);
- }
- public void AddParameter(DbCommand cmd, string parameterName, DbType dbType, object value)
- {
- DbParameter dbParameter = cmd.CreateParameter();
- dbParameter.DbType = dbType;
- dbParameter.ParameterName = parameterName;
- dbParameter.Value = value;
- cmd.Parameters.Add(dbParameter);
- }
- public void AddParameter(DbCommand cmd, string parameterName, object value)
- {
- DbParameter dbParameter = cmd.CreateParameter();
- dbParameter.DbType = DbType.String;
- dbParameter.ParameterName = parameterName;
- dbParameter.Value = value;
- cmd.Parameters.Add(dbParameter);
- }
- public void AddOutParameter(DbCommand cmd, string parameterName, DbType dbType, int size)
- {
- DbParameter dbParameter = cmd.CreateParameter();
- dbParameter.DbType = dbType;
- dbParameter.ParameterName = parameterName;
- dbParameter.Size = size;
- dbParameter.Direction = ParameterDirection.Output;
- cmd.Parameters.Add(dbParameter);
- }
- public void AddInParameter(DbCommand cmd, string parameterName, DbType dbType, object value)
- {
- DbParameter dbParameter = cmd.CreateParameter();
- dbParameter.DbType = dbType;
- dbParameter.ParameterName = parameterName;
- dbParameter.Value = value;
- dbParameter.Direction = ParameterDirection.Input;
- cmd.Parameters.Add(dbParameter);
- }
- public void AddReturnParameter(DbCommand cmd, string parameterName, DbType dbType)
- {
- DbParameter dbParameter = cmd.CreateParameter();
- dbParameter.DbType = dbType;
- dbParameter.ParameterName = parameterName;
- dbParameter.Direction = ParameterDirection.ReturnValue;
- cmd.Parameters.Add(dbParameter);
- }
- public DbParameter GetParameter(DbCommand cmd, string parameterName)
- {
- return cmd.Parameters[parameterName];
- }
- #endregion
- #region 执行
- public DataSet ExecuteDataSet(DbCommand cmd)
- {
- DbProviderFactory dbfactory = DbProviderFactories.GetFactory(DBHelper.dbProviderName);
- DbDataAdapter dbDataAdapter = dbfactory.CreateDataAdapter();
- dbDataAdapter.SelectCommand = cmd;
- DataSet ds = new DataSet();
- dbDataAdapter.Fill(ds);
- return ds;
- }
- public DataTable ExecuteDataTable(DbCommand cmd)
- {
- DbProviderFactory dbfactory = DbProviderFactories.GetFactory(DBHelper.dbProviderName);
- DbDataAdapter dbDataAdapter = dbfactory.CreateDataAdapter();
- dbDataAdapter.SelectCommand = cmd;
- DataTable dataTable = new DataTable();
- dbDataAdapter.Fill(dataTable);
- return dataTable;
- }
- public DbDataReader ExecuteReader(DbCommand cmd)
- {
- cmd.Connection.Open();
- DbDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
- return reader;
- }
- public int ExecuteNonQuery(DbCommand cmd)
- {
- cmd.Connection.Open();
- int ret = cmd.ExecuteNonQuery();
- cmd.Connection.Close();
- return ret;
- }
- public object ExecuteScalar(DbCommand cmd)
- {
- cmd.Connection.Open();
- object ret = cmd.ExecuteScalar();
- cmd.Connection.Close();
-
- return ret;
- }
- #endregion
- #region 执行事务
- public DataSet ExecuteDataSet(DbCommand cmd,Trans t)
- {
- cmd.Connection = t.DbConnection;
- cmd.Transaction = t.DbTrans;
- DbProviderFactory dbfactory = DbProviderFactories.GetFactory(DBHelper.dbProviderName);
- DbDataAdapter dbDataAdapter = dbfactory.CreateDataAdapter();
- dbDataAdapter.SelectCommand = cmd;
- DataSet ds = new DataSet();
- dbDataAdapter.Fill(ds);
- return ds;
- }
- public DataTable ExecuteDataTable(DbCommand cmd, Trans t)
- {
- cmd.Connection = t.DbConnection;
- cmd.Transaction = t.DbTrans;
- DbProviderFactory dbfactory = DbProviderFactories.GetFactory(DBHelper.dbProviderName);
- DbDataAdapter dbDataAdapter = dbfactory.CreateDataAdapter();
- dbDataAdapter.SelectCommand = cmd;
- DataTable dataTable = new DataTable();
- dbDataAdapter.Fill(dataTable);
- return dataTable;
- }
- public DbDataReader ExecuteReader(DbCommand cmd, Trans t)
- {
- cmd.Connection.Close();
- cmd.Connection = t.DbConnection;
- cmd.Transaction = t.DbTrans;
- DbDataReader reader = cmd.ExecuteReader();
- DataTable dt = new DataTable();
- return reader;
- }
- public int ExecuteNonQuery(DbCommand cmd, Trans t)
- {
- cmd.Connection.Close();
- cmd.Connection = t.DbConnection;
- cmd.Transaction = t.DbTrans;
- int ret = cmd.ExecuteNonQuery();
- return ret;
- }
- public object ExecuteScalar(DbCommand cmd, Trans t)
- {
- cmd.Connection.Close();
- cmd.Connection = t.DbConnection;
- cmd.Transaction = t.DbTrans;
- object ret = cmd.ExecuteScalar();
- return ret;
- }
- #endregion
- #region IDisposable 成员
- void IDisposable.Dispose()
- {
- throw new Exception("The method or operation is not implemented.");
- }
- #endregion
- }
- public class Trans : IDisposable
- {
- private DbConnection conn;
- private DbTransaction dbTrans;
- public DbConnection DbConnection
- {
- get { return this.conn; }
- }
- public DbTransaction DbTrans
- {
- get { return this.dbTrans; }
- }
- public Trans()
- {
- conn = DBHelper.CreateConnection();
- conn.Open();
- dbTrans = conn.BeginTransaction();
- }
- public Trans(string connectionString)
- {
- conn = DBHelper.CreateConnection(connectionString);
- conn.Open();
- dbTrans = conn.BeginTransaction();
- }
- public void Commit()
- {
- dbTrans.Commit();
- this.Close();
- }
- public void RollBack()
- {
- dbTrans.Rollback();
- this.Close();
- }
- public void Dispose()
- {
- this.Close();
- }
- public void Close()
- {
- if (conn.State == System.Data.ConnectionState.Open)
- {
- conn.Close();
- }
- }
- }
- }