Important alert: (current site time 7/15/2013 8:01:51 PM EDT)
 

VB icon

Business Entity Class Generator

Email
Submitted on: 1/13/2006 8:10:56 PM
By: Leon Tayson  
Level: Intermediate
User Rating: By 9 Users
Compatibility: SQL Server 2000, SQL Server 7.0
Views: 18119
author picture
(About the author)
 
     This SP accepts a database object name (table, view) parameter and generates (C# code) custom entity class based on the object's fields. You can then copy and paste the generated code to your C# class.

 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
--**************************************
-- Name: Business Entity Class Generator
-- Description:This SP accepts a database object name (table, view) parameter and generates (C# code) custom entity class based on the object's fields. You can then copy and paste the generated code to your C# class.
-- By: Leon Tayson
--
-- Inputs:@ObjectName - name of your table, view
--
-- Returns:C# codes for your business entity object
--
--This code is copyrighted and has-- limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=1039&lngWId=5--for details.--**************************************

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. Tayson
======================================================================*/
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 systypes st
ON st.xusertype = sc.xusertype
WHERE Id=OBJECT_ID(@ObjectName)
DECLARE @declarationCodes varchar(8000),
@ctorCodes varchar(8000),
@propertyCodes varchar(8000)
SET @declarationCodes = ''
SET @ctorCodes = ''
SET @propertyCodes = ''
OPEN objCursor
FETCH NEXT FROM objCursor
INTO @name, @type
DECLARE @cType varchar(20)-- C# type
DECLARE @ctorParms varchar(4000)
SET @ctorParms = ''
IF @@FETCH_STATUS <> 0
BEGIN
CLOSE objCursor
DEALLOCATE objCursor
PRINT 'Error... Please check passed parameter'
RETURN
END
WHILE @@FETCH_STATUS = 0
BEGIN
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'
ELSE
@type
END
--PRINT CHAR(9) + 'private ' + @ctype + ' ' + 'm_' + @name + ';'
SET @declarationCodes = @declarationCodes + CHAR(9) + 'private ' + @ctype + ' ' + 'm_' + @name + ';' + CHAR(13)
SET @ctorCodes = @ctorCodes + CHAR(9) + CHAR(9) + 'm_' + @name + ' = ' + LOWER(LEFT(@name, 1)) + SUBSTRING(@name, 2, LEN(@name)) + ';' + CHAR(13)
SET @ctorParms = @ctorParms + @ctype + ' ' + LOWER(LEFT(@name, 1)) + SUBSTRING(@name, 2, LEN(@name)) + ', '
SET @propertyCodes = @propertyCodes + CHAR(9) + 'public ' + @ctype + ' ' + @name + CHAR(13) +
CHAR(9) + '{' + CHAR(13) +
CHAR(9) + CHAR(9) + 'get { return m_' + @name + '; }' + CHAR(13) +
CHAR(9) + CHAR(9) + 'set { m_' + @name + ' = value; }' + CHAR(13) +
CHAR(9) + '}' + CHAR(13)
FETCH NEXT FROM objCursor
INTO @name, @type
END
PRINT '[Serializable]'
PRINT 'public class ' + @ObjectName + 'Info'
PRINT '{'
PRINT @declarationCodes
PRINT ''
PRINT CHAR(9) + 'public ' + @ObjectName + 'Info(' + LEFT(@ctorParms, LEN(@ctorParms) - 1) + ')'
PRINT CHAR(9) + '{'
PRINT @ctorCodes
PRINT CHAR(9) + '}'
PRINT ''
PRINT @propertyCodes
PRINT '}'
CLOSE objCursor
DEALLOCATE objCursor


Other 1 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Intermediate category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments
1/15/2006 9:42:38 PMJoseph A. De Guzman

Nice! Why didn't I think of that before.
(If this comment was disrespectful, please report it.)

 
11/12/2008 8:35:37 PMNobuo

How greate idea!! Thank you.
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.