DBHelper.cs
上传用户:simon2hong
上传日期:2021-11-18
资源大小:16746k
文件大小:15k
源码类别:

OA系统

开发平台:

C#

  1. using System;
  2. using System.Data;
  3. using System.Data.Common;
  4. using System.Text;
  5. using MySql.Data.MySqlClient;
  6. using System.Data.OracleClient;
  7. using System.Data.OleDb;
  8. using System.Data.SqlClient;
  9. namespace DBUtility
  10. {
  11.     /// <summary>
  12.     /// 数据库操作
  13.     /// </summary>
  14.     public class DBHelper
  15.     {
  16.         /// <summary>
  17.         /// 枚举:数据库类型
  18.         /// </summary>
  19.         public enum DatabaseTypes
  20.         {
  21.             Sql, MySql, Oracle, OleDb
  22.         }
  23.         private DatabaseTypes _databaseType;
  24.         private string _connectionString;
  25.         private IDBHelper _iDBHelper;
  26.         public DBHelper()
  27.         { }
  28.         public DBHelper(DatabaseTypes databaseType, string connectionString)
  29.         {
  30.             DatabaseType = databaseType;
  31.             this._connectionString = connectionString;
  32.         }
  33.         /// <summary>
  34.         /// 数据库类型
  35.         /// </summary>
  36.         public DatabaseTypes DatabaseType
  37.         {
  38.             get
  39.             {
  40.                 return _databaseType;
  41.             }
  42.             set
  43.             {
  44.                 _databaseType = value;
  45.                 switch (value)
  46.                 {
  47.                     case DatabaseTypes.OleDb:
  48.                         _iDBHelper = new OleDbHelper();
  49.                         break;
  50.                     case DatabaseTypes.MySql:
  51.                         _iDBHelper = new MySqlHelper();
  52.                         break;
  53.                     case DatabaseTypes.Oracle:
  54.                         _iDBHelper = new OracleHelper();
  55.                         break;
  56.                     case DatabaseTypes.Sql:
  57.                     default:
  58.                         _iDBHelper = new SqlHelper();
  59.                         break;
  60.                 }
  61.             }
  62.         }
  63.         /// <summary>
  64.         /// 数据库连接字符串
  65.         /// </summary>
  66.         public string ConnectionString
  67.         {
  68.             get { return _connectionString; }
  69.             set { _connectionString = value; }
  70.         }
  71.         /// <summary>
  72.         /// 创建数据库连接
  73.         /// </summary>
  74.         public DbConnection CreateConnection()
  75.         {
  76.             switch (_databaseType)
  77.             {
  78.                 case DatabaseTypes.MySql:
  79.                     return new MySqlConnection(_connectionString);
  80.                 case DatabaseTypes.Oracle:
  81.                     return new OracleConnection(_connectionString);
  82.                 case DatabaseTypes.OleDb:
  83.                     return new OleDbConnection(_connectionString);
  84.                 case DatabaseTypes.Sql:
  85.                 default:
  86.                     return new SqlConnection(_connectionString);
  87.             }
  88.         }
  89.         #region === 创造DbParameter的实例 ===
  90.         /// <summary>
  91.         /// 创造输入DbParameter的实例
  92.         /// </summary>
  93.         public DbParameter CreateInDbParameter(string paraName, DbType dbType, int size, object value)
  94.         {
  95.             return CreateDbParameter(paraName, dbType, size, value, ParameterDirection.Input);
  96.         }
  97.         /// <summary>
  98.         /// 创造输入DbParameter的实例
  99.         /// </summary>
  100.         public DbParameter CreateInDbParameter(string paraName, DbType dbType, object value)
  101.         {
  102.             return CreateDbParameter(paraName, dbType, 0, value, ParameterDirection.Input);
  103.         }
  104.         /// <summary>
  105.         /// 创造输出DbParameter的实例
  106.         /// </summary>        
  107.         public DbParameter CreateOutDbParameter(string paraName, DbType dbType, int size)
  108.         {
  109.             return CreateDbParameter(paraName, dbType, size, null, ParameterDirection.Output);
  110.         }
  111.         /// <summary>
  112.         /// 创造输出DbParameter的实例
  113.         /// </summary>        
  114.         public DbParameter CreateOutDbParameter(string paraName, DbType dbType)
  115.         {
  116.             return CreateDbParameter(paraName, dbType, 0, null, ParameterDirection.Output);
  117.         }
  118.         /// <summary>
  119.         /// 创造返回DbParameter的实例
  120.         /// </summary>        
  121.         public DbParameter CreateReturnDbParameter(string paraName, DbType dbType, int size)
  122.         {
  123.             return CreateDbParameter(paraName, dbType, size, null, ParameterDirection.ReturnValue);
  124.         }
  125.         /// <summary>
  126.         /// 创造返回DbParameter的实例
  127.         /// </summary>        
  128.         public DbParameter CreateReturnDbParameter(string paraName, DbType dbType)
  129.         {
  130.             return CreateDbParameter(paraName, dbType, 0, null, ParameterDirection.ReturnValue);
  131.         }
  132.         /// <summary>
  133.         /// 创造DbParameter的实例
  134.         /// </summary>
  135.         public DbParameter CreateDbParameter(string paraName, DbType dbType, int size, object value, ParameterDirection direction)
  136.         {
  137.             DbParameter para;
  138.             switch (_databaseType)
  139.             {
  140.                 case DatabaseTypes.MySql:
  141.                     para = new MySqlParameter();
  142.                     break;
  143.                 case DatabaseTypes.Oracle:
  144.                     para = new OracleParameter();
  145.                     break;
  146.                 case DatabaseTypes.OleDb:
  147.                     para = new OleDbParameter();
  148.                     break;
  149.                 case DatabaseTypes.Sql:
  150.                 default:
  151.                     para = new SqlParameter();
  152.                     break;
  153.             }
  154.             para.ParameterName = paraName;
  155.             if (size != 0)
  156.                 para.Size = size;
  157.             para.DbType = dbType;
  158.             if (value != null)
  159.                 para.Value = value;
  160.             para.Direction = direction;
  161.             return para;
  162.         }
  163.         #endregion
  164.         #region === 数据库执行方法 ===
  165.         /// <summary>
  166.         /// 执行 Transact-SQL 语句并返回受影响的行数。
  167.         /// </summary>
  168.         public int ExecuteNonQuery(CommandType cmdType, string cmdText, params DbParameter[] cmdParms)
  169.         {
  170.             return _iDBHelper.ExecuteNonQuery(_connectionString, cmdType, cmdText, cmdParms);
  171.         }
  172.         /// <summary>
  173.         /// 在事务中执行 Transact-SQL 语句并返回受影响的行数。
  174.         /// </summary>
  175.         public int ExecuteNonQuery(DbTransaction trans, CommandType cmdType, string cmdText, params DbParameter[] cmdParms)
  176.         {
  177.             return _iDBHelper.ExecuteNonQuery(trans, cmdType, cmdText, cmdParms);
  178.         }
  179.         /// <summary>
  180.         /// 在事务中执行查询,返回DataSet
  181.         /// </summary>
  182.         public DataSet ExecuteQuery(DbTransaction trans, CommandType cmdType, string cmdText, params DbParameter[] cmdParms)
  183.         {
  184.             return _iDBHelper.ExecuteQuery(trans, cmdType, cmdText, cmdParms);
  185.         }
  186.         /// <summary>
  187.         /// 执行查询,返回DataSet
  188.         /// </summary>
  189.         public DataSet ExecuteQuery(CommandType cmdType, string cmdText, params DbParameter[] cmdParms)
  190.         {
  191.             return _iDBHelper.ExecuteQuery(_connectionString, cmdType, cmdText, cmdParms);
  192.         }
  193.         /// <summary>
  194.         /// 在事务中执行查询,返回DataReader
  195.         /// </summary>
  196.         public DbDataReader ExecuteReader(DbTransaction trans, CommandType cmdType, string cmdText, params DbParameter[] cmdParms)
  197.         {
  198.             return _iDBHelper.ExecuteReader(trans, cmdType, cmdText, cmdParms);
  199.         }
  200.         /// <summary>
  201.         /// 执行查询,返回DataReader
  202.         /// </summary>
  203.         public DbDataReader ExecuteReader(CommandType cmdType, string cmdText, params DbParameter[] cmdParms)
  204.         {
  205.             return _iDBHelper.ExecuteReader(_connectionString, cmdType, cmdText, cmdParms);
  206.         }
  207.         /// <summary>
  208.         /// 在事务中执行查询,并返回查询所返回的结果集中第一行的第一列。忽略其他列或行。
  209.         /// </summary>
  210.         public object ExecuteScalar(DbTransaction trans, CommandType cmdType, string cmdText, params DbParameter[] cmdParms)
  211.         {
  212.             return _iDBHelper.ExecuteScalar(trans, cmdType, cmdText, cmdParms);
  213.         }
  214.         /// <summary>
  215.         /// 执行查询,并返回查询所返回的结果集中第一行的第一列。忽略其他列或行。
  216.         /// </summary>
  217.         public object ExecuteScalar(CommandType cmdType, string cmdText, params DbParameter[] cmdParms)
  218.         {
  219.             return _iDBHelper.ExecuteScalar(_connectionString, cmdType, cmdText, cmdParms);
  220.         }
  221.         /// <summary>
  222.         /// 分页获取数据
  223.         /// </summary>
  224.         /// <param name="connectionString">连接字符串</param>
  225.         /// <param name="tblName">表名</param>
  226.         /// <param name="fldName">字段名</param>
  227.         /// <param name="pageSize">页大小</param>
  228.         /// <param name="pageIndex">第几页</param>
  229.         /// <param name="fldSort">排序字段</param>
  230.         /// <param name="fldDir">升序{False}/降序(True)</param>
  231.         /// <param name="condition">条件(不需要where)</param>
  232.         public DbDataReader GetPageList(string tblName, int pageSize, int pageIndex, string fldSort, bool fldDir, string condition)
  233.         {
  234.             return _iDBHelper.GetPageList(_connectionString, tblName, pageSize, pageIndex, fldSort, fldDir, condition);
  235.         }
  236.         /// <summary>
  237.         /// 得到数据条数
  238.         /// </summary>
  239.         /// <param name="tblName">表名</param>
  240.         /// <param name="condition">条件(不需要where)</param>
  241.         /// <returns>数据条数</returns>
  242.         public int GetCount(string tblName, string condition)
  243.         {
  244.             return _iDBHelper.GetCount(_connectionString, tblName, condition);
  245.         }
  246.         #endregion
  247.         #region === 由Object取值 ===
  248.         /// <summary>
  249.         /// 取得Int16值
  250.         /// </summary>
  251.         public Int16 GetInt16(object obj)
  252.         {
  253.             if (obj != DBNull.Value)
  254.             {
  255.                 return Convert.ToInt16(obj);
  256.             }
  257.             else
  258.             {
  259.                 return 0;
  260.             }
  261.         }
  262.         /// <summary>
  263.         /// 取得UInt16值
  264.         /// </summary>
  265.         public UInt16 GetUInt16(object obj)
  266.         {
  267.             if (obj != DBNull.Value)
  268.             {
  269.                 return Convert.ToUInt16(obj);
  270.             }
  271.             else
  272.             {
  273.                 return 0;
  274.             }
  275.         }
  276.         /// <summary>
  277.         /// 取得Int值
  278.         /// </summary>
  279.         public int GetInt(object obj)
  280.         {
  281.             if (obj != DBNull.Value)
  282.             {
  283.                 return Convert.ToInt32(obj);
  284.             }
  285.             else
  286.             {
  287.                 return 0;
  288.             }
  289.         }
  290.         /// <summary>
  291.         /// 取得UInt值
  292.         /// </summary>
  293.         public uint GetUInt(object obj)
  294.         {
  295.             if (obj != DBNull.Value)
  296.             {
  297.                 return Convert.ToUInt32(obj);
  298.             }
  299.             else
  300.             {
  301.                 return 0;
  302.             }
  303.         }
  304.         /// <summary>
  305.         /// 取得UInt64值
  306.         /// </summary>
  307.         public ulong GetULong(object obj)
  308.         {
  309.             if (obj != DBNull.Value)
  310.             {
  311.                 return Convert.ToUInt64(obj);
  312.             }
  313.             else
  314.             {
  315.                 return 0;
  316.             }
  317.         }
  318.         /// <summary>
  319.         /// 取得byte值
  320.         /// </summary>
  321.         public Byte GetByte(object obj)
  322.         {
  323.             if (obj != DBNull.Value)
  324.             {
  325.                 return Convert.ToByte(obj);
  326.             }
  327.             else
  328.             {
  329.                 return 0;
  330.             }
  331.         }
  332.         /// <summary>
  333.         /// 取得sbyte值
  334.         /// </summary>
  335.         public sbyte GetSByte(object obj)
  336.         {
  337.             if (obj != DBNull.Value)
  338.             {
  339.                 return Convert.ToSByte(obj);
  340.             }
  341.             else
  342.             {
  343.                 return 0;
  344.             }
  345.         }
  346.         /// <summary>
  347.         /// 获得Long值
  348.         /// </summary>
  349.         public long GetLong(object obj)
  350.         {
  351.             if (obj != DBNull.Value)
  352.             {
  353.                 return Convert.ToInt64(obj);
  354.             }
  355.             else
  356.             {
  357.                 return 0;
  358.             }
  359.         }
  360.         /// <summary>
  361.         /// 取得Decimal值
  362.         /// </summary>
  363.         public decimal GetDecimal(object obj)
  364.         {
  365.             if (obj != DBNull.Value)
  366.             {
  367.                 return Convert.ToDecimal(obj);
  368.             }
  369.             else
  370.             {
  371.                 return 0;
  372.             }
  373.         }
  374.         /// <summary>
  375.         /// 取得float值
  376.         /// </summary>
  377.         public float GetFloat(object obj)
  378.         {
  379.             if (obj != DBNull.Value)
  380.             {
  381.                 return Convert.ToSingle(obj);
  382.             }
  383.             else
  384.             {
  385.                 return 0;
  386.             }
  387.         }
  388.         /// <summary>
  389.         /// 取得double值
  390.         /// </summary>
  391.         public double GetDouble(object obj)
  392.         {
  393.             if (obj != DBNull.Value)
  394.             {
  395.                 return Convert.ToDouble(obj);
  396.             }
  397.             else
  398.             {
  399.                 return 0;
  400.             }
  401.         }
  402.         /// <summary>
  403.         /// 取得Guid值
  404.         /// </summary>
  405.         public Guid GetGuid(object obj)
  406.         {
  407.             if (obj != DBNull.Value)
  408.             {
  409.                 return new Guid(obj.ToString());
  410.             }
  411.             else
  412.             {
  413.                 return Guid.Empty;
  414.             }
  415.         }
  416.         /// <summary>
  417.         /// 取得DateTime值
  418.         /// </summary>
  419.         public DateTime GetDateTime(object obj)
  420.         {
  421.             if (obj != DBNull.Value)
  422.             {
  423.                 DateTime tmp;
  424.                 if (DateTime.TryParse(obj.ToString(), out tmp))
  425.                 {
  426.                     return tmp;
  427.                 }
  428.                 else
  429.                 {
  430.                     return DateTime.MinValue;
  431.                 }
  432.             }
  433.             else
  434.             {
  435.                 return DateTime.MinValue;
  436.             }
  437.         }
  438.         /// <summary>
  439.         /// 取得bool值
  440.         /// </summary>
  441.         public bool GetBool(object obj)
  442.         {
  443.             if (obj != DBNull.Value)
  444.             {
  445.                 return Convert.ToBoolean(obj);
  446.             }
  447.             else
  448.             {
  449.                 return false;
  450.             }
  451.         }
  452.         /// <summary>
  453.         /// 取得byte[]
  454.         /// </summary>
  455.         public Byte[] GetBinary(object obj)
  456.         {
  457.             if (obj != DBNull.Value)
  458.             {
  459.                 return (Byte[])obj;
  460.             }
  461.             else
  462.             {
  463.                 return null;
  464.             }
  465.         }
  466.         /// <summary>
  467.         /// 取得string值
  468.         /// </summary>
  469.         public string GetString(object obj)
  470.         {
  471.             return obj.ToString();
  472.         }
  473.         #endregion
  474.     }
  475. }