Automatically Generate Code Using Visual Studio's Text Template Transformation Toolkit (T4)
page 1 of 3
Published: 24 May 2011
Unedited - Community Contributed
Visual Studio has built in support for generating code called the Text Template Transformation Toolkit. This article will demostrate how to create a class with a property for each column in a database table. The sample can easily be extended to fit any pattern you use when developing applications.
by Vince Varallo
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 29367/ 39


How many times have you found yourself creating a class that has a property for each field in a table, or creating select, insert, update, and delete stored procedures for each table in a database?  As a developer even we are subject to such mundane tasks which, as a developer, we are supposed to automate.  Did you know that Visual Studio has a built in toolkit which will allow you to automatically generate code for these tasks and it is free?  If you've installed Visual Studio then you already have it.  It's called the Text Template Transformation Toolkit (T4) and it works right within Visual Studio.

This article will demonstrate how to create a C# class file that is generated by T4.  The class will contain a property for each field in the database.  The example is simple but it will demonstrate the power of the toolkit and you can enhance the code to match the patterns you follow.  The code for the sample can be found here.

The example code uses the AdventureWorks sample database which you can download here.  Once you've installed the database you need to create a SQL Login that the application can use to connect to the database.  My sample code expects a SQL Login to be called "aspalliance" and the password should be set to "aspalliance".

View Entire Article

User Comments

Title: nfl jerseys cheap   
Name: NIKE NFL jerseys
Date: 2012-07-02 10:12:01 AM
We are professional jerseys manufacturer from china, nike nfl jerseys, mlb jerseys, nhl jerseys,nba jerseys and shoes
,heap jerseys,2012 nike nfl Jerseys,nba jersey and shorts,oklahoma city thunder jersey,official jeremy lin new york knicks jersey,NFL Jerseys Wholesale,blake griffin jersey blue,NFL jerseys For Sale online.All Our Jerseys Are Sewn On and Directly From Chinese Jerseys Factory
,Wholesale cheap jerseys,Cheap mlb jerseys,]Nike NFL Jerseys,Cheap China Wholesae,Wholesale jerseys From China,2012 nike nfl Jerseys,Jerseys From China,,2012 nike nfl Jerseys,Revolution 30 nba jerseys,jersey of nba chicago bulls direk rose ,nfl jerseys,green bay packers jerseys wholesale,Buffalo Bills nike nfl jerseys sale,good supplier soccer jerseys,cool base mlb jerseys,Revolution 30 nba jerseys,2012 stanley cup nhl jersey,
We are professional jerseys manufacturer from china, nike nfl jerseys, mlb jerseys, nhl jerseys,nba jerseys and shoes.
Title: External   
Name: Manikandan Ganesan
Date: 2011-06-19 6:26:51 PM

I used to create Project and Item templates before start any project and that helps me a lot. This is useful information and thanks for posting.

Title: External   
Name: Pyrros
Date: 2011-06-10 12:54:06 PM
How can we use this template to generate code outside this project (from another project)? Something like Add>New Item… and select the template above?
Title: mr   
Name: henry
Date: 2011-05-25 6:28:02 AM
Nice one. Pls can u modify it to iclude a method to insert to the able
Title: Mr.   
Name: Ranjan
Date: 2011-05-25 4:37:19 AM
A very good and useful thing i have found in this article for my project. Many times we work on the project which is n-tier architecture and coding for business layer is little time taking to create classes for each and every table of database in our project. So it makes it easy for developer to save their time.
Title: Mr.   
Name: S Krupa Shankar
Date: 2011-05-24 10:40:54 PM
A very good, short and crisp introduction.

And should it be dt.Columns instead of dt.Rows?

Product Spotlight
Product Spotlight 

Community Advice: ASP | SQL | XML | Regular Expressions | Windows

©Copyright 1998-2024  |  Page Processed at 2024-06-15 11:28:46 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search