Generate class from database table How can i generate a class from a table at a SQL Server?
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
Generate class from database table How can i generate a class from a table at a SQL Server?的更多相关文章
- [转]SQL SERVER – Importance of Database Schemas in SQL Server
原文地址http://blog.sqlauthority.com/2009/09/07/sql-server-importance-of-database-schemas-in-sql-server/ ...
- Create a SQL Server Database on a network shared drive
(原文地址:http://blogs.msdn.com/b/varund/archive/2010/09/02/create-a-sql-server-database-on-a-network-sh ...
- Visual Studio 2012创建SQL Server Database Project提示失败解决方法
新建一个SQL Server Database Project,提示: Unable to open Database project This version of SQL Server Data ...
- ADF_Database Develop系列2_设计数据库表之Table Partitions/Create Users/Generate DDL
2013-05-01 Created By BaoXinjian
- SQL Server中模式(schema)、数据库(database)、表(table)、用户(user)之间的关系
数据库的初学者往往会对关系型数据库模式(schema).数据库(database).表(table).用户(user)之间感到迷惘,总感觉他们的关系千丝万缕,但又不知道他们的联系和区别在哪里,对一些问 ...
- P6 Professional Installation and Configuration Guide (Microsoft SQL Server Database) 16 R1
P6 Professional Installation and Configuration Guide (Microsoft SQL Server Database) 16 R1 May ...
- Mysql –>EF edmx(model first)–> Sql server table
一.mysql environment When we create an new database,first We need draw er diagram for somebody to sho ...
- Dynamic Pivot table wizard SQL Server
原文 http://www.gyurcit.hu/pivot.html Dynamic Pivot table wizard This stored procedure generate dynami ...
- Importing/Indexing database (MySQL or SQL Server) in Solr using Data Import Handler--转载
原文地址:https://gist.github.com/maxivak/3e3ee1fca32f3949f052 Install Solr download and install Solr fro ...
- Easy way to change collation of all database objects in SQL Server
This info is from: http://www.codeproject.com/Articles/302405/The-Easy-way-of-changing-Collation-of- ...
随机推荐
- [HihoCoder1398]网络流五·最大权闭合子图
题目大意:有$N$项活动$M$个人,每个活动$act_i$有一个正的权值$a_i$,每个人$stu_i$有一个负的权值$b_i$.每项活动能够被完成当且仅当该项活动所需的所有人到场.如何选择活动使最终 ...
- 在Notepad++中快捷选中多行
原文: 在Notepad++中快捷选中多行 Notepad++是Windows下一款很好用的文本编辑器. 如果需要在一个文档中选中指定的若干行,常见的办法是鼠标按住拖动.这样对于少数几行的选取还可以, ...
- 微信企业号开发之weixin://preInjectJSBridge/fail
最近几天遇到个奇怪的问题,目前只有在Andriod平台上出现:weixin://preInjectJSBridge/fail 不止我一个人遇到这个问题,群里也有好几个问了这个问题.这个问题直接导致我们 ...
- java引用类型简述
主要内容: 1.引用类型简述 2.对象的可达性 3.软引用的垃圾回收分析 4.WeakHashMap分析 5.ThreadLocal内存泄漏分析 1.引用类型简述 在Java语言中除了基本数据类型外, ...
- win7 64位系统彻底卸载mysql
一.在控制面板中卸载mysql软件,卸载后删除mysql安装目录下的所有文件 二.windows+R运行“regedit”文件,打开注册表 三.删除注册表:HKEY_LOCAL_MACHINE\SYS ...
- Hbase常用Shell命令
status 查看系统状态 hbase(main):010:0> status 1 active master, 0 backup masters, 4 servers, 0 dead, 6.5 ...
- 深入浅出的理解框架(Struts2、Hibernate、Spring)与 MVC 设计模式
现在许许多多的初学者和程序员,都在趋之若鹜地学习Web开发的宝典级框架:Struts2,Spring,Hibernate.似乎这些框架成为了一个人是否精通Java,是否会写J2EE程序的唯一事实标准和 ...
- ScrollView不能包含多个子项,ScrollView can host only one direct child
http://debuglog.iteye.com/blog/1441828 ScrollView不能包含多个子项,ScrollView can host only one direct child ...
- java maven打包jar 方法参数名变var1,var2之类的无意义参数名怎么办
这是idea 对.class反编译的结果.要想看完整源码,可以使用maven-source-plugin,在pom.xml里配置: <plugin> <groupId>org. ...
- Apache CXF JAX-WS example
1. 环境说明 jdk 1.6.0_29 apache cxf 2.7.7 2. 新建JavaProject 3. 添加jar包,将apache cxf下面lib里面的jar包都添加到项目中(可能有 ...