Search
 
SCRIPT & CODE EXAMPLE
 

SQL

sql to c# model

-- paste this code in ssms & replace 'TableName' with the table name you want to convert.

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
Comment

PREVIOUS NEXT
Code Example
Sql :: how to recreate postgres database in docker 
Sql :: sql table backup 
Sql :: mysql size of database 
Sql :: copy from one table to another postgres using matching column 
Sql :: oracle drop type if exists 
Sql :: distinct in sql 
Sql :: match in sql server 
Sql :: sql server python connection 
Sql :: multiple row primary key 
Sql :: mysql order by on condition 
Sql :: partition-by 
Sql :: ERROR: permission denied for table accounts postgresql 13 
Sql :: sql drop all tables 
Sql :: Select All From A Table In A MySQL Database 
Sql :: dump sql file to database postgres 
Sql :: Caused by: java.lang.RuntimeException: Unable to obtain credentials to communicate with the Cloud SQL API 
Sql :: oracle compile trigger 
Sql :: intersect sql 
Sql :: load data from text file to mysql database on mac terminal 
Sql :: psql command not found windows 
Sql :: create temp table sql 
Sql :: SQL SUM() Function 
Sql :: postgres meta command to show all rows in table 
Sql :: sql where clause 
Sql :: t sql cursor tr 
Sql :: sql max count 
Sql :: sql creating tables 
Sql :: open cursors in oracle 
Sql :: create and attach user to a postgresql database 
Sql :: postgres disable foreign keys 
ADD CONTENT
Topic
Content
Source link
Name
2+7 =