Click here to Skip to main content
Click here to Skip to main content

Create a Business Logic Layer, Data Access Layer classes, and Stored Procedure scripts from a database table

By , 4 Jun 2010
 

Introduction

One of the most tedious parts of developing database-driven applications is coding the classes for your database objects. Especially in a situation when you are dealing with a database containing a large number of tables, or tables with many columns. Today, the application development process has vastly expanded so it is better to use sub applications to generate code dynamically / automatically. I developed this application to reduce burden on software developers and provide them some relief. As a software developer, I can understand very well that programming is a tough job so they need some rest.

Key features

The application is designed to create:

  1. Business Logic Layer class
  2. Data Access Layer class
  3. SQL Database Stored Procedure script

Key benefits

  1. Reduce development time.
  2. Reduce development cost.
  3. Follow standard coding techniques and application architecture.
  4. Fully commented code so any one can easily modify it according to their needs.

How to use this application

In order to use this application, you must know the computer name/ IP address and the login information for connecting to a SQL Server database.

NotConnected.JPG

After successful login, you can go to another tab "Create Class and Stored Procedure". Select a particular database and check the tables to create the Stored Procedure and classes. Click on the Create button to create Stored Procedures and classes.

All classes and Stored Procedure scripts reside in the bin folder.

Connected.JPG

Example

As a very simple example, suppose we have the table name Employee.

DBTable.JPG

Create Business Logic Layer class

The Business Logic Layer class contains the namespaces, default constructor, private fields, and public properties with getters and setters and all the methods (Select, Insert, Delete, and Update) required for connecting the Database Access Layer. For every database table, it creates a class with the same name as the table name and concatenates it with controller. This example application will create a class with the name clsEmployeeController. It also creates properties with the name same as the table attributes.

BusinessLayer.JPG

Create Data Access Layer class

The Database Access Layer class contains the namespaces, default constructor, private fields, and public properties with getters and setters and all the methods (Select, Insert, Delete, and Update) required to directly deal with a database with Stored Procedures. For every database table, it creates a class with the same name as the table name and creates the properties with the same name as the table attributes. In this example, the application will create a class with the name clsEmployee.

DataBaseLayer.JPG

Create Stored Procedure script

It also creates a script of four Stored Procedures (Select, Insert, Delete, Update) in a Notepad file with the same name as the table name. You can simply copy and run it on the SQL Query Analyzer.

Points of Interest

I did not use any library in this application. It is a small application with big benefits.

License

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

About the Author

Syeda Anila Nusrat
Software Developer Syndustria Pvt Ltd
Pakistan Pakistan
Member
Name: Syeda Anila Nusrat
 
Education:-
National University of Computer and Emerging Sciences
MS (Computer Science) 2010–2012 (expected)
Federal Urdu University of Arts, Science and Technology
BS (Computer Science) 2004–2007
 
Professional Experience:-
Software Engineer at Syndustria Pvt Ltd (Jun2008-Feb2010)
Software Engineer(internee) at yEvolve Pvt Ltd (Feb2008-Apr 2008)
 
Specialties:-
IDE: Ms Visual Studio 2005 and 2008
Programming Languages: C++.NET, C#.NET, VB.NET, ADO.NET
Web Technologies: ASP.NET, AJAX, Html, CSS, Java Scripting
Databases: SQL Server 2000, SQL Server 2005
Programming Methodologies: Multithreading and DLLs
Scheduling and Modeling tools: Ms-Project 2000, MS Visio, Rational Rose
 
Honors and Awards:-
Received Merit Certificate for scoring 3.81 CGPA on scale of 4.0 in BS(CS)
 
My professional Network
http://pk.linkedin.com/in/syedaanila

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.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionMicrosoft.ApplicationBlocks.DatamemberMember 223132122 Aug '12 - 23:49 
What does this Microsoft.ApplicationBlocks.Data namespace do here ???
What does it contain ?
 
Great piece of work.
Thanks a lot !!!
You made my day !!!!
AnswerRe: Microsoft.ApplicationBlocks.Datamemberaurinai17 Dec '12 - 6:15 
where you able to use this application withou modification , i don't see where the connection string is used in the insert , update , delete , methode .... connection handling should be added, open and close
QuestionAny chance for a VB.NET version?memberGroBeMaus10 Apr '12 - 10:42 
Thank you for the great utility, at least you put in some effort where others might not have bothered. Is there any chance that you might have a VB version sitting around?
QuestionWhy are there two classes for each table?memberOpethManiac12 Jan '12 - 4:53 
What is the purpose of having two different classes for the same table?
 
And what is this "cls" before the class name good for?
AnswerRe: Why are there two classes for each table?memberGroBeMaus11 Apr '12 - 4:09 
The "cls" is his notation for "class" I just use "c" that way anything you see with "cls" in the name you will know is a class. The reason for the two classes is that he is separating the business logic from the data access logic. You use the controller class (business logic), the controller class uses the entity class to do the work (data logic)...more or less.
GeneralMy vote of 4membervanita tripathi17 Nov '11 - 19:35 
Quoted the example very nicely to understand
GeneralMy vote of 5memberMahesh Gholap3 Jun '11 - 21:44 
Hey Excellent work... Dear!!!
General[My vote of 1] Please tell me you used CodeDommemberArgyle4Ever4 Jun '10 - 5:31 
Please tell me you used CodeDom to generate the code. Seriously Code generation is built in to .net if you use the CodeDom, you will find then you can also build in any .net language that is installed on the users system.
 
Writing it "by hand" using a StringBuilder/TextWriter is not the way to go...
GeneralRe: [My vote of 1] Please tell me you used CodeDommemberSyeda Anila Nusrat4 Jun '10 - 8:07 
I had never heard about CodeDom before. Thank u so much for enhancing my knowledge. If u want to perform same task using CodeDom then i must say BEST OF LUCK.
 
Regards
Syeda Anila Nusrat
GeneralRe: [My vote of 1] Please tell me you used CodeDommemberArgyle4Ever4 Jun '10 - 9:35 
I have done it a few times using COdedom, its a very logical way of working and its well worth implementing it in CodeDom for the language flexibility you would get. esp if you are going to come up with a end product.
 
JC
GeneralRe: [My vote of 1] Please tell me you used CodeDommemberjosto221 Jul '10 - 6:35 
Use CodeDom is not always the best option. customizing CodeDOM code is very difficult, while customizing a template for codesmith or mygeneration is quite easy. In fact, these tools are very common nowadays.
Generalcombine with asp.net MVCmemberzhuqil3 Jun '10 - 19:40 
Hi Syeda Anila Nusrat
It is simple but useful .I have a similar tool . So I think if it can combine with asp.net MVC , it will more perfect. Smile | :)
zhuqil

GeneralCode generation error [modified]memberipadilla3 Jun '10 - 10:32 
Nice program. I have checked it on Northwind database and I have found the following error in tables: CustomerCustomerDemo, CustomerDemographics, Region and Employees Territories.
 
03/06/2010 10:18:53 ==> La longitud no puede ser inferior a cero.
Nombre del parámetro: length
 
Translation:
Length can not be less than cero.
Parameter name: length
 
The generated code was OK on all other tables.
 
Why this error?
Thank you
ipadilla

modified on Thursday, June 3, 2010 4:41 PM

GeneralRe: Code generation errormemberSyeda Anila Nusrat3 Jun '10 - 14:44 
Did u select any table which has no attribute(column)? Please uncheck dtProperties in the list of database tables when u r generating classes and stored procedure scripts.
 
Regards
Syeda Anila Nusrat
GeneralRe: Code generation errormemberipadilla3 Jun '10 - 20:40 
Thank you Syeda Anila Nusrat for reply,
 
What I did were the following:
1) I check all the tables, Dacta Access Layer Clas, Business Logic Layer Class stored procedure (select,insert,update,delete).
 
2) Then I click on create, the program start to generate code and produce clsCategories.cs, start to generate CustamerCustomerDemo.cs and fail producing a file CustomerCustomerDemo.cs with 0 bytes.
I have the same problem with Tables: CustomerCustomerDemo, CustomerDemographics, Region and Employees Territories.
 
3) If I unckeck these 4 tables: CustomerCustomerDemo, CustomerDemographics, Region and Employees Territories, the rest of tables generate all code without problems at all.
 
I do not understand your question "Did u select any table which has no attribute(column)?", all tables have column. Database is from Microsoft side, althought a litle old.
 
ipadilla
GeneralRe: Code generation errormemberSyeda Anila Nusrat3 Jun '10 - 21:47 
Check those 4 tables and create only classes not SP scripts. if successful then create SP scripts.
I want to know where r u getting problem, when u r creating classes or SP scripts ???
 
Regards
Syeda Anila Nusrat
GeneralRe: Code generation error [modified]memberipadilla3 Jun '10 - 22:57 
Hi Syeda Anila Nusrat ,
 
the problem is only when you create "Data Access Layer Class", all other options work OK.
On "Buiness Logic Layer Class and Stored procedure" code is generated OK.
 
Tables structure:
Table CustomerCustomerDemo fields:
1) CustomerID nchar(5)
2) CustomerTypID nchar(10)
 
Table CustomerDemographics fields:
1) CustomerTypeID nchar(10)
2) CustomerDesc nText
 
Table EmployeeTerritories fields:
1) EmployeeID int
2) TerritoryID nvarchar(20)
 
Table Region fields:
1) RegionID int
2) RegionDescription nchar(50)
 
All other tables have 3 or more fields. Seem to be that this error is caused when structure of the table have less than 3 fields.
ipadilla

modified on Friday, June 4, 2010 5:17 AM

GeneralRe: Code generation errormemberSyeda Anila Nusrat4 Jun '10 - 4:31 
Yes u were right. Dont worry I have made changes in code and have uploaded as well. Download the updated code and enjoy Smile | :)
 
Regards
Syeda Anila Nusrat
GeneralRe: Code generation error [modified]memberipadilla4 Jun '10 - 6:20 
Hi Syeda Anila Nusrat,
after you corrections I have found the same error on StoredProcedure-Insert:
 
Length can not be less than cero.
Parameter name: length
 
Here you are the solution:
 
To avoid this error you have to do the following
1) Go to "private void Insert_StoredProcedure(StreamWriter sw, string strSPname, ArrayList AttributeNameArrayList, ArrayList AttributeTypeArrayList_Sql)"

2) Change the line:
 
------error: for (int i = 1; i < AttributeNameArrayList.Count-1; i++)
for
------OK; for (int i = 0; i < AttributeNameArrayList.Count-1; i++)
 
private void Insert_StoredProcedure(StreamWriter sw, string strSPname, ArrayList AttributeNameArrayList, ArrayList AttributeTypeArrayList_Sql)
{
try
{
const string consTemp = @"@";
string temp = string.Empty;
string strColumns = string.Empty;
string strParametersWithDataType = string.Empty;
string strParametersWithoutDataType = string.Empty;

//Parameters with datatype
------error for (int i = 1; i < AttributeNameArrayList.Count-1; i++)
{
 
Do you agree?
 
But, what happen if the table have only one field?
Simply the program fail...
ipadilla

modified on Monday, June 7, 2010 1:42 PM

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

Permalink | Advertise | Privacy | Mobile
Web01 | 2.6.130523.1 | Last Updated 4 Jun 2010
Article Copyright 2010 by Syeda Anila Nusrat
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid