Skip to content

Instantly share code, notes, and snippets.

@drewchapin
Last active September 25, 2023 12:56
Show Gist options
  • Save drewchapin/bd51faa04884f02cc833f658fc6f6725 to your computer and use it in GitHub Desktop.
Save drewchapin/bd51faa04884f02cc833f658fc6f6725 to your computer and use it in GitHub Desktop.
/**
* @author Drew Chapin <drew@drewchapin.com>
* @date 2017/06/27
* @copyright Public Domain
*/
using System.Collections.Generic;
namespace System.Data
{
/// <summary>
/// Static class that can convert between a System Type and SqlDbType.
/// </summary>
public static class SqlDbTypeMap
{
private static Dictionary<object,object> Default;
static SqlDbTypeMap()
{
Default = new Dictionary<object,object>();
//
// SqlDBType
//
Default[SqlDbType.BigInt] = typeof(System.Int64);
Default[SqlDbType.Binary] = typeof(System.Byte[]);
Default[SqlDbType.Bit] = typeof(System.Boolean);
Default[SqlDbType.Char] = typeof(System.String);
Default[SqlDbType.Date] = typeof(System.DateTime);
Default[SqlDbType.DateTime] = typeof(System.DateTime);
Default[SqlDbType.DateTime2] = typeof(System.DateTime);
Default[SqlDbType.DateTimeOffset] = typeof(System.DateTimeOffset);
Default[SqlDbType.Decimal] = typeof(System.Decimal);
Default[SqlDbType.Float] = typeof(System.Double);
Default[SqlDbType.Image] = typeof(System.Byte[]);
Default[SqlDbType.Int] = typeof(System.Int32);
Default[SqlDbType.Money] = typeof(System.Decimal);
Default[SqlDbType.NChar] = typeof(System.String);
Default[SqlDbType.NText] = typeof(System.String);
Default[SqlDbType.NVarChar] = typeof(System.String);
Default[SqlDbType.Real] = typeof(System.Single);
Default[SqlDbType.SmallDateTime] = typeof(System.DateTime);
Default[SqlDbType.SmallInt] = typeof(System.Int16);
Default[SqlDbType.SmallMoney] = typeof(System.Decimal);
Default[SqlDbType.Text] = typeof(System.String);
Default[SqlDbType.Time] = typeof(System.TimeSpan);
Default[SqlDbType.Timestamp] = typeof(System.Byte[]);
Default[SqlDbType.TinyInt] = typeof(System.Byte);
Default[SqlDbType.UniqueIdentifier] = typeof(System.Guid);
Default[SqlDbType.VarBinary] = typeof(System.Byte[]);
Default[SqlDbType.VarChar] = typeof(System.String);
Default[SqlDbType.Variant] = typeof(System.Object);
//
// System Types
//
Default[typeof(System.Boolean)] = SqlDbType.Bit;
Default[typeof(System.Byte)] = SqlDbType.TinyInt;
Default[typeof(System.Byte[])] = SqlDbType.VarBinary;
Default[typeof(System.DateTime)] = SqlDbType.DateTime;
Default[typeof(System.DateTimeOffset)] = SqlDbType.DateTimeOffset;
Default[typeof(System.Decimal)] = SqlDbType.Decimal;
Default[typeof(System.Double)] = SqlDbType.Float;
Default[typeof(System.Guid)] = SqlDbType.UniqueIdentifier;
Default[typeof(System.Int16)] = SqlDbType.SmallInt;
Default[typeof(System.Int32)] = SqlDbType.Int;
Default[typeof(System.Int64)] = SqlDbType.BigInt;
Default[typeof(System.Object)] = SqlDbType.Variant;
Default[typeof(System.Single)] = SqlDbType.Real;
Default[typeof(System.String)] = SqlDbType.NVarChar;
Default[typeof(System.TimeSpan)] = SqlDbType.Time;
}
/// <summary>
/// Converts a System Type into an equivalent SqlDbType.
/// </summary>
/// <param name="type">The System Type to convert from</param>
/// <returns>An equivalent SqlDbType of <paramref name="type">type</paramref></returns>
/// <exception cref="System.ArgumentNullException">System.ArgumentNullException</exception>
/// <exception cref="System.Collections.Generic.KeyNotFoundException:">System.Collections.Generic.KeyNotFoundException:</exception>
public static SqlDbType Convert( Type type )
{
return (SqlDbType)Default[type];
}
/// <summary>
/// Converts a SqlDbType into the equivalent System Type
/// </summary>
/// <param name="type">The SqlDbType to convert from</param>
/// <returns>An equivalent System Type of <paramref name="type">type</paramref></returns>
/// <exception cref="System.ArgumentNullException">System.ArgumentNullException</exception>
/// <exception cref="System.Collections.Generic.KeyNotFoundException:">System.Collections.Generic.KeyNotFoundException:</exception>
public static Type Convert( SqlDbType type )
{
return (Type)Default[type];
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment