Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save rajrao/786f778ea84ac9e309216527de02080c to your computer and use it in GitHub Desktop.
Save rajrao/786f778ea84ac9e309216527de02080c to your computer and use it in GitHub Desktop.
Convert SQL Type to .NET Type
public static class SqlUtilities
{
readonly static SqlDataTypeOption[] unsupportedTypes = new[]
{
SqlDataTypeOption.Sql_Variant,
SqlDataTypeOption.Timestamp,
SqlDataTypeOption.Rowversion,
};
public static string GetDotnetType(this SqlDataTypeOption sqlDataType, bool isNullable = false)
{
if (IsUnsupportedType())
{
return string.Empty;
}
var dotnetType = typeof(string);
switch (sqlDataType)
{
case SqlDataTypeOption.BigInt: dotnetType = typeof(long); break;
case SqlDataTypeOption.Binary:
case SqlDataTypeOption.Image:
case SqlDataTypeOption.VarBinary: dotnetType = typeof(byte[]); break;
case SqlDataTypeOption.Bit: dotnetType = typeof(bool); break;
case SqlDataTypeOption.Char: dotnetType = typeof(char); break;
case SqlDataTypeOption.Time: dotnetType = typeof(TimeOnly); break;
case SqlDataTypeOption.Date: dotnetType = typeof(DateOnly); break;
case SqlDataTypeOption.DateTime:
case SqlDataTypeOption.SmallDateTime: dotnetType = typeof(DateTime); break;
case SqlDataTypeOption.DateTime2:
case SqlDataTypeOption.DateTimeOffset: dotnetType = typeof(DateTimeOffset); break;
case SqlDataTypeOption.Decimal:
case SqlDataTypeOption.Money:
case SqlDataTypeOption.Numeric: dotnetType = typeof(decimal); break;
case SqlDataTypeOption.Float: dotnetType = typeof(double); break;
case SqlDataTypeOption.Int: dotnetType = typeof(int); break;
case SqlDataTypeOption.NChar:
case SqlDataTypeOption.NVarChar:
case SqlDataTypeOption.Text:
case SqlDataTypeOption.VarChar: dotnetType = typeof(string); break;
case SqlDataTypeOption.Real: dotnetType = typeof(float); break;
case SqlDataTypeOption.SmallInt: dotnetType = typeof(short); break;
case SqlDataTypeOption.TinyInt: dotnetType = typeof(byte); break;
case SqlDataTypeOption.UniqueIdentifier: dotnetType = typeof(Guid); break;
}
return dotnetType.Name + (isNullable ? "?" : string.Empty);
bool IsUnsupportedType()
{
return unsupportedTypes.Contains(sqlDataType);
}
}
public static string GetDotnetType(this string sqlDataType, bool isNullable = false)
{
RemoveAnyPercision();
if (!Enum.TryParse(sqlDataType, true, out SqlDataTypeOption dataTypeOption))
{
return string.Empty;
}
return dataTypeOption.GetDotnetType(isNullable);
void RemoveAnyPercision()
{
int index = sqlDataType.IndexOf("(");
if (index != -1)
{
sqlDataType = sqlDataType.Substring(0, index).Trim();
}
}
}
}
@rajrao
Copy link
Author

rajrao commented Apr 23, 2023

image
On the left is the original code and IsUnsupportedType creates a newarr each time its called.
On the right, the code uses the defined array unsupportedtypes.

image
On the right the static ctor (cctor) creates the unsupported type once and reuses

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment