Skip to content

Instantly share code, notes, and snippets.

@kmoormann
Last active August 29, 2015 14:14
Show Gist options
  • Save kmoormann/d6e46680ef75d90e2855 to your computer and use it in GitHub Desktop.
Save kmoormann/d6e46680ef75d90e2855 to your computer and use it in GitHub Desktop.
C# Dto Builder Based On Information Schema
DECLARE @tableName VARCHAR(50) = 'Foo'
SELECT
'public '
+ CASE
WHEN DATA_TYPE = 'int' THEN 'int'
WHEN DATA_TYPE = 'uniqueidentifier' THEN 'Guid'
WHEN DATA_TYPE = 'nvarchar' THEN 'string'
WHEN DATA_TYPE = 'datetime' THEN 'DateTime'
WHEN DATA_TYPE = 'bit' THEN 'bool'
WHEN DATA_TYPE = 'decimal' THEN 'decimal'
ELSE DATA_TYPE
END
+ CASE WHEN (IS_NULLABLE = 'YES' AND DATA_TYPE <> 'nvarchar') THEN '?'
ELSE ''
END
+ ' '
+ COLUMN_NAME
+ ' { get; set; }'
FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = @tableName
ORDER BY ORDINAL_POSITION
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment