The model layer generated by the SQL stored procedure. Two styles are optional.

Source: Internet
Author: User
Tags ole

The following stored procedure is executed directly in the database and then called as an example. Provides two generated code styles

Style 1: applicable to the following frameworks 3.5

Set ansi_nulls onset quoted_identifier ongo create proc [DBO]. [p_db_wsp] @ dbname varchar (50), -- database name @ path varchar (100), -- Name of the directory where the object class is located, for example, D:/My/models @ namespace varchar (50) -- entity class namespace, default value: modelsas -- determine whether the database exists if (db_id (@ dbname) is not null) begin if (isnull (@ namespace, '') = '') set @ namespace = 'models' -- allow advanced options to be configured exec sp_configure 'show advanced options', 1 -- reconfigure -- enable OLE Automation Procedures exec sp_configure 'ole automation procedures ', 1 -- enable xp_cmdshell and write the file exec sp_configure 'xp _ cmdshell' to the disk ', 1 -- reconfigure declare @ dbsql varchar (1000), @ tablename varchar (100) set @ dbsql = 'destare WSP cursor for select name from '+ @ dbname + '.. sysobjects where xtype = ''u'' and name <> ''sysdiagrams ''' exec (@ dbsql) open WSP fetch WSP into @ tablename -- uses a cursor to traverse each table in the database cyclically while (@ F Etch_status = 0) begin -- declare @ nsql nvarchar (4000) and @ SQL varchar (8000) are combined Based on fields in the object class in the table) 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. name in (''imag'', ''uniqueidentifier'', ''ntext'', ''varchar '', ''ntext'', ''nchar '', ''nvarchar '', ''text'', ''char'') then'' string ''when. name in (''tinyint'', ''smallint'', ''int'', ''bigint'') then'''int'' when. name in (''datetime'', ''smalldatetime'') Then ''datetime'' when. name in (''float'', ''decimal', ''numeric '', ''money'', ''real'', ''smallmoney '') then ''decimal ''when. name = ''bit' 'Then''' bool ''else. name end + ''' + lower (''_'' + B. name) + '';'' + char (13) + char (9) + ''public ''+ case when. name in (''imag'', ''uniqueidentifier'', ''ntext'', ''varchar '', ''ntext'', ''nchar '', ''nvarchar '', ''text'', ''char'') then'' string ''when. name in (''tinyint'', ''smallint', ''int'') Then ''int'' when. name = ''bigint' 'then''' long ''when. name in (''datetime'', ''smalldatetime'') Then ''datetime'' when. Name in (''float'', ''decimal', ''numeric '', ''money'', ''real'', ''smallmoney '') then ''decimal ''when. name = ''bit'' then'' 'bool ''' else. 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 Nam E, xtype from '+ @ dbname + '.. policypes where status = 0) awhere. 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 + '''') -- delete exec if it exists ('exec xp_cmdshell 'echo '+ @ SQL +'> '+ @ file + '''') -- set @ SQL = NULL fetch WSP into @ tablename end close WSP deallocate WSP print is generated successfully! 'End else print 'database does not exist!'

Output style:

public string aaa{  set{ _aaa=value;}  get{return _aaa;}} 

Style 2: Applicable to Framework 3.5 and later versions (changed today)

Set ansi_nulls onset quoted_identifier ongo alter proc [DBO]. [p_db_wsp] @ dbname varchar (50), -- database name @ path varchar (100), -- Name of the directory where the object class is located, for example, D:/My/models @ namespace varchar (50) -- entity class namespace, default value: modelsas -- determine whether the database exists if (db_id (@ dbname) is not null) begin if (isnull (@ namespace, '') = '') set @ namespace = 'models' -- allow advanced options to be configured exec sp_configure 'show advanced options', 1 -- reconfigure -- enable OLE Automation P Rocedures exec sp_configure 'ole automation procedures ', 1 -- enable xp_cmdshell and write the file exec sp_configure 'xp _ cmdshell' to the disk ', 1 -- reconfigure declare @ dbsql varchar (1000), @ tablename varchar (100) set @ dbsql = 'destare WSP cursor for select name from '+ @ dbname + '.. sysobjects where xtype = ''u'' and name <> ''sysdiagrams ''' exec (@ dbsql) open WSP fetch WSP into @ tablename -- uses a cursor to traverse each table in the database cyclically while (@ fe Tch_status = 0) begin -- declare @ nsql nvarchar (4000) and @ SQL varchar (8000) are combined Based on fields in the object class in the table) 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. name in (''imag'', ''uniqueidentifier'', ''ntext'', ''varchar '', ''ntext'', ''nchar '', ''nvarchar '', ''text'', ''char'') then'' string ''when. name in (''tinyint'', ''smallint'', ''int'', ''bigint'') then'''int'' when. name in (''datetime'', ''smalldatetime'') Then ''datetime'' when. name in (''float'', ''decimal', ''numeric '', ''money'', ''real'', ''smallmoney '') then ''decimal ''when. name =' 'Bit ''then'' 'bool ''' else. name end + ''' + lower (''_'' + B. name) + '';'' + char (13) + char (9) + ''public ''+ case when. name in (''imag'', ''uniqueidentifier'', ''ntext'', ''varchar '', ''ntext'', ''nchar '', ''nvarchar '', ''text'', ''char'') then'' string ''when. name in (''tinyint'', ''smallint', ''int'') Then ''int'' when. name = ''bigint' 'then''' long ''when. name in (''datetime'', ''smalldatetime'') then'' datetim E ''when. name in (''float'', ''decimal', ''numeric '', ''money'', ''real'', ''smallmoney '') then ''decimal ''when. name = ''bit'' then'' 'bool ''' else. name end + ''' + B. name + char (13) + char (9) + ''{Get; set;'' + char (13) + char (9) + ''}'' + char (13) from '+ @ dbname + '.. syscolumns B, (select distinct name, xtype from '+ @ dbname + '.. policypes where status = 0) A where. 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 + '''') -- delete exec if it exists ('exec xp_cmdshell 'echo '+ @ SQL +'> '+ @ file + '''') -- set @ SQL = NULL fetch WSP into @ tablename end close WSP deallocate WSP print is generated successfully! 'End else print 'database does not exist!'

Output style:

public string aaa { get; set; }

Call example:

Exec [DBO]. [p_db_wsp] 'database name'. 'If your project is in that folder, write the physical path directly. For example: D:/My/models ', 'namespace name you need to specify'

Usage:
Open solution, and click "show all files" in Solution Explorer"
Right-click the generated file and select, including in the project]
Over.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.