一个很小的MySql数据格式转化方法类,做数据库相关的开发因该会用到,记下来以备后用:
public static class DbConvert { public static int ToInt32(DataRow dataRow, string columnName) { if (dataRow[columnName] != DBNull.Value) { return Convert.ToInt32(dataRow[columnName]); } else { return 0; } } public static double ToDouble(DataRow dataRow, string columnName) { if (dataRow[columnName] != DBNull.Value) { return Convert.ToDouble(dataRow[columnName]); } else { return 0; } } public static int ToInt32(object input) { if (input == null) { return 0; } return Convert.ToInt32(input); } public static string ToString(DataRow dataRow, string columnName) { if (dataRow[columnName] != DBNull.Value) { return dataRow[columnName].ToString(); } else { return null; } } public static bool ToBoolean(DataRow dataRow, string columnName) { if (dataRow[columnName] != DBNull.Value) { return Convert.ToBoolean(dataRow[columnName]); } else { return false; } } public static DateTime ToDateTime(DataRow dataRow, string columnName) { if (dataRow[columnName] != DBNull.Value) { return Convert.ToDateTime(dataRow[columnName]); } else { return DateTime.MinValue; } } }