Created
April 1, 2020 19:39
-
-
Save justincarlson/2c4a4768498b005b8370fce85b06712e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- table settings | |
declare @decorate int = 0 | |
declare @tableName varchar(200) = 'Foo' | |
declare @database varchar(200) = 'Bar' | |
-- scripts vars | |
declare @columnName varchar(200) | |
declare @nullable varchar(50) | |
declare @datatype varchar(50) | |
declare @maxlen int | |
declare @pos int | |
declare @Stype varchar(50) | |
declare @sProperty varchar(200) | |
DECLARE table_cursor CURSOR FOR | |
SELECT TABLE_NAME | |
FROM [INFORMATION_SCHEMA].[TABLES] | |
WHERE TABLE_NAME = @tableName and [INFORMATION_SCHEMA].[TABLES].TABLE_CATALOG = @database | |
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 | |
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 | |
WHILE @@FETCH_STATUS = 0 | |
BEGIN | |
-- datatype | |
select @sType = case @datatype | |
when 'int' then 'int' | |
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 (@configure = 1 and @pos = 1) | |
PRINT ' [Key]' | |
If (@configure = 1 and @nullable = 'NO') | |
PRINT ' [Required]' | |
if (@configure = 1 and @sType = 'string' and @maxLen <> '-1') | |
Print ' [MaxLength(' + convert(varchar(4),@maxLen) + ')]' | |
SELECT @sProperty = ' public ' + @sType + ' ' + @columnName + ' { get; set; }' | |
PRINT @sProperty | |
--print '' | |
FETCH NEXT FROM column_cursor INTO @columnName, @nullable, @datatype, @maxlen, @pos | |
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