扩展方法

 #region DataReader扩展
    static class DataReaderExtension
    {
        /// <summary>
        /// 获取DataReader值
        /// </summary>
        /// <param name="dr">DataReader</param>
        /// <param name="name">字段名</param>
        /// <returns></returns>
        private static object GetValue(this IDataReader dr, string name)
        {
            return dr[name];
            /*for (int i = 0; i < dr.FieldCount; i++)
            {
                string column = dr.GetName(i);
                if (string.Compare(column, name, true) == 0)
                    return dr[i];
            }
            return DBNull.Value;*/
        }
        /// <summary>
        /// 获取值并且转换为布尔值
        /// </summary>
        public static bool GetBoolean(this IDataReader dr, string name)
        {
            object value = dr.GetValue(name);
            if (value != DBNull.Value)
                return (bool)value;
            else
                return false;
        }
        /// <summary>
        /// 获取值并且转换为8位无符号整数
        /// </summary>
        public static byte GetByte(this IDataReader dr, string name)
        {
            object value = dr.GetValue(name);
            if (value != DBNull.Value)
                return (byte)value;
            else
                return 0;
        }
        /// <summary>
        /// 获取值并且转换为时间
        /// </summary>
        public static DateTime GetDateTime(this IDataReader dr, string name)
        {
            object value = dr.GetValue(name);
            if (value != DBNull.Value)
                return Convert.ToDateTime(value);
            else
                return DateTime.MinValue;
        }
        /// <summary>
        /// 获取值并且转换为可空时间
        /// </summary>
        public static DateTime? GetNullDateTime(this IDataReader dr, string name)
        {
            object value = dr.GetValue(name);
            if (value != DBNull.Value)
                return Convert.ToDateTime(value);
            else
                return null;
        }
        /// <summary>
        /// 获取值并且转换为数字
        /// </summary>
        public static decimal GetDecimal(this IDataReader dr, string name)
        {
            object value = dr.GetValue(name);
            if (value is DBNull)
                return 0;
            else
                return (decimal)value;
        }
        /// <summary>
        /// 获取值并且转换为双精度小数
        /// </summary>
        public static double GetDouble(this IDataReader dr, string name)
        {
            object value = dr.GetValue(name);
            if (value != DBNull.Value)
                return (double)value;
            else
                return 0;
        }
        /// <summary>
        /// 获取值并且转换为单精度小数
        /// </summary>
        public static float GetFloat(this IDataReader dr, string name)
        {
            object value = dr.GetValue(name);
            if (value != DBNull.Value)
                return (float)value;
            else
                return 0;
        }
        /// <summary>
        /// 获取值并且转换为短整型
        /// </summary>
        public static short GetShort(this IDataReader dr, string name)
        {
            object value = dr.GetValue(name);
            if (value != DBNull.Value)
                return (short)value;
            else
                return 0;
        }
        /// <summary>
        /// 获取值并且转换为整型
        /// </summary>
        public static int GetInt(this IDataReader dr, string name)
        {
            object value = dr.GetValue(name);
            if (value != DBNull.Value)
                return (int)value;
            else
                return 0;
        }
        /// <summary>
        /// 获取值并且转换为长整形
        /// </summary>
        public static long GetLong(this IDataReader dr, string name)
        {
            object value = dr.GetValue(name);
            if (value != DBNull.Value)
                return (long)value;
            else
                return 0;
        }
        /// <summary>
        /// 获取值并且转换为字符串
        /// </summary>
        public static string GetString(this IDataReader dr, string name)
        {
            object value = dr.GetValue(name);
            if (value != DBNull.Value)
                return value.ToString();
            else
                return string.Empty;
        }
    } 
    #endregion

    #region DataRow扩展
    static class DataRowExtension
    {
        /// <summary>
        /// 获取DataReader值
        /// </summary>
        /// <param name="dr">DataReader</param>
        /// <param name="name">字段名</param>
        /// <returns></returns>
        private static object GetValue(this DataRow row, string name)
        {
            int column_index = row.Table.Columns.IndexOf(name);
            if (column_index >= 0)
                return row[column_index];
            else
                return DBNull.Value;
        }
        /// <summary>
        /// 获取值并且转换为布尔值
        /// </summary>
        public static bool GetBoolean(this DataRow row, string name)
        {
            
            object value = row[name];
            if (value != DBNull.Value)
                return (bool)value;
            else
                return false;
        }
        /// <summary>
        /// 获取值并且转换为8位无符号整数
        /// </summary>
        public static byte GetByte(this DataRow row, string name)
        {
            object value = row.GetValue(name);
            if (value != DBNull.Value)
                return (byte)value;
            else
                return 0;
        }
        /// <summary>
        /// 获取值并且转换为时间
        /// </summary>
        public static DateTime GetDateTime(this DataRow row, string name)
        {
            object value = row.GetValue(name);
            if (value != DBNull.Value)
                return Convert.ToDateTime(value);
            else
                return DateTime.MinValue;
        }
        /// <summary>
        /// 获取值并且转换为可空时间
        /// </summary>
        public static DateTime? GetNullDateTime(this DataRow row, string name)
        {
            object value = row.GetValue(name);
            if (value != DBNull.Value)
                return Convert.ToDateTime(value);
            else
                return null;
        }
        /// <summary>
        /// 获取值并且转换为数字
        /// </summary>
        public static decimal GetDecimal(this DataRow row, string name)
        {
            object value = row.GetValue(name);
            if (value is DBNull)
                return 0;
            else
                return (decimal)value;
        }
        /// <summary>
        /// 获取值并且转换为双精度小数
        /// </summary>
        public static double GetDouble(this DataRow row, string name)
        {
            object value = row.GetValue(name);
            if (value != DBNull.Value)
                return (double)value;
            else
                return 0;
        }
        /// <summary>
        /// 获取值并且转换为单精度小数
        /// </summary>
        public static float GetFloat(this DataRow row, string name)
        {
            object value = row.GetValue(name);
            if (value != DBNull.Value)
                return (float)value;
            else
                return 0;
        }
        /// <summary>
        /// 获取值并且转换为短整型
        /// </summary>
        public static short GetShort(this DataRow row, string name)
        {
            object value = row.GetValue(name);
            if (value != DBNull.Value)
                return (short)value;
            else
                return 0;
        }
        /// <summary>
        /// 获取值并且转换为整型
        /// </summary>
        public static int GetInt(this DataRow row, string name)
        {
            object value = row.GetValue(name);
            if (value != DBNull.Value)
                return (int)value;
            else
                return 0;
        }
        /// <summary>
        /// 获取值并且转换为长整形
        /// </summary>
        public static long GetLong(this DataRow row, string name)
        {
            object value = row.GetValue(name);
            if (value != DBNull.Value)
                return (long)value;
            else
                return 0;
        }
        /// <summary>
        /// 获取值并且转换为字符串
        /// </summary>
        public static string GetString(this DataRow row, string name)
        {
            object value = row.GetValue(name);
            if (value != DBNull.Value)
                return value.ToString();
            else
                return string.Empty;
        }
    }
    #endregion

 

posted @ 2019-02-20 13:44  wonderfulviews  阅读(130)  评论(0编辑  收藏  举报