Skip to content

Instantly share code, notes, and snippets.

@vgerbase
Created November 9, 2016 23:29
Show Gist options
  • Select an option

  • Save vgerbase/8c8ab48e67a4fcc65399e0f1ee65b9db to your computer and use it in GitHub Desktop.

Select an option

Save vgerbase/8c8ab48e67a4fcc65399e0f1ee65b9db to your computer and use it in GitHub Desktop.
Generate class properties from table
SELECT
'public '
+ CASE
WHEN c.DATA_TYPE IN ('bigint') THEN 'long'
WHEN c.DATA_TYPE IN ('int') THEN 'int'
WHEN c.DATA_TYPE IN ('smallint') THEN 'short'
WHEN c.DATA_TYPE IN ('tinyint') THEN 'byte'
WHEN c.DATA_TYPE IN ('decimal', 'numeric', 'smallmoney', 'money') THEN 'decimal'
WHEN c.DATA_TYPE IN ('real') THEN 'single'
WHEN c.DATA_TYPE IN ('float', 'decimal') THEN 'double'
WHEN c.DATA_TYPE IN ('char', 'varchar', 'nchar', 'nvarchar', 'text', 'ntext') THEN 'string'
WHEN c.DATA_TYPE IN ('smalldatetime', 'datetime', 'datetime2', 'date') THEN 'DateTime'
WHEN c.DATA_TYPE IN ('time') THEN 'TimeSpan'
WHEN c.DATA_TYPE IN ('datetimeoffset') THEN 'DateTimeOffset'
WHEN c.DATA_TYPE IN ('bit') THEN 'bool'
WHEN c.DATA_TYPE IN ('uniqueidentifier') THEN 'Guid'
WHEN c.DATA_TYPE IN ('binary', 'varbinary', 'image', 'timestamp', 'rowversion') THEN 'byte[]'
WHEN c.DATA_TYPE IN ('xml') THEN 'Xml'
END
+ CASE
WHEN c.IS_NULLABLE = 'YES' AND c.DATA_TYPE <> 'varchar' THEN '? '
ELSE ' '
END
+ c.COLUMN_NAME + ' { get; set; }'
FROM information_schema.columns c
WHERE c.table_name = 'Premio'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment