SQL Clone
SQLServerCentral is supported by Redgate
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:
FirstName VARCHAR(128),
LastName VARCHAR(128),
Address VARCHAR(256)

By running the following:
EXEC usp_TableToClass 'Contact'

The following class code gets generated:

public class Contact

#region Constructors
public Contact()
#region Private Fields
private string _FirstName;
private string _LastName;
private string _Address;
#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; }

Total article views: 9267 | Views in the last 30 days: 8
Related Articles

how to devied fullname into firstname and lastname

how to devied fullname into firstname and lastname


How to Search Name string for space and convert lastname substring

Search Name string for space and convert lastname substring


Match firstname lastname

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


Swap firstname to lastname

I have column called fullname which has firstname lstname example John Smith How can I get it to L...


Lastname, Firstname switch

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