Пример #1
0
        /// <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)
            {
                var obj = pi.GetValue(entity, null);
                if (obj != null)
                {
                    switch (pi.PropertyType.ToString())
                    {
                    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, obj, dbtype));
                }
            }
            return(parameter.ToArray());
        }
Пример #2
0
        /// <summary>
        /// 对象参数转换DbParameter
        /// </summary>
        /// <returns></returns>
        public static DbParameter[] GetParameter(Hashtable ht)
        {
            IList <DbParameter> parameter = new List <DbParameter>();
            DbType dbtype = new DbType();

            foreach (string key in ht.Keys)
            {
                if (ht[key] is DateTime)
                {
                    dbtype = DbType.DateTime;
                }
                else
                {
                    dbtype = DbType.String;
                }
                parameter.Add(DbParameters.CreateDbParameter(DbParameters.CreateDbParmCharacter() + key, ht[key], dbtype));
            }
            return(parameter.ToArray());
        }