public static object ConvertToDataCellValue(this object o) { object objRtn = o; switch (o.GetType().ToString()) { case "System.String" : case "System.Char": if (o.ToString().Trim() == string.Empty) objRtn = DBNull.Value; break; case "System.Int32" : case "System.Decimal": case "System.Int16": case "System.Int64": case "System.UInt16": case "System.UInt32": case "System.UInt64": if (o.ConvertToIntBaseNegativeOne() == -1) objRtn = DBNull.Value; break; case "System.DateTime" : if (o.ConvertToDateTime() == DateTime.MinValue) objRtn = DBNull.Value; break; case "System.Boolean": break; default: break; } return objRtn; }
public static string ConvertToFormatString(this string source) { return string.Format("{0:yyyy-MM-dd}", source.ConvertToDateTime()); }
public static string ConvertToDataCellValueInSQL(this object o) { string strRtn = string.Empty; switch (o.GetType().ToString()) { case "System.String": case "System.Char": if (o.ToString().Trim() == string.Empty) { strRtn = "NULL"; } else { strRtn = "'" + o + "'"; } break; case "System.Int32": case "System.Decimal": case "System.Int16": case "System.Int64": case "System.UInt16": case "System.UInt32": case "System.UInt64": if (o.ToString() == string.Empty || o.ConvertToIntBaseNegativeOne() == -1) { strRtn = "NULL"; } else { strRtn = o.ToString(); } break; case "System.DateTime": if (o.ConvertToDateTime() == DateTime.MinValue) { strRtn = "NULL"; } else { strRtn = "'" + o.ToString() + "'"; } break; case "System.Boolean": strRtn = Convert.ToBoolean(o) ? "1" : "0"; break; default: if (o == null || o == DBNull.Value) { strRtn = "NULL"; } else { strRtn = "'" + o.ToString() + "'"; } break; } return strRtn; }