Skip to content
Skip to content
CTASoftware Blog

Para Desenvolvedores De Software

  • Sobre Mim
← Trabalhando com Data SQL Server
Deletar Registros Duplicados MySQL →
-->

Criar classe de entidade a partir de tabela SQL Server

Posted on 31 de março de 2015 by Everton Gonçalves

Disponibilizo abaixo um script para geração de classes de entidade (BO, BE, VO) como comumente são chamadas através de script SQL Server.

CREATE PROCEDURE [dbo].[GeraVO] @tabela varchar(250)

AS
BEGIN
SET NOCOUNT ON;

declare @TableName sysname = @tabela
declare @Result varchar(max) = ‘public class ‘ + @TableName + ‘
{‘

select @Result = @Result + ‘
public ‘ + ColumnType + NullableSign + ‘ ‘ + ColumnName + ‘ { get; set; }
‘
from
(
select
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 ‘float’
when ‘image’ then ‘byte[]’
when ‘int’ then ‘int’
when ‘money’ then ‘decimal’
when ‘nchar’ then ‘char’
when ‘ntext’ then ‘string’
when ‘numeric’ then ‘decimal’
when ‘nvarchar’ then ‘string’
when ‘real’ then ‘double’
when ‘smalldatetime’ then ‘DateTime’
when ‘smallint’ then ‘short’
when ‘smallmoney’ then ‘decimal’
when ‘text’ then ‘string’
when ‘time’ then ‘TimeSpan’
when ‘timestamp’ then ‘DateTime’
when ‘tinyint’ then ‘byte’
when ‘uniqueidentifier’ then ‘Guid’
when ‘varbinary’ then ‘byte[]’
when ‘varchar’ then ‘string’
else ‘UNKNOWN_’ + typ.name
end ColumnType,
case
when col.is_nullable = 1 and typ.name in (‘bigint’, ‘bit’, ‘date’, ‘datetime’, ‘datetime2’, ‘datetimeoffset’, ‘decimal’, ‘float’, ‘int’, ‘money’, ‘numeric’, ‘real’, ‘smalldatetime’, ‘smallint’, ‘smallmoney’, ‘time’, ‘tinyint’, ‘uniqueidentifier’)
then ‘?’
else ”
end NullableSign
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 + ‘
}’

print @Result
END

GO

 

Esse script foi retirado do post http://stackoverflow.com/questions/5873170/generate-class-from-database-table apenas foi adaptado para que trabalhe como procedure, visto que todos os usuários do projeto poderão utilizar.

Execute a procedure

exec GeraVO ‘NOME_DA_TABELA’

 

Até o próximo POST

 

Compartilhe isso:

  • Clique para compartilhar no Twitter(abre em nova janela)
  • Clique para compartilhar no Facebook(abre em nova janela)
  • Clique para compartilhar no WhatsApp(abre em nova janela)
  • Clique para compartilhar no Telegram(abre em nova janela)
  • Clique para compartilhar no LinkedIn(abre em nova janela)
  • Clique para enviar um link por e-mail para um amigo(abre em nova janela)

Relacionado

About Everton Gonçalves

http://www.ctasoftware.com.br
View all posts by Everton Gonçalves
This entry was posted in .NET, SQL and tagged .NET, C#, SQL, SQL Server. Bookmark the <a href="https://www.ctasoftware.com.br/blog/criar-classe-de-entidade-a-partir-de-tabela-sql-server/" title="Permalink to Criar classe de entidade a partir de tabela SQL Server" rel="bookmark">permalink</a>.
← Trabalhando com Data SQL Server
Deletar Registros Duplicados MySQL →

Deixe um comentário Cancelar resposta

Você precisa fazer o login para publicar um comentário.

© 2026 | Blog info WordPress Theme | By Bharat Kambariya