1、转换为String
public static string ConvertString(this Object obj)
{
if (obj == null || obj == System.DBNull.Value)
return string.Empty;
return obj.ToString();
}
2、转换为double
public static double ConvertDouble(this Object obj)
{
double db = 0;
if (obj == null || obj == System.DBNull.Value)
return db;
bool bol = double.TryParse(obj.ToString(), out db);
if (bol)
return db;
else
return 0;
}
3、转换为int
public static int ConvertInt(this Object obj)
{
int m_int = 0;
if (obj == null || obj == System.DBNull.Value)
return m_int;
bool bol = Int32.TryParse(obj.ToString(), out m_int);
if (bol)
return m_int;
else
return 0;
}
4、转换为日期
public static DateTime ConvertDate(this Object obj)
{
DateTime m_date = DateTime.MinValue;
if (obj == null || obj == System.DBNull.Value)
return m_date;
bool bol = DateTime.TryParse(obj.ToString(), out m_date);
if (bol)
return m_date;
else
return DateTime.MinValue;
}
5、转换为时间
public static DateTime ConvertDateTime(this string str)
{
DateItem di = new DateItem();
string date = di.ConvertDateToData(str, "-");
return DateTime.Parse(date);
}