Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 

Get your favorite SSC scripts directly in SSMS with the free SQL Scripts addin. Search for scripts directly from SSMS, and instantly access any saved scripts in your SSC briefcase from the favorites tab.
Download now (direct download link)

Generate C# class code for table

By Cade Bryant,

One of the most tedious parts of developing database-driven application is coding the wrapper classes for your database objects. This is especially true if you are dealing with a database containing a large number of tables, or tables with many columns.

My proc, usp_TableToClass, can make this task much easier and less time-consuming. For any table name passed in as an argument, it generates the complete core of a C# class file corresponding to the table - including a parameterless constructor, private fields, and public properties with getters and setters.

As a very simple example, suppose we have the following table:
CREATE TABLE Contact
(
FirstName VARCHAR(128),
LastName VARCHAR(128),
Address VARCHAR(256)
)
GO

By running the following:
EXEC usp_TableToClass 'Contact'

The following class code gets generated:

public class Contact
{

#region Constructors
public Contact()
{
}
#endregion
#region Private Fields
private string _FirstName;
private string _LastName;
private string _Address;
#endregion
#region Public Properties
public string FirstName
{
get { return _FirstName; }
set { _FirstName = value; }
}
public string LastName
{
get { return _LastName; }
set { _LastName = value; }
}
public string Address
{
get { return _Address; }
set { _Address = value; }
}
#endregion
}

Total article views: 8979 | Views in the last 30 days: 15
 
Related Articles
FORUM

how to devied fullname into firstname and lastname

how to devied fullname into firstname and lastname

FORUM

Match firstname lastname

Hi, Could anyone help me with T-SQL to match names. I have to match names from one database to ba...

FORUM

How to Search Name string for space and convert lastname substring

Search Name string for space and convert lastname substring

FORUM

Lastname, Firstname switch

I dont know if this is any help but this is the code I started with: SELECT HOST9006.DESCRIPTION,...

FORUM

PARSE FULL NAME INTO LASTNAME,FIRSTNAME, MI

I have name data to import into a table. This data is in the following format: DOE/JOHN F JONE...

Tags
.net    
c#    
development    
programming    
 
Contribute

Join the most active online SQL Server Community

SQL knowledge, delivered daily, free:

Email address:  

You make SSC a better place

As a member of SQLServerCentral, you get free access to loads of fresh content: thousands of articles and SQL scripts, a library of free eBooks, a weekly database news roundup, a great Q & A platform… And it’s our huge, buzzing community of SQL Server Professionals that makes it such a success.

Join us!

Steve Jones
Editor, SQLServerCentral.com

Already a member? Jump in:

Email address:   Password:   Remember me: Forgotten your password?
Steve Jones