Click here to Skip to main content
6,636,867 members and growing! (23,746 online)
Email Password   helpLost your password?
Database » Database » General     Intermediate

Automated Class Builder for Database Tables

By Nirosh

This will create a set of classes that generate a set of C# class files which map into the database tables. They will have the class name same as the table name and they will have a set of properties that are same as the table attributes.
C#, Windows, .NET 1.0, .NET 1.1, ADO.NET, VS.NET2003, Architect, DBA, Dev
Posted:11 Nov 2004
Views:61,978
Bookmarked:56 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
26 votes for this article.
Popularity: 4.55 Rating: 3.22 out of 5
4 votes, 15.4%
1
1 vote, 3.8%
2
3 votes, 11.5%
3
6 votes, 23.1%
4
12 votes, 46.2%
5

Sample Image - ModelCreator.jpg

Introduction

Today, the application development process has vastly expanded. There are enough number of processes around that helps Rapid Application Development (RAD). But we all know that, for a project, which time is of utmost importance. None of these approaches suit well. At that instance, all of us, doesn�t really matter whether we like it or not, go for an ad-hoc approach. You change most of the definitions and define them to suite your current need. I am not trying to prove that it is the best way to go about, but when something has to be done fast fighting with the time, I feel that the ad-hoc approach suits well. But it is even better if we have some automated process within our application development life cycle, or in other words, it is good to have a sub application written to develop some part of our application automatically so that it matches both speed and efficiency.

This article is trying to find a fast way to develop database model classes set automatically using a sub application that generates code automatically/ dynamically. As the initial approach, I will create this application that generates a set of CS files which map into the database tables. They will have the same class name as the table name, and they will have the properties that are same as the table attributes.

As this figure illustrates, the application creates model classes that map with all the tables of the database. It creates get/ set methods for the property as well as user defined constructor for each class so that we can use the user defined constructor to pass attributes (set properties) to the class as it is being created.

Background

As I explained a while ago, some times, we develop applications in defined, standard ways, which is the better way to go. But still there are many instances, at least I have enough previous experiences that happened to change the database after fully or partially developing the application. Then, as you change the database, as a parallel process, you got to change the model classes (if you are following the MVC architecture), in other words, the class sets that handle the database manipulation and operations. The next version of this application will introduce an automated approach to solve the problem completely. There, the application will develop a set of classes that are responsible for doing most of the database manipulations, such as add, delete, update, and select field and records. As the initial approach, I will focus on developing an application that can generate a set of C# class files which map with the database tables.

Requirements

The users who read/ test this article need to have some understanding of C# Windows Forms application development, MS SQL server and stored procedures. Additionally, they are expected to have the Visual Studio .NET IDE and MS SQL server 7 or higher installed in their machine.

Note: If you are planning to use a SQL server that resides in a network machine, you need to have the "SQL Server Enterprise Manager" installed in your machine, or you need to talk to your friendly network admin to create a test database for you.

Using the code

In order to test this application, you have to have a valid database server IP address, a simple database, and the administrator user credential to connect to the database. Obviously, you got to create a database with a set of tables in it. In my case, I have the database in my database server with the name �PRD-01�. In that server, I have created a database called �ManGoDB�, and I used the super user �sa� to log in to my database.

Entry to the Application

The application is at its very basic stage and it has one main class with the name �ModelCreator.cs� that does all the key operations. Entry point to this application is the �Connect and Create� button click event. It will fire a �CreateConnectionString()� method, which basically gets the input from the user and dynamically creates the connection string.

private void lbtnConnect_Click(object sender, System.EventArgs e)
{
  if (CreateConnectionString())
    CreateModelClassFiles(tcGetDataReader());
}

Once that process is done, the application calls the method named tcGetDataReader():

/// <summary>

/// Get the SqlDataReader object

/// SqlDataReader

/// </summary>

public SqlDataReader tcGetDataReader()
{
  SqlConnection connection = null;
  try
  {
    connection = GetConnection(SQL_CONN_STRING);
    if (connection == null)
      return null;
    SqlDataReader dr = SqlHelper.ExecuteReader(
             connection,
             CommandType.StoredProcedure,
             "getData");
    if (dr.HasRows)
      return dr;
    else
      return null;
  }
  catch(Exception ex)
  {
    MessageBox.Show(ex.Message);
    return null;
  }
}

Get Table Names, Attributes and their types from the database

One of the more important sections of this article is the "getData" stored procedure. It retrieves the table names, their attributes as well as the data types of these attributes in a table. The stored procedure reads as follows:

CREATE PROCEDURE getData AS 
 select table_name, column_name, data_type
  from information_schema.columns
  where table_name in
  (
   select table_name
   from Information_Schema.Tables
   where Table_Type='Base Table'
  ) order by table_name
GO

Main Method, CreateModelClassFiles

/// <summary>

/// Create the Model class list iterating through the tables

/// </summary>

/// <param name="dr">Sql Data reader for the database schema</param>

private void CreateModelClassFiles(SqlDataReader dr)
{
  if (dr != null)
  {
    string lstrOldTableName = string.Empty;
    StreamWriter sw = null;
    System.Text.StringBuilder sb = null;
    System.Text.StringBuilder sbAttr = null;
    while(dr.Read())
    {
      string lstrTableName = dr.GetString(0);
      string lstrAttributeName = dr.GetString(1);
      string lstrAttributeType = GetSystemType(dr.GetString(2));
      if (lstrOldTableName != lstrTableName)
      {
        if (sw != null)
        {
          this.CreateClassBottom(sw, sb.ToString().TrimEnd(
                     new char[]{',', ' ', '\r', '\t', '\n'}),
                     sbAttr.ToString());
            sw.Close();
        }
        sb = new System.Text.StringBuilder(lstrTableName);
        sb.Append(".cs");
        FileInfo lobjFileInfo = new FileInfo(sb.ToString());
        sw = lobjFileInfo.CreateText();
        this.CreateClassTop(sw, lstrTableName);
        sb = new System.Text.StringBuilder("\r\n\t/// \r\n\t" + 
             "/// User defined Contructor\r\n\t/// \r\n\tpublic ");
        sbAttr = new System.Text.StringBuilder();
        sb.Append(lstrTableName);
        sb.Append("(");
      }
      else
      {
        this.CreateClassBody(sw, lstrAttributeType, lstrAttributeName);
        sb.AppendFormat("{0} {1}, \r\n\t\t", 
           new object[]{lstrAttributeType, lstrAttributeName});
        sbAttr.AppendFormat("\r\n\t\tthis._{0} = {0};", 
           new object[]{lstrAttributeName});
      }
      lstrOldTableName = lstrTableName;
      this.progressBarMain.Increment(1); 
    }
    MessageBox.Show("Done !!");
  }
}

Once this method is called, it does every thing for you.

Points of Interest

Some parts of the code still needs some improvement. At the same time, it seems to me that this application can be greatly improved to be very effective when the database schema is changing at a rapid rate.

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

About the Author

Nirosh


Member
In-depth coverage of Microsoft .Net Technologies.

- If it is good for both good and bad people then it is bad for a good society..

Contact Nirosh via c_nir*o*sh@hotmail.com (Remove * to use)
View Nirosh's profile on Linked-In
Occupation: Architect
Location: Sri Lanka Sri Lanka

Other popular Database articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 21 of 21 (Total in Forum: 21) (Refresh)FirstPrevNext
GeneralThanks PinmemberNitin Sawant21:13 8 Oct '09  
GeneralSQL Management Studio PinmemberDerek Bartram3:11 1 May '08  
GeneralRe: SQL Management Studio PinmemberNirosh17:05 1 May '08  
GeneralRe: SQL Management Studio PinmemberDerek Bartram3:14 3 May '08  
GeneralRe: SQL Management Studio [modified] PinmemberNirosh6:26 3 May '08  
GeneralRe: SQL Management Studio PinmemberDerek Bartram8:00 3 May '08  
GeneralGetData StoredProcedure Pinmembergaurav8422:49 24 Jun '07  
GeneralRe: GetData StoredProcedure PinmemberNirosh16:17 25 Jun '07  
QuestionRe: GetData StoredProcedure Pinmemberfantasy121516:31 21 Aug '08  
AnswerRe: GetData StoredProcedure PinmemberNirosh4:15 22 Aug '08  
GeneralThis is not a good example of well written code PinmemberLuciano Bargmann17:03 9 Jun '07  
GeneralRe: This is not a good example of well written code PinmemberNirosh17:42 10 Jun '07  
GeneralRe: This is not a good example of well written code PinmemberLuciano Bargmann3:51 14 Jun '07  
GeneralSmall bug - Last class file doesn't get saved properly Pinmemberfuzzy2logic13:25 28 May '07  
GeneralRe: Small bug - Last class file doesn't get saved properly PinmemberNirosh13:28 28 May '07  
GeneralRoutine skips 1st field of every table PinmemberDanShoop6:10 20 Feb '07  
GeneralRe: Routine skips 1st field of every table PinmemberNirosh18:05 20 Feb '07  
Generalis getData DB-generic ? PinmemberFrank Olorin Rizzi3:45 12 Nov '04  
GeneralRe: is getData DB-generic ? PinmemberGfw10:28 13 Nov '04  
GeneralRe: is getData DB-generic ? PinmemberNirosh18:58 14 Nov '04  
GeneralRe: is getData DB-generic ? PinmemberNirosh17:29 14 Nov '04  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 11 Nov 2004
Editor: Smitha Vijayan
Copyright 2004 by Nirosh
Everything else Copyright © CodeProject, 1999-2009
Web17 | Advertise on the Code Project