Skip to content

Instantly share code, notes, and snippets.

@zonaro
Last active November 22, 2022 11:52
Show Gist options
  • Save zonaro/d8368d697b8691dbf6e783fe5d647bc4 to your computer and use it in GitHub Desktop.
Save zonaro/d8368d697b8691dbf6e783fe5d647bc4 to your computer and use it in GitHub Desktop.
Generate C# Classes from table or Views
create or ALTER procedure [dbo].[GenerateClass] (@TableName sysname = null, @GenerateProperties bit = 1, @PlainTextClass bit = 0)
as
BEGIN
declare @Result varchar(max) = 'namespace ' + replace(replace(replace(DB_NAME(),' ','_'),'.','_'),'~','_') + '
{'
DECLARE the_cursor CURSOR FAST_FORWARD
FOR SELECT TABLE_NAME
FROM INFORMATION_SCHEMA.TABLES
WHERE (TABLE_TYPE = 'BASE TABLE' or TABLE_TYPE = 'VIEW') and isnull(@TableName,TABLE_NAME) = TABLE_NAME
order by TABLE_TYPE, TABLE_NAME
OPEN the_cursor
FETCH NEXT FROM the_cursor INTO @TableName
WHILE @@FETCH_STATUS = 0
BEGIN
set @Result = @Result + '
///<summary>
///Class for '+@TableName+'
///</summary>
public partial class ' + replace(replace(replace(@TableName,' ','_'),'.','_'),'~','_') + '
{
///<summary>
///Table Name
///</summary>
public const string SQLTableName = "['+@TableName+']";
///<summary>
///DataBase Name
///</summary>
public const string SQLDataBaseName = "['+DB_NAME()+']";
///<summary>
///Return a SELECT Query for '+@TableName+'
///</summary>
public static string GetQuery() { return "SELECT * FROM " + SQLDataBaseName + "." + SQLTableName; }
///<summary>
///Return a SELECT Query for '+@TableName+'
///</summary>
public override string ToString() { return GetQuery(); }
'
select @Result = @Result + '
///<summary>
///Name: [' + ColumnName + '] | Type: '+ColumnType+' | Scale: '+cast(scale as varchar)+' | Precision: '+cast(precision as varchar)+' | Max Length: '+cast(max_length as varchar)+' | Allow Null: '+iif(is_nullable=1,'Yes','No')+'
///</summary>
public ' + case @PlainTextClass when 1 then 'string' else ColumnType end + NullableSign + ' ' + ColumnName + case @GenerateProperties when 1 then ' { get; set; }' else ';' end + '
'
from
(
select
replace(replace(replace(col.name,' ','_'),'.','_'),'~','_') ColumnName,
column_id ColumnId,
case typ.name
when 'bigint' then 'long'
when 'binary' then 'byte[]'
when 'bit' then 'bool'
when 'char' then 'string'
when 'date' then 'DateTime'
when 'datetime' then 'DateTime'
when 'datetime2' then 'DateTime'
when 'datetimeoffset' then 'DateTimeOffset'
when 'decimal' then 'decimal'
when 'float' then 'double'
when 'image' then 'byte[]'
when 'int' then 'int'
when 'money' then 'decimal'
when 'nchar' then 'string'
when 'ntext' then 'string'
when 'numeric' then 'decimal'
when 'nvarchar' then 'string'
when 'real' then 'float'
when 'smalldatetime' then 'DateTime'
when 'smallint' then 'short'
when 'smallmoney' then 'decimal'
when 'text' then 'string'
when 'time' then 'TimeSpan'
when 'timestamp' then 'long'
when 'tinyint' then 'byte'
when 'uniqueidentifier' then 'Guid'
when 'varbinary' then 'byte[]'
when 'varchar' then 'string'
else 'object'
end ColumnType,
case
when col.is_nullable = 1
then '?'
else ''
end NullableSign,
col.scale,
col.precision,
col.is_nullable,
col.max_length
from sys.columns col
join sys.types typ on
col.system_type_id = typ.system_type_id AND col.user_type_id = typ.user_type_id
where object_id = object_id(@TableName)
) t
order by ColumnId
set @Result = @Result + ' }'
FETCH NEXT FROM the_cursor INTO @TableName
END
CLOSE the_cursor
DEALLOCATE the_cursor
set @Result = @Result + '
}'
print @result
select @Result
END
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment