zoukankan
html css js c++ java
SQL Server 根据数据库名创建实体类
set ANSI_NULLS ON set QUOTED_IDENTIFIER ON go -- ============================================= -- Author: <shipeng.wang> -- Create date: <2009-09-14> -- Description: <根据数据库名创建实体类> -- ============================================= create proc [dbo].[p_db_wsp] @dbname varchar(50), --数据库名 @path varchar(100), --实体类所在目录名,如D:/My/Models @namespace varchar(50) --实体类命名空间,默认值为Models as --判断数据库是否存在 if(db_id(@dbname)is not null) begin if(isnull(@namespace,'')='') set @namespace='Models' -- 允许配置高级选项 EXEC sp_configure 'show advanced options', 1 -- 重新配置 RECONFIGURE -- 启用Ole Automation Procedures EXEC sp_configure 'Ole Automation Procedures', 1 -- 启用xp_cmdshell,可以向磁盘中写入文件 EXEC sp_configure 'xp_cmdshell', 1 -- 重新配置 RECONFIGURE declare @dbsql varchar(1000),@tablename varchar(100) set @dbsql='declare wsp cursor for select name from '+@dbname+'..sysobjects where xtype=''u'' and name <>''sysdiagrams''' exec(@dbsql) open wsp fetch wsp into @tablename--使用游标循环遍历数据库中每个表 while(@@fetch_status=0) begin --根据表中字段组合实体类中的字段和属性 declare @nsql nvarchar(4000),@sql varchar(8000) set @nsql='select @s=isnull(@s+char(9)+''private '',''using System;'+char(13)+'using System.Collections.Generic;' +char(13)+'using System.Text;'+char(13)+'namespace '+@namespace+char(13)+ '{'+char(13)+char(9)+'public class '+@tablename+char(13)+'{''+char(13)+char(9)+''private '')+ case when a.name in(''image'',''uniqueidentifier'',''ntext'',''varchar'',''ntext'',''nchar'',''nvarchar'',''text'',''char'') then ''string'' when a.name in(''tinyint'',''smallint'',''int'',''bigint'') then ''int'' when a.name in(''datetime'',''smalldatetime'') then ''DateTime'' when a.name in(''float'',''decimal'',''numeric'',''money'',''real'',''smallmoney'') then ''decimal'' when a.name =''bit'' then ''bool'' else a.name end+'' ''+lower(''_''+b.name)+'';''+char(13)+char(9)+''public ''+ case when a.name in(''image'',''uniqueidentifier'',''ntext'',''varchar'',''ntext'',''nchar'',''nvarchar'',''text'',''char'') then ''string'' when a.name in(''tinyint'',''smallint'',''int'') then ''int'' when a.name=''bigint'' then ''long'' when a.name in(''datetime'',''smalldatetime'') then ''DateTime'' when a.name in(''float'',''decimal'',''numeric'',''money'',''real'',''smallmoney'') then ''decimal'' when a.name =''bit'' then ''bool'' else a.name end +'' ''+b.name+char(13)+char(9)+''{''+char(13)+char(9)+char(9)+''get{return ''+lower(''_''+b.name)+'';}''+ char(13)+char(9)+char(9)+''set{''+lower(''_''+b.name)+''=value;}''+char(13)+char(9)+''}''+char(13) from '+@dbname+'..syscolumns b, (select distinct name,xtype from '+@dbname+'..systypes where status=0) a where a.xtype=b.xtype and b.id=object_id('''+@dbname+'..'+@tablename+''')' exec sp_executesql @nsql,N'@s varchar(8000) output',@sql output set @sql=@sql+char(9)+'}'+char(13)+'}' --print @sql DECLARE @err INT,@fso INT,@fleExists BIT,@file VARCHAR(100) SET @file=@path+'/'+@tablename+'.cs' EXEC @err=sp_OACreate 'Scripting.FileSystemObject',@fso OUTPUT EXEC @err=sp_OAMethod @fso, 'FileExists',@fleExists OUTPUT,@file EXEC @err = sp_OADestroy @fso IF @fleExists!=0 exec('exec xp_cmdshell ''del '+@file+'''') --存在则删除 exec('exec xp_cmdshell ''echo '+@sql+' > '+@file+'''') --将文本写进文件中 set @sql=null fetch wsp into @tablename end close wsp deallocate wsp print '生成成功!' end else print '数据库不存在!'
查看全文
相关阅读:
预备作业03 20162311张之睿
[LeetCode 题解]: String to Interger (atoi)
[LeetCode 题解]: Add Two Numbers
[LeetCode 题解]: Interger to Roman
[LeetCode 题解]: Longest Substring Without Repeating Characters
[LeetCode 题解]: Roman to Interger
[LeetCode 题解]: palindromes
[LeetCode 题解]: Two Sum
[LeetCode 题解]: Maximum Subarray
[LeetCode 题解]:Gas Station
原文地址:https://www.cnblogs.com/javawebsoa/p/2458206.html
最新文章
常用的DOS命令总结
Java爬虫之抓取一个网站上的全部链接
MySQL的慢查询日志
在Linux环境下安装部署MySQL数据库系统实例
Java中的常量如何避免反模式
SQL语句优化之提高查询效率
如何搭建lamp(CentOS7+Apache+MySQL+PHP)环境
git出现refusing to merge unrelated histories
centos7安装notepadqq
UEditor调用上传图片、上传文件等模块
热门文章
jQuery.form.js使用
PHP超全局变量$_SERVER
frame的用法
thinkphp 5.6以上版本出现No input file specified解决办法
【转】史上最详细的Composer安装tp5教程
tp学习笔记1
预备作业03 20162320刘先润
预备作业02 20162320刘先润
预备作业01
20162311 《程序设计与数据结构》第一周学习总结
Copyright © 2011-2022 走看看