Generate class from database table How can i generate a class from a table at a SQL Server?
2017-04-11 14:36
423 查看
Set @TableName to the name of your table.
declare @TableName sysname = 'TableName' 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 'string' 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
相关文章推荐
- How to Downgrade a Database from SQL Server 2005 to SQL Server 2000
- How can I generate database tables from C# classes?
- [转]How can I list all foreign keys referencing a given table in SQL Server?
- Copy a table from one database to another in SQL Server 2005
- How to read and save Images in a Sql Server Database using ADO.NET and C#
- SQL to Select a random row from a database table
- How to transform a pdf file from database@server side to client ?
- How to convert from string to datetime in sql server? - Sealyu - BlogJava
- How to move the databases that are used by SharePoint Portal Server 2003 to a computer that is running SQL Server
- 【原】获取数据库(SQL SERVER 2005)的所有信息 Get all database information from SQL Server 2005 测试通过
- How do I... Identify and delete duplicates from SQL Server tables?
- How to move databases between computers that are running SQL Server
- An example to show how to make Tree structure data table in SQL server
- Check if a database or table exists using Sql Server 2005
- 【原】获取数据库(SQL SERVER 2005)的所有信息 Get all database information from SQL Server 2005 测试通过【想写基于数据库代码生成器可参考】
- Database Engine Tuning Adviser: How to tune your new SQL Server 2005
- Map SQL Server Profiler EventClass ID to its name in a saved trace table
- Get all database from SQL Server 2005
- How to Implement an Automatic Sliding Window in a Partitioned Table on SQL Server 2005
- How to select the data type in SQLserver database such as varchar, nvarchar