Click here to Skip to main content
15,919,479 members
Articles / Database Development / SQL Server
Article

How to Build C# code based on DB Script

Rate me:
Please Sign up or sign in to vote.
1.73/5 (6 votes)
20 Apr 2008CPOL3 min read 18.6K   131   12   3
These Alpha App is a tool used to create C# classes based on SQL Server Express DB script

Introduction

This application is a pretty simple code generator. After creating a script for a MS SQL Server 2005 Database, and selecting it through the generator, a folder with classes for each table in the DB script will be created. The output language is C#, and the script must be of an SQL Server database.

varchar columns will be converted to String

int columns will be converted to int

bit columns will be converted to Boolean

Default constructors, Parameters' Constructor will be created as well properties for each column.

Using the application and code

The code is available in the .zip file with a solution file. It can be changed to provide VB code as well, or support to MySQL (comming soon). There is obviously a lot of String manipulation, and perhaps the effects of using a non-script file as input may end in errors.

PScreen.JPG

The basics are:

Browse - Look into your file system for that DB script you want to be "parsed" and translatd to C#. A sample sql is provided, just to help see what kind of input should be used.

Namespace - This is by default "SAXObjects", but you'll want your c# classes to belong to your own namespace, and that this is the right place to do that.

Imports - "System" manespaces are imported by default. Add as many as you think you will need when improving the generated code.

Output Dir - If you are still wondering where exactly all the ".cs" file will be saved after generated, this is the place. After the process is complete, all ".cs" file will be available in a directory (or folder), at the same level of the sql script file used. By default the directory will be named "Classes", but feel free to change it to...perhaps "App_Code".

Author - Even though you are not making any the code yet, you will be customizing a few thingslater. Your name deserves to be in these files. Your name and the date the files were generated will appear in the first lines of every file. By default the real author of the code will be placed, but you can change it here.

Generate Options - If you don't need something that is implemented, uncheked it, and you are done!

What's the Point of Interest ?

The code was initially intended to work directly with a DB connection. This could be a good idea, but reading a DB script is better when no DB connection is available. I'm looking forward to see other people enhance the code to make it work with other languages/DB systems.

History

v1.0.0
.NET 2.0 C# and SQL Server support.
v1.2.0
Code Fixes for readability.
Imports (Optional and Configurable)
Parameters Constructor (New, Optional and Configurable)
Default Constructor (Optional and Configurable)
Static Variables (Optional, Configurable and Change in Sintax)
Output Dir (New and Configurable)
Author (New and Configurable)
Comments with Author's Name and Date/Time of Code Generation (New and Optional)
Bugfix (Empty source file selection Exception)
Layout Changes

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Engineer
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralFlesh it out a bit Pin
Shog921-Apr-08 8:44
sitebuilderShog921-Apr-08 8:44 
GeneralCode is always welcome Pin
evolved21-Apr-08 3:14
evolved21-Apr-08 3:14 
Thanks for the code, an article is preferred here so people can read from what you learned and such. But, I really recommend you check out CodeSmith and the .netTiers templates.

.netTiers: www.nettiers.com - use the SVN client and get the latest nightly, dont download the ones from the website dropdown, they are out of sync with the repository.
GeneralRe: Code is always welcome Pin
Xavier Cruz22-Apr-08 18:08
Xavier Cruz22-Apr-08 18:08 

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.