Skip to content

Instantly share code, notes, and snippets.

@dannylloyd
Last active December 15, 2015 02:58
Show Gist options
  • Save dannylloyd/5190516 to your computer and use it in GitHub Desktop.
Save dannylloyd/5190516 to your computer and use it in GitHub Desktop.
Converts SQL table to VB class
declare @TableName sysname
set @TableName = 'TableName'
declare @Namespace varchar(50)
set @Namespace = 'Namespace'
declare @prop varchar(max)
PRINT 'Imports PetaPoco '
PRINT ''
PRINT 'Namespace ' + @Namespace
PRINT '<TableName("' + @TableName + '")>'
declare @primaryKey varchar(50)
set @primaryKey = (SELECT c.NAME FROM sys.key_constraints kc INNER JOIN sys.index_columns ic ON kc.parent_object_id = ic.object_id INNER JOIN sys.tables b ON kc.parent_object_id = b.OBJECT_ID INNER JOIN sys.columns c ON ic.object_id = c.object_id AND ic.column_id = c.column_id WHERE kc.type = 'PK' and b.name = @TableName)
PRINT '<PrimaryKey("' + @primaryKey + '")>'
PRINT 'Public Class ' + @TableName
declare props cursor for
select distinct ' Public Property ' + ColumnName + ' as ' + ColumnType AS prop
from (
select replace(col.name, ' ', '_') ColumnName, column_id,
case typ.name
when 'bigint' then 'long' + COALESCE((CASE col.is_nullable WHEN 1 THEN '?' end), '') when 'binary' then 'byte[]' when 'bit' then 'boolean' + COALESCE((CASE col.is_nullable WHEN 1 THEN '?' end), '') when 'char' then 'String' when 'date' then 'DateTime' + COALESCE((CASE col.is_nullable WHEN 1 THEN '?' end), '') when 'datetime' then 'DateTime' + COALESCE((CASE col.is_nullable WHEN 1 THEN '?' end), '') when 'datetime2' then 'DateTime' + COALESCE((CASE col.is_nullable WHEN 1 THEN '?' end), '') when 'datetimeoffset' then 'DateTimeOffset' when 'decimal' then 'decimal' + COALESCE((CASE col.is_nullable WHEN 1 THEN '?' end), '') when 'float' then 'float' + COALESCE((CASE col.is_nullable WHEN 1 THEN '?' end), '') when 'image' then 'byte[]' when 'int' then 'Integer' + COALESCE((CASE col.is_nullable WHEN 1 THEN '?' end), '') when 'money' then 'decimal' + COALESCE((CASE col.is_nullable WHEN 1 THEN '?' end), '') when 'nchar' then 'char' when 'ntext' then 'String' when 'numeric' then 'decimal' + COALESCE((CASE col.is_nullable WHEN 1 THEN '?' end), '') when 'nvarchar' then 'String' when 'real' then 'double' + COALESCE((CASE col.is_nullable WHEN 1 THEN '?' end), '') when 'smalldatetime' then 'DateTime' when 'smallint' then 'short' when 'smallmoney' then 'decimal' + COALESCE((CASE col.is_nullable WHEN 1 THEN '?' end), '') when 'text' then 'String' when 'time' then 'TimeSpan' when 'timestamp' then 'DateTime' + COALESCE((CASE col.is_nullable WHEN 1 THEN '?' end), '') when 'tinyint' then 'byte' when 'uniqueidentifier' then 'Guid' when 'varbinary' then 'byte[]' when 'varchar' then 'String' ELSE 'asdf'
end ColumnType
from sys.columns col join sys.types typ on col.system_type_id = typ.system_type_id
where object_id = object_id(@TableName)
) t
--order by prop
open props
FETCH NEXT FROM props INTO @prop
WHILE @@FETCH_STATUS = 0
BEGIN
print @prop
FETCH NEXT FROM props INTO @prop
END
close props
DEALLOCATE props
PRINT 'End Class'
PRINT 'End Namespace'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment