Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

How to Build C# code based on DB Script

0.00/5 (No votes)
20 Apr 2008 1  
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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here