Skip to content

Instantly share code, notes, and snippets.

@paveltimofeev
Created July 5, 2011 06:51
Show Gist options
  • Save paveltimofeev/1064366 to your computer and use it in GitHub Desktop.
Save paveltimofeev/1064366 to your computer and use it in GitHub Desktop.
Addon for SqlModel framework
/// <summary>
/// Преобразует значение ячейки DataTable'а в указанный тип или возвращает значение по умолчанию, если в ящейке нет данных (DBNull)
/// </summary>
/// <typeparam name="T">Целевой тип, должен реализовывать IConvertible</typeparam>
/// <param name="value">Значение ячейки</param>
/// <param name="defaultValue">Значение по умолчанию</param>
public static T GetValue<T>(object value, T defaultValue)
where T : IConvertible
{
if (Convert.IsDBNull(value))
return defaultValue;
else
return (T)Convert.ChangeType(value, typeof(T));
}
/// <summary>
/// Returns DBNull.Value if parameter date equals new DateTime(), else return date.
/// Возвращает DBNull.Value если date == new DateTime(), в противном случае возвращает date
/// </summary>
public static object GetSqlDateOrNull(DateTime date)
{
if (date == new DateTime())
return DBNull.Value;
else
return date;
}
/// <summary>
/// Converts integer value to DBNull.Value if it is equal -1, or return itself if isn't.
/// It's useful when index value '-1' means null value.
/// </summary>
/// <param name="value">integer value</param>
/// <returns></returns>
public static object IndexToSql(int value)
{
if (value == -1)
return DBNull.Value;
else
return value;
}
/// <summary>
/// Sets timeout of command
/// </summary>
/// <param name="value">Timeout of command (seconds)</param>
public SqlModel SetCmdTimeout(int commandTimeout)
{
sqlcommand.CommandTimeout = commandTimeout;
return this;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment