Click here to Skip to main content
15,860,972 members
Articles / Programming Languages / Visual Basic

Database Helper v 2.0.0

Rate me:
Please Sign up or sign in to vote.
4.68/5 (75 votes)
8 Jun 2010CPOL8 min read 207.9K   21.6K   270   42
An open source code generation utility with some useful features to generate procedures,class for tables and .net code for procedures automatically.
DatabaseHelper

Background

When we want to create database applications, we must interact repeatedly with a database. At the code level, for each interaction with the database, we must create a connection, define the command that will be executed and finally get any results. Quite often the database access code is troublesome and it's not optimized for high performance database applications. For best results in terms of creating database applications in less time and with higher performance, the best practice is to separate the Data Access Layer from our main application code. Database Helper serves exactly that purpose and helps produce database applications easier and faster.

About Database Helper

Database Helper is an open source application that helps programming and interacting with a database. It has three useful tools which will be described in the following sections

  1. Generate Stored Procedures Code in Database Level
  2. Generate .NET (VB and C#) Single Class and Collection Class for each table in database (BLL)
  3. Generate .NET (VB and C#) Code for Stored Procedures and some other useful methods (DAL)

Generate Stored Procedures Code in Database

This tool, creates 5 stored procedures for each table including SelectAll, SelectRow, Insert, Update, Delete. I want to thank Mr. Keramati for writing this tool and giving me the source code to merge with my other tools. His original version generated 5 procedures for a selected table, but I customized it to generate the 5 stored procedures for all of tables in database. Mr. Keramati has also published his source code on CodePlex seperately.

The output of this tool is a folder named Sql containing a single file with .sql extension with all of the stored procedures generated for all tables in database.

Generate .NET Classes (Business Logic Layer)

This tool, creates two classes for each table. The first class helps you write your code more strongly-typed, while the second class is a Collection class (for first class) inherited from the CollectionBase class to hold data rows in memory like DataTable. This permits working with many rows in a strongly-typed manner and you can even bind a DataGridView.DataSource to this object. The classes may be generated in either C# or VB.NET language in a namespace that has same name as the database.

The output of this tool is a folder named BLL that contains a .NET source file including all the classes that were generated from tables in the database.

Generate .NET Code for Stored Procedures (Data Access Layer)

This tool extracts all stored procedures from the database and creates a method for each one including method parameters, if any. In other words, it will create the Data Access Layer for you.

All generated methods return a DataTable object, if DataTable.Rows > 0, otherwise, the returned DataTable object does not have any valid rows.

It also generates the following additional methods for easier interaction with the database.

  1. First overload of the constructor that takes the full connection string as a parameter. You can access the connection string via the ConnectionString property.
  2. Second overload of the constructor that takes two parameters, ServerName and DataBaseName, and creates a Trusted Connection. You can access the connection string via the ConnectionString property.
  3. Third overload of the constructor that takes ServerName,DatabaseName, UserName and Password as parameters and creates an Untrusted Connection. You can access the connection string via the ConnectionString property.
  4. GetData(): This method takes a SqlCommand as a parameter and returns the query result as DataTable object. Note: you don't need to pass a connection to the constructor of SqlCommand object. It will be initialized inside the GetData() method.
  5. ExecNonQuery(): This method executes a non-query SQL command and like GetData(), takes a SqlCommand as a parameter. It then return an int as result.

The output of this tool is a .NET source file named DAL, containing a class named DAL in the Database namespace.

How Database Helper Works

Database Helper is very easy to use following these steps:

  1. Enter the Data Source in the corresponding ComboBox.
  2. Click the Connect button to load all databases on your server (this takes a few minutes). After loading databases, each database node has two sub nodes: Stored Procedures and Tables.
  3. Select which database that you want to generate code for (or each sub node of specified detabase).
  4. In the output section, select an option. If you want to generate T-SQL code for stored procedures at the database level, then select Generate Stored Procedures radio button. If you want to generate .NET code for existing stored procedures and create the data access layer, then select Generate Code for Stored Procedures (DAL) radio button and select a language for code generation.
  5. If you want to generate .NET class code for the tables, select Generate Tables Classes (BLL) check box (optional) and select a language for code generation.
  6. Select output path for result.
  7. Click the Generate Code button. Database Helper will generate the output results in specified directory. If your output is .NET code, then you must add it to your project along with a using directive.

How to Work with the Sample Project

Download the sample file attached to this article and open zip file. If you run TestDb_Script.sql in your Sql Server Management Studio (SSMS), it will create a sample database named TestDB that contains a table named Student and some records to work with.

Next, run DbHelper from the debug folder, initialize the connection options (DataSource, User, Password, etc.) and then click Connect. All of the databases on the current DataSource will be loaded. Select the TestDb database that we created earlier.

Because this database does not have any stored procedures, in the output, select Generate Stored Procedures, select the directory path (language is not important in this mode), then click Generate Code. Open the file that is created (for example, Your Path\Sql\TestDB_Sps.Sql) with SSMS and run it in your TestDb database.

Then, run DbHelper again (or click Connect again if you didn't close it) and again select the TestDb database.

In the output, select Generate Code for Stored Procedures (DAL), and check Generate Tables Classes (BLL). Select a language and path, then click Generate Code. A class sourceFile named DAL.cs or DAL.vb will be created which includes the code for the stored procedures and Data Access Layer along with a folder named BLL which contains TestDb.cs or TestDb.vb that includes all the classes code for the tables. You must add these files and a using directive to your solution.

New Functionality in Database Helper v 2.0.0 (Plugins)

This version of Database Helper has a small SDK to extend its funtionality. PlugIns are *.dll files and exist in the PlugIns folder, for example PlugIns\SamplePlugInforDbHelper.dll. When the program is started, it reads the available plugins in the PlugIns folder and adds them to the PlugIns menu automatically. When the user selects a PlugIn from the PlugIns menu, the plugin executes.

To create a new plugin for Database Helper, follow these steps:

  1. Create a new Class Library project.
  2. Add reference to DatabaseHelperSdk.dll.
  3. Create a new class that implements the IPlugIn interface (this interface is in the DatabaseHelperSdk namespace).
  4. Implement the three methods defined in the IPlugIn interface):
    • InitializeComponent: This method is the main method that initializes the plugIn and adds it to the PlugIns menu.
    • Author: This method contains information about the plugin's author.
    • Description: This method contains a description of the plugin.
  5. Create a Click event for your plugin menu in the InitializeComponent method and implement its event handler method to execute the plugin code when selected.
  6. Compile your project and save the created assembly (with .dll extension) in the Database Helper\PlugIns folder. Now when Database Helper starts, you should see your plugin in the PlugIns menu.

For more information to know how to create plugins, please download the SamplePlugin project from the URL at the top of this article.

History

Version 1.0.0

Database Helper was written in C# 2.0 and the source code is available from the URL above. To generate .NET code, I used the System.CodeDom and System.CodeDom.Compiler namespaces. For loading database information, I used the SQLDMO.dll library. This library is quite good, but takes a long time to load the database info.

Version 2.0.0

In Version 2.0.0 I used Smo (Sql Server Management Objects) instead of Dmo (Distributed Management Objects) to work with Sql Server (2005) easier and use full multi-threading (thanks to Anonymous Delegates). The database info loading algorithm was optimized, which made loading the database information about 10 times faster.

The new release of Database Helper Version 2.0 has some important changes as follow:

  1. Use of SMO (Sql Server Management Objects) instead of DMO (Distributed Management Objects) to load database information.
  2. Changed the algorithm for loading database information to full multi-threading, improving the speed about 10 times faster.
  3. Changed Insert stored procedure to use all types, including characters, integers, and unique identifiers. In version 1.0.0, it only with integers.
  4. Changed Insert method in .NET code to return the inserted record using the out keyword.
  5. Added plugin support and created SDK for it to facilitate third party development. The sample plugin application can be downloaded from the URL at the top of this article)

Finally

Thank you for reading my article, I hope enjoy it. If you encounter any problems for any problems or have any questions please email me at: sst.server@gmail.com.

Regards,
Hamed Vaziri.

License

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


Written By
Database Developer
Iran (Islamic Republic of) Iran (Islamic Republic of)
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionSome problems with the tool - resolved Pin
DumpsterJuice6-Jan-15 5:01
DumpsterJuice6-Jan-15 5:01 
GeneralMy vote of 5 Pin
skynet200912-Nov-12 12:03
skynet200912-Nov-12 12:03 
GeneralMy vote of 5 Pin
Kanasz Robert5-Nov-12 2:47
professionalKanasz Robert5-Nov-12 2:47 
QuestionHere is a SQL Server 2008 Version(.Net 4.0) Pin
Dewey10-Jul-12 14:12
Dewey10-Jul-12 14:12 
AnswerRe: Here is a SQL Server 2008 Version(.Net 4.0) Pin
mouhannad14-Nov-13 23:03
mouhannad14-Nov-13 23:03 
GeneralSQL 2008 Pin
Gene Magerr4-Jun-11 4:37
Gene Magerr4-Jun-11 4:37 
GeneralError running example [modified] Pin
Thomas A10-Feb-11 6:47
Thomas A10-Feb-11 6:47 
GeneralRe: Error running example Pin
hdv21212-Feb-11 10:15
hdv21212-Feb-11 10:15 
GeneralLooks nice! Pin
Shane Story31-Aug-10 5:45
Shane Story31-Aug-10 5:45 
GeneralError if SQL database name is a number Pin
R Lewis9-Jul-10 14:19
R Lewis9-Jul-10 14:19 
GeneralDatabase Helper v2.0.0 [modified] Pin
ipadilla8-Jun-10 19:47
ipadilla8-Jun-10 19:47 
GeneralSmall bug : Always retreives databases from local database server Pin
arzulfi23-Nov-09 5:26
arzulfi23-Nov-09 5:26 
GeneralRe: Small bug : Always retreives databases from local database server Pin
tipa_loc11-May-12 4:26
tipa_loc11-May-12 4:26 
NewsThat Great... Pin
LTMKH23-Jun-09 11:31
LTMKH23-Jun-09 11:31 
GeneralWell done! Pin
CrshTstDmmy6-Apr-09 6:20
CrshTstDmmy6-Apr-09 6:20 
GeneralRe: Well done! Pin
hdv2127-Apr-09 3:53
hdv2127-Apr-09 3:53 
GeneralGood article Pin
Donsw19-Jan-09 9:21
Donsw19-Jan-09 9:21 
GeneralRe: Good article Pin
hdv21229-Jan-09 10:14
hdv21229-Jan-09 10:14 
QuestionCan't Connect Pin
CodeComando21-Oct-08 15:14
CodeComando21-Oct-08 15:14 
AnswerRe: Can't Connect Pin
hdv21222-Oct-08 8:25
hdv21222-Oct-08 8:25 
AnswerRe: Can't Connect Pin
FruitBatInShades19-Nov-08 23:06
FruitBatInShades19-Nov-08 23:06 
GeneralRe: Can't Connect Pin
hdv21226-Nov-08 9:10
hdv21226-Nov-08 9:10 
AnswerRe: Can't Connect Pin
gluip16-Dec-08 19:53
gluip16-Dec-08 19:53 
GeneralRe: Can't Connect Pin
Kent K17-Jan-09 10:52
professionalKent K17-Jan-09 10:52 
Generalis it there... Pin
TheCardinal23-Aug-08 23:28
TheCardinal23-Aug-08 23:28 

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.