/// <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()); }
/// <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()); }