Click here to Skip to main content
13,352,280 members (73,484 online)
Click here to Skip to main content
Add your own
alternative version


75 bookmarked
Posted 10 Oct 2007

Dynamically Generate C# Data Access Code for Microsoft SQL and Other Databases

, 19 Dec 2008
Rate this:
Please Sign up or sign in to vote.
With this tool, dynamically generate C# data layer code (CRUD functions) for Microsoft SQL and other databases


I have made the entire program and its code available. Please use the following links for the downloads:

Using the Program

This section is a "quick start guide", as the Word document (link above) goes into much more detail about its usage. I encourage you to, at least, download the Word document to understand the program in more detail.

However, this is a good start.

SQLCODEGEN.EXE is a command line program. It uses the input on the command-line to start the code generation process.

Call it like this:

sqlcodegen -tables Customer,Address -classtemplate FullAccess 
			-server homesqlserver -database TestDB1 

In this case, sqlcodegen will generate full CRUD for Customer and Address tables, creating classes called Customer and Address, respectively, in files called Customer.cs and Address.cs. It knows this from the -table argument.

The code will be patterned on a template named "FullAccess" as denoted by the -classtemplate argument. (The sqlcodegen.exe.config file specifies where the templates are located and the sources to use for the templates).

The server is named homesqlserver and the database to find the tables is called TestDB1, as indicated by the -server and -database arguments.

Points of Interest

SQLCODEGEN.EXE is a very flexible, easily configured program.

The program is driven by the command line input and configuration file data. It uses templates and "snippets" files as well as XML files to control the content of the output. The templates and snippet files are text files. You can easily change the templates and snippets to match your requirements by using any text editor.

In addition to the templates and snippets, SQLCODEGEN.EXE uses a few XML files for additional configuration. While I refer you to the Word documentation for more details on these files, I did want to bring your attention to these files by pointing out that these files exist. I encourage you to explore the program to see how you can modify the configuration for your needs.

One final note: I have exposed a couple of interfaces that allow you to extend the functionality of SQLCODEGEN.EXE. The interfaces are defined in the SQLCODEGENSUPPORT.DLL. One interface will allow you to extend SQLCODEGEN.EXE to work against different databases. Another allows you to customize some of the code generation so that the generated output matches the coding standards for yourself or your organization.

Final Note

Just one small note: While I do refer you to the Word document for more details, I do understand that the document is not totally complete. However, there is plenty of good information in the documentation to get you going. Along with the source code, and through contacting me, I am reasonably confident you will be able to use SQLCODEGEN.EXE immediately.

While I am not a huge fan of code generation, I do believe it has its place in software engineering. It is perfect for generating repetitive and mundane code, like CRUD (create, retrieve, update, delete) functionality. It can help reduce coding errors and save time for the fun stuff, like working on business requirements.

I know there's other tools out there that also do this, as well as others who've made their own code generation tools. But I like writing my own tools. And, I did not want to spend a significant amount of money or time learning commercially available code generation software.

So I came up with SQLCODEGEN.EXE to generate C# data access code.

As delivered, it only works for Microsoft SQL Server. However, as noted below and in the Word document, there are ways to make it work for other data sources as well.


  • 2007-11-13: I developed SQLCODEGEN.EXE against Microsoft SQL 2005. I know that system table schema for previous versions of Microsoft SQL differs so this tool, fresh from the zip file or a compile, may not work against earlier versions of Microsoft SQL. All is not lost however! You can implement your own version ITableInquiry interface, as demonstrated in the included example, in place of using the default implementation.
  • 2008-12-19: Updated source code only


This article, along with any associated source code and files, is licensed under The Common Development and Distribution License (CDDL)


About the Author

United States United States
No Biography provided

You may also be interested in...


Comments and Discussions

Generalunable to run the code Pin
zahid027-Mar-10 0:42
memberzahid027-Mar-10 0:42 
GeneralRe: unable to run the code Pin
mattraffel27-Mar-10 4:36
membermattraffel27-Mar-10 4:36 
GeneralI Get The Error Pin
yehuda13-Apr-08 1:25
memberyehuda13-Apr-08 1:25 
GeneralRe: I Get The Error Pin
mattraffel21-Apr-08 3:50
membermattraffel21-Apr-08 3:50 
I'm guessing there needs to be a few changes in your connection string. Have you looked at It seems like I got this error with SQL server express and had to change some settings in sql server itself as well. I'll post it when remember it Smile | :) Feel free to reply with your connection string and I will take a look at it.

QuestionNo Username and Password option Pin
sides_dale24-Nov-07 15:54
membersides_dale24-Nov-07 15:54 
AnswerRe: No Username and Password option Pin
mattraffel28-Nov-07 10:22
membermattraffel28-Nov-07 10:22 
GeneralClassProperty Class is missing in source project Pin
Ayyanar15-Oct-07 21:54
memberAyyanar15-Oct-07 21:54 
AnswerRe: ClassProperty Class is missing in source project [modified] Pin
mattraffel16-Oct-07 2:48
membermattraffel16-Oct-07 2:48 
GeneralRe: ClassProperty Class is missing in source project Pin
Ayyanar16-Oct-07 19:03
memberAyyanar16-Oct-07 19:03 
AnswerRe: ClassProperty Class is missing in source project Pin
mattraffel17-Oct-07 3:35
membermattraffel17-Oct-07 3:35 
GeneralnVarChar Pin
Dewey10-Oct-07 21:51
memberDewey10-Oct-07 21:51 
GeneralRe: nVarChar Pin
mattraffel11-Oct-07 3:35
membermattraffel11-Oct-07 3:35 
GeneralRe: nVarChar Pin
zahid027-Mar-10 0:41
memberzahid027-Mar-10 0:41 
GeneralRe: nVarChar Pin
mattraffel27-Mar-10 4:35
membermattraffel27-Mar-10 4:35 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.180111.1 | Last Updated 19 Dec 2008
Article Copyright 2007 by mattraffel
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid