/// <summary>
 /// 为即将执行准备一个命令
 /// </summary>
 /// <param name="conn">SqlConnection对象</param>
 /// <param name="cmd">SqlCommand对象</param>
 /// <param name="isOpenTrans">DbTransaction对象</param>
 /// <param name="cmdType">执行命令的类型(存储过程或T-SQL,等等)</param>
 /// <param name="cmdText">存储过程名称或者T-SQL命令行, e.g. Select * from Products</param>
 /// <param name="dbParameter">执行命令所需的sql语句对应参数</param>
 private void PrepareCommand(DbConnection conn, IDbCommand cmd, DbTransaction isOpenTrans, CommandType cmdType, string cmdText, params DbParameter[] dbParameter)
 {
     if (conn.State != ConnectionState.Open)
     {
         conn.Open();
     }
     cmd.Connection  = conn;
     cmd.CommandText = cmdText;
     if (isOpenTrans != null)
     {
         cmd.Transaction = isOpenTrans;
     }
     cmd.CommandType = cmdType;
     if (dbParameter != null)
     {
         dbParameter = DbParameters.ToDbParameter(dbParameter);
         foreach (var parameter in dbParameter)
         {
             cmd.Parameters.Add(parameter);
         }
     }
 }
        /// <summary>
        /// 哈希表生成Insert语句
        /// </summary>
        /// <param name="tableName">表名</param>
        /// <param name="ht">Hashtable</param>
        /// <returns>int</returns>
        public static StringBuilder InsertSql(string tableName, Hashtable ht)
        {
            StringBuilder sb = new StringBuilder();

            sb.Append(" Insert Into ");
            sb.Append(tableName);
            sb.Append("(");
            StringBuilder sp       = new StringBuilder();
            StringBuilder sb_prame = new StringBuilder();

            foreach (string key in ht.Keys)
            {
                if (ht[key] != null)
                {
                    sb_prame.Append("," + key);
                    sp.Append("," + DbParameters.CreateDbParmCharacter() + "" + key);
                }
            }
            sb.Append(sb_prame.ToString().Substring(1, sb_prame.ToString().Length - 1) + ") Values (");
            sb.Append(sp.ToString().Substring(1, sp.ToString().Length - 1) + ")");
            return(sb);
        }
        /// <summary>
        /// 对象参数转换DbParameter
        /// </summary>
        /// <returns></returns>
        public static DbParameter[] GetParameter <T>(T entity)
        {
            IList <DbParameter> parameter = new List <DbParameter>();
            DbType dbtype = new DbType();
            Type   type   = entity.GetType();

            PropertyInfo[] props = type.GetProperties();
            foreach (PropertyInfo pi in props)
            {
                if (pi.GetValue(entity, null) != null)
                {
                    switch (pi.PropertyType.ToString())
                    {
                    case "System.Nullable`1[System.Int64]":
                        dbtype = DbType.Int64;
                        break;

                    case "System.Nullable`1[System.Int32]":
                        dbtype = DbType.Int32;
                        break;

                    case "System.Nullable`1[System.Decimal]":
                        dbtype = DbType.Decimal;
                        break;

                    case "System.Nullable`1[System.DateTime]":
                        dbtype = DbType.DateTime;
                        break;

                    default:
                        dbtype = DbType.String;
                        break;
                    }
                    parameter.Add(DbParameters.CreateDbParameter(DbParameters.CreateDbParmCharacter() + pi.Name, pi.GetValue(entity, null), dbtype));
                }
            }
            return(parameter.ToArray());
        }
        /// <summary>
        /// 泛型方法,反射生成InsertSql语句
        /// </summary>
        /// <param name="entity">实体类</param>
        /// <returns>int</returns>
        public static StringBuilder InsertSql <T>(T entity)
        {
            Type          type = entity.GetType();
            StringBuilder sb   = new StringBuilder();

            sb.Append(" Insert Into ");
            sb.Append(EntityAttribute.GetEntityTable <T>());
            sb.Append("(");
            StringBuilder sp       = new StringBuilder();
            StringBuilder sb_prame = new StringBuilder();

            PropertyInfo[] props = type.GetProperties();
            foreach (PropertyInfo prop in props)
            {
                if (prop.GetValue(entity, null) != null)
                {
                    sb_prame.Append("," + (prop.Name));
                    sp.Append("," + DbParameters.CreateDbParmCharacter() + "" + (prop.Name));
                }
            }
            sb.Append(sb_prame.ToString().Substring(1, sb_prame.ToString().Length - 1) + ") Values (");
            sb.Append(sp.ToString().Substring(1, sp.ToString().Length - 1) + ")");
            return(sb);
        }
 /// <summary>
 /// 拼接删除SQL语句
 /// </summary>
 /// <param name="tableName">表名</param>
 /// <param name="pkName">字段主键</param>
 /// <returns></returns>
 public static StringBuilder DeleteSql(string tableName, string pkName)
 {
     return(new StringBuilder("Delete From " + tableName + " Where " + pkName + " = " + DbParameters.CreateDbParmCharacter() + pkName + ""));
 }