The following is an SQL Server Stored Procedure Code used to generate entity classes based on online materials:
If exists (select * From DBO. sysobjects where id = object_id (N 'procgenerateentityclass') and objectproperty (ID, n'isprocedure ') = 1)
Drop procedure procgenerateentityclass
Go
/* ===================================================== ======================================
Business Entity class Generator
This SP accepts a database object (table, view) Name's parameter and
Generates (C # code) custom entity Class Based on the object's Fields
Sample usage:
Use northwind
Exec procgenerateentityclass 'shippers'
Author: Leon C. mongoson
Modified by: Jack Tang
Modify Date: 2008-12-4
========================================================== ===================================== */
Create procedure procgenerateentityclass
@ Objectname varchar (100)
As
Declare @ name varchar (20 ),
@ Type varchar (20)
Declare objcursor cursor
For
Select SC. Name, st. Name type from syscolumns SC
Inner join policypes St
On st. xusertype = SC. xusertype
Where id = object_id (@ objectname)
Declare @ propertycodes varchar (8000)
Set @ propertycodes =''
Open objcursor
Fetch next from objcursor
Into @ name, @ Type
Declare @ ctype varchar (20) -- C # type
If @ fetch_status <> 0
Begin
Close objcursor
Deallocate objcursor
Print 'error... please check passed parameter'
Return
End
While @ fetch_status = 0
Begin
-- Convert SQL Server database data type to C # Data Type
Set @ ctype =
Case
When @ type like '% char %' or @ type like '% text %'
Then 'string'
When @ Type in ('decimal', 'numeric ')
Then 'double'
When @ type = 'real'
Then 'float'
When @ type like '% money %'
Then 'decimal'
When @ type = 'bit'
Then 'bool'
When @ type = 'bigint'
Then 'Long'
When @ type like '% int %'
Then 'int'
When @ type = 'datetime'
Then 'datetime'
Else
@ Type
End
Set @ propertycodes = @ propertycodes + char (9) + 'public' + @ ctype + ''+ @ name + '{Get; set;}' + char (13) + char (13)
Fetch next from objcursor
Into @ name, @ Type
End
-- Print '[serializable]'
Print 'public class' + @ objectname + 'info'
Print '{'
Print''
Print char (9) + 'public' + @ objectname + 'info ()'
Print char (9) + '{'
Print char (9) + '}'
Print''
Print @ propertycodes
Print '}'
Close objcursor
Deallocate objcursor
Reference address: http://www.planet-source-code.com/vb/scripts/ShowCode.asp? Txtcodeid = 1039 & lngwid = 5