Generate object classes using Stored Procedures

Source: Internet
Author: User
Tags class generator

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

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.