一区二区三区日韩精品-日韩经典一区二区三区-五月激情综合丁香婷婷-欧美精品中文字幕专区

分享

再不用擔(dān)心DataRow類型轉(zhuǎn)換和空值了(使用擴(kuò)展方法解決高頻問題)

 長(zhǎng)江黃鶴 2019-08-20
public static class SystemDataExtension
{
    #region DataReader 擴(kuò)展
    public static T SafeRead<T>(this IDataReader reader, string fieldName, T defaultValue)
    {
        try
        {
            object obj = reader[fieldName];
            if (obj == null || obj == System.DBNull.Value)
                return defaultValue;
            return (T)Convert.ChangeType(obj, defaultValue.GetType());
        }
        catch
        {
            return defaultValue;
        }
    }
    /// <summary>         
    /// 獲取字符串類型數(shù)據(jù)  
    /// </summary>    
    /// <param name="dr"></param> 
    /// <param name="name"></param>     
    /// <returns></returns>     
    public static string GetString(this IDataReader dr, string name)
    {
        if (dr[name] != DBNull.Value && dr[name] != null)
            return dr[name].ToString();
        return String.Empty;
    }
    /// <summary>         
    /// 獲取非空類型數(shù)據(jù)        
    /// </summary>         
    /// <param name="con"></param>
    /// <param name="name"></param>
    /// <returns></returns>       
    public static DateTime GetDateTime(this IDataReader dr, string name)
    {
        DateTime result = DateTime.Now;
        if (dr[name] != DBNull.Value && dr[name] != null)
        {
            if (!DateTime.TryParse(dr[name].ToString(), out result))
                throw new Exception("日期格式數(shù)據(jù)轉(zhuǎn)換失敗");
        }
        return result;
    }
    /// <summary>         
    /// 獲取可空類型日期數(shù)據(jù)        
    /// </summary>         
    /// <param name="dr"></param>        
    /// <param name="name"></param>        
    /// <returns></returns>         
    public static DateTime? GetNullDateTime(this IDataReader dr, string name)
    {
        DateTime? result = null;
        DateTime time = DateTime.Now;
        if (dr[name] != DBNull.Value && dr[name] != null)
        {
            if (!DateTime.TryParse(dr[name].ToString(), out time))
                throw new Exception("日期格式數(shù)據(jù)轉(zhuǎn)換失敗");
            result = time;
        }
        return result;
    }
    /// <summary>         
    /// 獲取guid類型數(shù)據(jù)        
    /// </summary>         
    /// <param name="dr"></param>        
    /// <param name="name"></param>        
    /// <returns></returns>         
    public static Guid GetGuid(this IDataReader dr, string name)
    {
        Guid guid = Guid.Empty;
        if (dr[name] != DBNull.Value && dr[name] != null)
        {
            if (Guid.TryParse(dr[name].ToString(), out guid))
                throw new Exception("guid類型數(shù)據(jù)轉(zhuǎn)換失敗");
        }
        return guid;
    }
    /// <summary>        
    /// 獲取整形數(shù)據(jù)        
    /// </summary>         
    /// <param name="dr"></param>        
    /// <param name="name"></param>        
    /// <returns></returns>        
    public static int GetInt32(this IDataReader dr, string name)
    {
        int result = 0;
        if (dr[name] != DBNull.Value && dr[name] != null)
        {
            if (!int.TryParse(dr[name].ToString(), out result))
                throw new Exception("整形轉(zhuǎn)換失敗");
        }
        return result;
    }
    /// <summary>        
    /// 獲取雙精度類型數(shù)據(jù)
    /// </summary>   
    /// <param name="dr"></param>  
    /// <param name="name"></param>    
    /// <returns></returns>         
    public static double GetDouble(this IDataReader dr, string name)
    {
        double result = 0.00;
        if (dr[name] != DBNull.Value && dr[name] != null)
        {
            if (!double.TryParse(dr[name].ToString(), out result))
                throw new Exception("雙精度類型轉(zhuǎn)換失敗");
        }
        return result;
    }
    /// <summary>         
    /// 獲取單精度類型數(shù)據(jù)        
    /// </summary>         
    /// <param name="dr"></param>        
    /// <param name="name"></param>        
    /// <returns></returns>         
    public static float GetSingle(this IDataReader dr, string name)
    {
        float result = 0.00f;
        if (dr[name] != DBNull.Value && dr[name] != null)
        {
            if (!float.TryParse(dr[name].ToString(), out result))
                throw new Exception("單精度類型轉(zhuǎn)換失敗");
        }
        return result;
    }
    /// <summary>         
    /// 獲取decimal類型數(shù)據(jù)        
    /// </summary>         
    /// <param name="dr"></param> 
    /// <param name="name"></param>    
    /// <returns></returns>         
    public static decimal GetDecimal(this IDataReader dr, string name)
    {
        decimal result = 0.00m;
        if (dr[name] != DBNull.Value && dr[name] != null)
        {
            if (!decimal.TryParse(dr[name].ToString(), out result))
                throw new Exception("Decimal類型轉(zhuǎn)換失敗");
        }
        return result;
    }
    /// <summary>         
    /// 獲取int16類型數(shù)據(jù)        
    /// </summary>         
    /// <param name="dr"></param>        
    /// <param name="name"></param>        
    /// <returns></returns>         
    public static Int16 GetInt16(this IDataReader dr, string name)
    {
        short result = 0;
        if (dr[name] != DBNull.Value && dr[name] != null)
        {
            if (!short.TryParse(dr[name].ToString(), out result))
                throw new Exception("短整形轉(zhuǎn)換失敗");
        }
        return result;
    }
    /// <summary>         
    /// 獲取Byte類型數(shù)據(jù)        
    /// </summary>
    ///  <param name="dr"></param>        
    /// <param name="name"></param>        
    /// <returns></returns>         
    public static byte GetByte(this IDataReader dr, string name)
    {
        byte result = 0;
        if (dr[name] != DBNull.Value && dr[name] != null)
        {
            if (!byte.TryParse(dr[name].ToString(), out result))
                throw new Exception("Byte類型轉(zhuǎn)換失敗");
        }
        return result;
    }
    /// <summary>         
    /// 獲取bool類型數(shù)據(jù)如果傳值是1或者是返回true;       
    /// </summary>         
    /// <param name="con"></param>        
    /// <param name="name"></param>        
    /// <returns></returns>         
    public static bool GetBool(this IDataReader dr, string name)
    {
        if (dr[name] != DBNull.Value && dr[name] != null)
        {
            return dr[name].ToString() == "1" || dr[name].ToString() == "是" || dr[name].ToString().ToLower() == "true";
        }
        return false;
    }
    #endregion
    #region DataRow 擴(kuò)展
    public static T SafeRead<T>(this DataRow dr, string fieldName, T defaultValue)
    {
        try
        {
            object obj = dr[fieldName];
            if (obj == null || obj == System.DBNull.Value)
                return defaultValue;
            return (T)Convert.ChangeType(obj, defaultValue.GetType());
        }
        catch
        {
            return defaultValue;
        }
    }
    /// <summary>         
    /// 獲取字符串類型數(shù)據(jù)  
    /// </summary>    
    /// <param name="dr"></param> 
    /// <param name="name"></param>     
    /// <returns></returns>     
    public static string GetString(this DataRow dr, string name)
    {
        if (dr[name] != DBNull.Value && dr[name] != null)
            return dr[name].ToString();
        return String.Empty;
    }
    /// <summary>         
    /// 獲取非空類型數(shù)據(jù)        
    /// </summary>         
    /// <param name="con"></param>
    /// <param name="name"></param>
    /// <returns></returns>       
    public static DateTime GetDateTime(this DataRow dr, string name)
    {
        DateTime result = DateTime.Now;
        if (dr[name] != DBNull.Value && dr[name] != null)
        {
            if (!DateTime.TryParse(dr[name].ToString(), out result))
                throw new Exception("日期格式數(shù)據(jù)轉(zhuǎn)換失敗");
        }
        return result;
    }
    /// <summary>         
    /// 獲取可空類型日期數(shù)據(jù)        
    /// </summary>         
    /// <param name="dr"></param>        
    /// <param name="name"></param>        
    /// <returns></returns>         
    public static DateTime? GetNullDateTime(this DataRow dr, string name)
    {
        DateTime? result = null;
        DateTime time = DateTime.Now;
        if (dr[name] != DBNull.Value && dr[name] != null)
        {
            if (!DateTime.TryParse(dr[name].ToString(), out time))
                throw new Exception("日期格式數(shù)據(jù)轉(zhuǎn)換失敗");
            result = time;
        }
        return result;
    }
    /// <summary>         
    /// 獲取guid類型數(shù)據(jù)        
    /// </summary>         
    /// <param name="dr"></param>        
    /// <param name="name"></param>        
    /// <returns></returns>         
    public static Guid GetGuid(this DataRow dr, string name)
    {
        Guid guid = Guid.Empty;
        if (dr[name] != DBNull.Value && dr[name] != null)
        {
            if (Guid.TryParse(dr[name].ToString(), out guid))
                throw new Exception("guid類型數(shù)據(jù)轉(zhuǎn)換失敗");
        }
        return guid;
    }
    /// <summary>        
    /// 獲取整形數(shù)據(jù)        
    /// </summary>         
    /// <param name="dr"></param>        
    /// <param name="name"></param>        
    /// <returns></returns>        
    public static int GetInt32(this DataRow dr, string name)
    {
        int result = 0;
        if (dr[name] != DBNull.Value && dr[name] != null)
        {
            if (!int.TryParse(dr[name].ToString(), out result))
                throw new Exception("整形轉(zhuǎn)換失敗");
        }
        return result;
    }
    /// <summary>        
    /// 獲取雙精度類型數(shù)據(jù)
    /// </summary>   
    /// <param name="dr"></param>  
    /// <param name="name"></param>    
    /// <returns></returns>         
    public static double GetDouble(this DataRow dr, string name)
    {
        double result = 0.00;
        if (dr[name] != DBNull.Value && dr[name] != null)
        {
            if (!double.TryParse(dr[name].ToString(), out result))
                throw new Exception("雙精度類型轉(zhuǎn)換失敗");
        }
        return result;
    }
    /// <summary>         
    /// 獲取單精度類型數(shù)據(jù)        
    /// </summary>         
    /// <param name="dr"></param>        
    /// <param name="name"></param>        
    /// <returns></returns>         
    public static float GetSingle(this DataRow dr, string name)
    {
        float result = 0.00f;
        if (dr[name] != DBNull.Value && dr[name] != null)
        {
            if (!float.TryParse(dr[name].ToString(), out result))
                throw new Exception("單精度類型轉(zhuǎn)換失敗");
        }
        return result;
    }
    /// <summary>         
    /// 獲取decimal類型數(shù)據(jù)        
    /// </summary>         
    /// <param name="dr"></param> 
    /// <param name="name"></param>    
    /// <returns></returns>         
    public static decimal GetDecimal(this DataRow dr, string name)
    {
        decimal result = 0.00m;
        if (dr[name] != DBNull.Value && dr[name] != null)
        {
            if (!decimal.TryParse(dr[name].ToString(), out result))
                throw new Exception("Decimal類型轉(zhuǎn)換失敗");
        }
        return result;
    }
    /// <summary>         
    /// 獲取int16類型數(shù)據(jù)        
    /// </summary>         
    /// <param name="dr"></param>        
    /// <param name="name"></param>        
    /// <returns></returns>         
    public static Int16 GetInt16(this DataRow dr, string name)
    {
        short result = 0;
        if (dr[name] != DBNull.Value && dr[name] != null)
        {
            if (!short.TryParse(dr[name].ToString(), out result))
                throw new Exception("短整形轉(zhuǎn)換失敗");
        }
        return result;
    }
    /// <summary>         
    /// 獲取Byte類型數(shù)據(jù)        
    /// </summary>
    ///  <param name="dr"></param>        
    /// <param name="name"></param>        
    /// <returns></returns>         
    public static byte GetByte(this DataRow dr, string name)
    {
        byte result = 0;
        if (dr[name] != DBNull.Value && dr[name] != null)
        {
            if (!byte.TryParse(dr[name].ToString(), out result))
                throw new Exception("Byte類型轉(zhuǎn)換失敗");
        }
        return result;
    }
    /// <summary>         
    /// 獲取bool類型數(shù)據(jù)如果傳值是1或者是返回true;       
    /// </summary>         
    /// <param name="con"></param>        
    /// <param name="name"></param>        
    /// <returns></returns>         
    public static bool GetBool(this DataRow dr, string name)
    {
        if (dr[name] != DBNull.Value && dr[name] != null)
        {
            return dr[name].ToString() == "1" || dr[name].ToString() == "是" || dr[name].ToString().ToLower() == "true";
        }
        return false;
    }
    #endregion
}

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    欧美精品一区二区三区白虎| 久久国产精品亚州精品毛片| 日韩特级黄色大片在线观看| 一区二区三区日韩经典| 日韩精品你懂的在线观看| 亚洲一区精品二人人爽久久| 激情丁香激情五月婷婷| 婷婷开心五月亚洲综合| 国产三级欧美三级日韩三级| 国产亚洲欧美日韩精品一区| 亚洲一区二区三区三州| 日韩女优精品一区二区三区| 日韩av生活片一区二区三区| 国产传媒精品视频一区| 亚洲欧洲一区二区综合精品| 日韩中文字幕欧美亚洲| 色小姐干香蕉在线综合网| 好吊一区二区三区在线看| 黄色国产一区二区三区| 国产成人精品一区在线观看| 内用黄老外示儒术出处| 午夜精品福利视频观看| 亚洲精品小视频在线观看| 国产欧美精品对白性色| 久久大香蕉精品在线观看| 国产麻豆精品福利在线| 国产欧美一区二区三区精品视| 国产精品白丝一区二区| 99国产精品国产精品九九| 精品al亚洲麻豆一区| 欧美成人欧美一级乱黄| 清纯少妇被捅到高潮免费观看| 99视频精品免费视频| 国产精品午夜性色视频| 搡老熟女老女人一区二区| 麻豆视传媒短视频在线看| 国产男女激情在线视频| 午夜久久久精品国产精品| 精品亚洲香蕉久久综合网| 国产精品尹人香蕉综合网| 亚洲欧美日韩综合在线成成|