在使用DataRow讀取數據時,通常會遇到數據可能為Null, 但是又需要轉換為如int等其它類型的數據,因此就通常會寫這樣的代碼: if (dr[name] != DBNull.Value && dr[name] != null){ if (!int.TryParse(dr[name].ToStr ...
在使用DataRow讀取數據時,通常會遇到數據可能為Null, 但是又需要轉換為如int等其它類型的數據,因此就通常會寫這樣的代碼:
if (dr[name] != DBNull.Value && dr[name] != null)
{
if (!int.TryParse(dr[name].ToString(), out result))
throw new Exception("整形轉換失敗");
}
而且這樣的代碼通常會大段大段出現在很多地方。
對於比較懶惰的程式員來說,這樣的RY(repeat yourself)是一件很痛苦的事情,因此是時候來想個更好的方法了。這裡首先想到的就是Extension Method。
很簡單的幾句代碼,有了這個方法之後,在使用DataRow時,就可以通過擴展方法讀取數據了
model.Id = dr.SafeRead<Int32>("Id",0);
model.codeNum = dr.GetString("codeNum");
簡單但是實用的代碼,希望對大家在項目中有所幫助。
這裡參考了,一下兩篇文章,併進行了代碼重新組織,最後是整理後類源碼
使用 Extension Methods 來使 IDataReader 更加方便
public static class SystemDataExtension { #region DataReader 擴展 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> /// 獲取字元串類型數據 /// </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> /// 獲取非空類型數據 /// </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("日期格式數據轉換失敗"); } return result; } /// <summary> /// 獲取可空類型日期數據 /// </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("日期格式數據轉換失敗"); result = time; } return result; } /// <summary> /// 獲取guid類型數據 /// </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類型數據轉換失敗"); } return guid; } /// <summary> /// 獲取整形數據 /// </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("整形轉換失敗"); } return result; } /// <summary> /// 獲取雙精度類型數據 /// </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("雙精度類型轉換失敗"); } return result; } /// <summary> /// 獲取單精度類型數據 /// </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("單精度類型轉換失敗"); } return result; } /// <summary> /// 獲取decimal類型數據 /// </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類型轉換失敗"); } return result; } /// <summary> /// 獲取int16類型數據 /// </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("短整形轉換失敗"); } return result; } /// <summary> /// 獲取Byte類型數據 /// </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類型轉換失敗"); } return result; } /// <summary> /// 獲取bool類型數據如果傳值是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 擴展 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> /// 獲取字元串類型數據 /// </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> /// 獲取非空類型數據 /// </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("日期格式數據轉換失敗"); } return result; } /// <summary> /// 獲取可空類型日期數據 /// </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("日期格式數據轉換失敗"); result = time; } return result; } /// <summary> /// 獲取guid類型數據 /// </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類型數據轉換失敗"); } return guid; } /// <summary> /// 獲取整形數據 /// </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("整形轉換失敗"); } return result; } /// <summary> /// 獲取雙精度類型數據 /// </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("雙精度類型轉換失敗"); } return result; } /// <summary> /// 獲取單精度類型數據 /// </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("單精度類型轉換失敗"); } return result; } /// <summary> /// 獲取decimal類型數據 /// </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類型轉換失敗"); } return result; } /// <summary> /// 獲取int16類型數據 /// </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("短整形轉換失敗"); } return result; } /// <summary> /// 獲取Byte類型數據 /// </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類型轉換失敗"); } return result; } /// <summary> /// 獲取bool類型數據如果傳值是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 }