SQL Server Central is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
Search:  
 
 

Generate C# class code for table

By Cade Bryant, 2007/11/16

Total article views: 1268 | Views in the last 30 days: 114

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
}

By Cade Bryant, 2007/11/16

Total article views: 1268 | Views in the last 30 days: 114
Your response
 
 
Related tags

.Net     Development    
C#     Programming    
 
Already registered?  

Free registration required

To read the rest of this article, and access thousands of other articles, we ask you to register on the site and subscribe to our newsletters.

Register

E-mail address:
Password:
Password (confirm):

  

Subscriptions

We ask you to register on the site and subscribe to our newsletters. Subscribing to our newsletters gets you:

  • ALL of our content (thousands of articles, scripts, and forum postings)
  • A daily newsletter (example)
  • A weekly news round up (example)
  • The opportunity to ask and answer questions in our forums
  • A daily Question of the Day to test and help you increase your knowledge of SQL Server.

We ask that you give the newsletter a try for a week. Over 200,000 SQL Server Professionals a day find it entertaining and useful. If not, you are welcome to unsubscribe at anytime.

Steve Jones
Editor, SQLServerCentral.com