Click here to Skip to main content
Licence CPOL
First Posted 20 Apr 2008
Views 6,928
Downloads 69
Bookmarked 12 times

How to Build C# code based on DB Script

By | 20 Apr 2008 | Article
These Alpha App is a tool used to create C# classes based on SQL Server Express DB script
 
Part of The SQL Zone sponsored by
See Also

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)

About the Author

Xavier Cruz

Engineer

United States United States

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralFlesh it out a bit PinsitebuilderShog98:44 21 Apr '08  
GeneralCode is always welcome Pinmemberevolved3:14 21 Apr '08  
GeneralRe: Code is always welcome Pinmemberxcruzz18:08 22 Apr '08  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    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 | Mobile
Web01 | 2.5.120517.1 | Last Updated 21 Apr 2008
Article Copyright 2008 by Xavier Cruz
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid