Last active
August 18, 2020 07:09
-
-
Save RickyLin/c3b03e3103efbdb528f1d5ba8325f84f to your computer and use it in GitHub Desktop.
Convert SQL data type to C# data type
This file contains 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
ALTER FUNCTION [dbo].[fnGetCSharpTypeNameFromSQLTypeName] | |
( | |
@SQLTypeName NVARCHAR(64) | |
, @IsNullable BIT | |
) | |
RETURNS NVARCHAR(64) | |
AS | |
-- TEST: SELECT dbo.fnGetCSharpTypeNameFromSQLTypeName('MONEY', 1) | |
BEGIN | |
DECLARE @Result NVARCHAR(64) | |
SET @Result = CASE @SQLTypeName | |
WHEN 'VARCHAR' THEN 'string' | |
WHEN 'NVARCHAR' THEN 'string' | |
WHEN 'CHAR' THEN 'string' | |
WHEN 'NCHAR' THEN 'string' | |
WHEN 'INT' THEN 'int' | |
WHEN 'MONEY' THEN 'decimal' | |
WHEN 'NUMERIC' THEN 'decimal' | |
WHEN 'BIT' THEN 'bool' | |
WHEN 'IMAGE' THEN 'byte[]' | |
WHEN 'DATETIME' THEN 'DateTime' | |
WHEN 'SMALLDATETIME' THEN 'DateTime' | |
ELSE @SQLTypeName + ' // <-- can not convert' | |
END | |
IF @Result <> 'string' AND @IsNullable = 1 | |
BEGIN | |
SET @Result = @Result + '?' | |
END | |
RETURN @Result | |
END |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment