Breaking News: Grepper is joining You.com. Read the official announcement!
Check it out

SQL TO C# models generator

Claudio Vizzini answered on February 24, 2023 Popularity 2/10 Helpfulness 1/10

Contents


More Related Answers

  • sql to c# model

  • SQL TO C# models generator

    0

    declare @TableName sysname = 'TableName' declare @Result varchar(max) = 'public class ' + @TableName + '

    {'

    SELECT

    @Result = @Result + '

    public ' + ColumnType + NullableSign + ' ' + ColumnName + ' { get; set; }

    '

    FROM

    (

    SELECT

    REPLACE(col.name, ' ', '_') ColumnName,

    column_id ColumnId,

    CASE

    typ.name

    WHEN 'bigint' THEN 'long'

    WHEN 'binary' THEN 'byte[]'

    WHEN 'bit' THEN 'bool'

    WHEN 'char' THEN 'string'

    WHEN 'date' THEN 'DateTime'

    WHEN 'datetime' THEN 'DateTime'

    WHEN 'datetime2' THEN 'DateTime'

    WHEN 'datetimeoffset' THEN 'DateTimeOffset'

    WHEN 'decimal' THEN 'decimal'

    WHEN 'float' THEN 'double'

    WHEN 'image' THEN 'byte[]'

    WHEN 'int' THEN 'int'

    WHEN 'money' THEN 'decimal'

    WHEN 'nchar' THEN 'string'

    WHEN 'ntext' THEN 'string'

    WHEN 'numeric' THEN 'decimal'

    WHEN 'nvarchar' THEN 'string'

    WHEN 'real' THEN 'float'

    WHEN 'smalldatetime' THEN 'DateTime'

    WHEN 'smallint' THEN 'short'

    WHEN 'smallmoney' THEN 'decimal'

    WHEN 'text' THEN 'string'

    WHEN 'time' THEN 'TimeSpan'

    WHEN 'timestamp' THEN 'long'

    WHEN 'tinyint' THEN 'byte'

    WHEN 'uniqueidentifier' THEN 'Guid'

    WHEN 'varbinary' THEN 'byte[]'

    WHEN 'varchar' THEN 'string'

    ELSE 'UNKNOWN_' + typ.name

    END ColumnType,

    CASE

    WHEN col.is_nullable = 1

    AND typ.name IN (

    'bigint',

    'bit',

    'date',

    'datetime',

    'datetime2',

    'datetimeoffset',

    'decimal',

    'float',

    'int',

    'money',

    'numeric',

    'real',

    'smalldatetime',

    'smallint',

    'smallmoney',

    'time',

    'tinyint',

    'uniqueidentifier'

    ) THEN '?'

    ELSE ''

    END NullableSign

    FROM

    sys.columns col

    JOIN sys.types typ ON col.system_type_id = typ.system_type_id

    AND col.user_type_id = typ.user_type_id

    WHERE

    object_id = object_id(@TableName)

    ) t

    ORDER BY

    ColumnId

    SET

    @Result = @Result + '

    }' print @Result 

    Popularity 2/10 Helpfulness 1/10 Language whatever
    Link to this answer
    Share Copy Link
    Contributed on Feb 24 2023
    Claudio Vizzini
    0 Answers  Avg Quality 2/10


    X

    Continue with Google

    By continuing, I agree that I have read and agree to Greppers's Terms of Service and Privacy Policy.
    X
    Grepper Account Login Required

    Oops, You will need to install Grepper and log-in to perform this action.