Skip to content

Instantly share code, notes, and snippets.

@jafin
Forked from joey-qc/TSQL-to-POCO
Last active April 29, 2020 04:08
Show Gist options
  • Save jafin/a61073fc401f61b78d33eed0d4f66c61 to your computer and use it in GitHub Desktop.
Save jafin/a61073fc401f61b78d33eed0d4f66c61 to your computer and use it in GitHub Desktop.
A simple TSQL script to quickly generate c# POCO classes from SQL Server tables and views. You may tweak the output as needed. Not all datatypes are represented but this should save a bunch of boilerplate coding. USAGE: Run this query against the database of your choice. The script will loop through tables, views and their respective columns. Re…
declare @tableName varchar(200)
declare @columnName varchar(200)
declare @nullable varchar(50)
declare @datatype varchar(50)
declare @maxlen int
declare @pos int
declare @Stype varchar(50)
declare @isnullable varchar(1)
declare @Sproperty varchar(200)
DECLARE table_cursor CURSOR FOR
SELECT TABLE_NAME
FROM [INFORMATION_SCHEMA].[TABLES]
UNION ALL
SELECT TABLE_NAME
FROM [INFORMATION_SCHEMA].[VIEWS]
OPEN table_cursor
FETCH NEXT FROM table_cursor
INTO @tableName
WHILE @@FETCH_STATUS = 0
BEGIN
PRINT 'public class ' + @tableName + ' {'
DECLARE column_cursor CURSOR FOR
SELECT COLUMN_NAME, IS_NULLABLE, DATA_TYPE, isnull(CHARACTER_MAXIMUM_LENGTH,'-1'), ORDINAL_POSITION
, CASE WHEN (IS_NULLABLE = 'NO') THEN ''
ELSE
CASE WHEN (DATA_TYPE IN ('char','nchar','varchar','nvarchar')) THEN '' ELSE '?' END
END
from [INFORMATION_SCHEMA].[COLUMNS]
WHERE [TABLE_NAME] = @tableName
order by [ORDINAL_POSITION]
OPEN column_cursor
FETCH NEXT FROM column_cursor INTO @columnName, @nullable, @datatype, @maxlen, @pos, @isNullable
WHILE @@FETCH_STATUS = 0
BEGIN
-- datatype
select @sType = case @datatype
when 'int' then 'int'
when 'smallint' then 'short'
when 'bigint' then 'long'
when 'decimal' then 'Decimal'
when 'money' then 'Decimal'
when 'char' then 'string'
when 'nchar' then 'string'
when 'smallint' then 'short'
when 'varchar' then 'string'
when 'nvarchar' then 'string'
when 'uniqueidentifier' then 'Guid'
when 'datetime' then 'DateTime'
when 'bit' then 'bool'
else 'string'
END
If (@pos = 1)
PRINT ' [Key]'
If (@nullable = 'NO' AND @pos > 1)
PRINT ' [Required]'
if (@sType = 'string' and @maxLen <> '-1')
PRINT ' [MaxLength(' + convert(varchar(4),@maxLen) + ')]'
if (@sType = 'datetime')
PRINT ' [Column(TypeName = "datetime")]'
if PATINDEX('% %', @ColumnName) > 0
PRINT ' [Column("' + @columnName + '")]'
SELECT @sProperty = ' public ' + @sType + @isNullable + ' ' + REPLACE(@columnName, ' ', '') + ' { get; set; }'
PRINT @sProperty
--print ''
FETCH NEXT FROM column_cursor INTO @columnName, @nullable, @datatype, @maxlen, @pos, @isNullable
END
CLOSE column_cursor
DEALLOCATE column_cursor
print '}'
print ''
FETCH NEXT FROM table_cursor
INTO @tableName
END
CLOSE table_cursor
DEALLOCATE table_cursor
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment