Last active
April 24, 2023 17:22
-
-
Save JerryNixon/7a8ea867ef71dd8717d502a0d6ae951c to your computer and use it in GitHub Desktop.
Convert SQL Type to .NET Type
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Microsoft.SqlServer.TransactSql.ScriptDom; | |
public static class SqlUtilities | |
{ | |
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() | |
{ | |
var types = new[] | |
{ | |
SqlDataTypeOption.Sql_Variant, | |
SqlDataTypeOption.Timestamp, | |
SqlDataTypeOption.Rowversion, | |
}; | |
return types.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(); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
OK so I mentioned on Twitter that I would take a pass at it. Here is a summary of the changes I made:
dotnetType
variable and any subsequent multi-allocations and/or assignmentsGetDotNetType
toGetDotNetTypeString
, since the function does not return 'Type`.RemoveAnyPercision
by:RemoveSqlPrecision
to be more explicit about what it doesGetDotNetTypeString
to reduce allocations and simplified the return to a ternary expression.Here is the code.
I hope that helps!