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
}
|