namespace HW.Utility.Data
|
{
|
using System;
|
using System.ComponentModel;
|
using System.Data;
|
|
public abstract class BaseDataUtility
|
{
|
private Database dataAccess;
|
|
protected abstract Database CreateDataBase();
|
public static T GetDataByDataReader<T>(IDataReader dr, int index, T defaultT) where T: IConvertible
|
{
|
object obj2 = dr[index];
|
if ((obj2 == null) || (obj2 is DBNull))
|
{
|
return defaultT;
|
}
|
return (T) Convert.ChangeType(obj2, typeof(T));
|
}
|
|
public static T GetDataByDataReader<T>(IDataReader dr, string columnName, T defaultT) where T: IConvertible
|
{
|
object obj2 = dr[columnName];
|
if ((obj2 == null) || (obj2 is DBNull))
|
{
|
return defaultT;
|
}
|
if (typeof(T) == typeof(string))
|
{
|
obj2 = obj2.ToString();
|
}
|
return (T) Convert.ChangeType(obj2, typeof(T));
|
}
|
|
public static T GetDataByDataRow<T>(DataRow dr, int index, T defaultT) where T: IConvertible
|
{
|
object obj2 = dr[index];
|
if ((obj2 == null) || (obj2 is DBNull))
|
{
|
return defaultT;
|
}
|
if (typeof(T) == typeof(string))
|
{
|
obj2 = obj2.ToString();
|
}
|
return (T) Convert.ChangeType(obj2, typeof(T));
|
}
|
|
public static T GetDataByDataRow<T>(DataRow dr, string columnName, T defaultT) where T: IConvertible
|
{
|
object obj2 = dr[columnName];
|
if ((obj2 == null) || (obj2 is DBNull))
|
{
|
return defaultT;
|
}
|
if (typeof(T) == typeof(string))
|
{
|
obj2 = obj2.ToString();
|
}
|
return (T) Convert.ChangeType(obj2, typeof(T));
|
}
|
|
public static T? GetNullableDataByDataReader<T>(IDataReader dr, string columnName) where T: struct, IConvertible
|
{
|
T? nullable = null;
|
object obj2 = dr[columnName];
|
if (!((obj2 == null) || (obj2 is DBNull)))
|
{
|
nullable = new T?(GetDataByDataReader<T>(dr, columnName, default(T)));
|
}
|
return nullable;
|
}
|
|
public static string GetString(DataRow dr, int index)
|
{
|
return GetDataByDataRow<string>(dr, index, string.Empty);
|
}
|
|
public static string GetString(DataRow dr, string columnName)
|
{
|
return GetDataByDataRow<string>(dr, columnName, string.Empty);
|
}
|
|
public static string GetString(IDataReader dr, int index)
|
{
|
return GetDataByDataReader<string>(dr, index, string.Empty);
|
}
|
|
public static string GetString(IDataReader dr, string columnName)
|
{
|
return GetDataByDataReader<string>(dr, columnName, string.Empty);
|
}
|
|
public static string GetTrimString(DataRow dr, int index)
|
{
|
return GetString(dr, index).Trim();
|
}
|
|
public static string GetTrimString(DataRow dr, string columnName)
|
{
|
return GetString(dr, columnName).Trim();
|
}
|
|
public static string GetTrimString(IDataReader dr, int index)
|
{
|
return GetString(dr, index).Trim();
|
}
|
|
public static string GetTrimString(IDataReader dr, string columnName)
|
{
|
return GetString(dr, columnName).Trim();
|
}
|
|
public static bool IsNullValue(object obj)
|
{
|
return ((obj == null) || (obj is DBNull));
|
}
|
|
[Browsable(false)]
|
protected virtual Database DataAccess
|
{
|
get
|
{
|
if (this.dataAccess == null)
|
{
|
this.dataAccess = this.CreateDataBase();
|
if (this.dataAccess == null)
|
{
|
throw new NullReferenceException("由虚拟方法CreateDataBase生成的DataBase对象为空");
|
}
|
}
|
return this.dataAccess;
|
}
|
}
|
}
|
}
|