Click here to Skip to main content
15,867,141 members
Articles / Database Development / SQL Server
Article

DAL Class and Transact-SQL Generator for C# and VB.NET

Rate me:
Please Sign up or sign in to vote.
4.48/5 (27 votes)
8 Dec 2007CPOL2 min read 132.6K   13.7K   96   25
Automatically generates the required class and stored procedure to work with SQL databases

Introduction

This program will help you write the necessary code for doing base operations of your application's Data Layer (both Web and Windows applications). The function of this program is making a DAL class for easily selecting, manipulating and deleting records of the Database. The result is a simple class for each table in your database. The structure is described below in a schematic view.

Background

Screenshot - Diagram.jpg

Using the Code

Class Structure

  1. Property
  2. Insert method
  3. Update method
  4. Delete method
  5. Select method

Description

  1. These generated properties indicate each field of a table. You may use them to get or set values in your application:

    C#
    private string ConnectionString;
    public City(string ConnStr)
    {
        ConnectionString = ConnStr;
    }
    
    private string m_Code;
    public string Code
    {
        get { return m_Code; }
        set { m_Code = value; }
    }
    
    private string m_Title;
    public string Title
    {
        get { return m_Title; }
        set { m_Title = value; }
    }
    
    private string m_Province_Code;
    public string Province_Code
    {
        get { return m_Province_Code; }
        set { m_Province_Code = value; }
    }
  2. This method uses generated stored procedures to Insert records into the database. It passes given values of each field to that stored procedure:

    C#
    public void Insert(string Code , string Title , string Province_Code)
    {
        SqlConnection conn = new SqlConnection(ConnectionString);
        SqlCommand cmd = new SqlCommand("Insert_City", conn);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue(@Code ,Code );
        cmd.Parameters.AddWithValue(@Title ,Title );
        cmd.Parameters.AddWithValue(@Province_Code ,Province_Code );
    
        try
        {
            conn.Open();
            cmd.ExecuteNonQuery();
        }
        catch
        {}
        finally
        {
            if (conn.State==ConnectionState.Open)
            {
                conn.Close();
            }
            conn.Dispose();
            cmd.Dispose();
        }
    }

    You can also use the second overload of the Insert method to insert a record using currently specified properties.

  3. The Update method manipulates a record in database using the generated stored procedures. Such as above, this method calls the proper stored procedure with the given values:

    C#
     public void Update(string Code , string Title , string Province_Code)
    {
        SqlConnection conn = new SqlConnection(ConnectionString);
        SqlCommand cmd = new SqlCommand("Update_City", conn);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue(@Code ,Code );
        cmd.Parameters.AddWithValue(@Title ,Title );
        cmd.Parameters.AddWithValue(@Province_Code ,Province_Code );
    
        try
        {
            conn.Open();
            cmd.ExecuteNonQuery();
        }
        catch
        {}
        finally
        {
            if (conn.State==ConnectionState.Open)
            {
                conn.Close();
            }
            conn.Dispose();
            cmd.Dispose();
        }
    }
  4. This method uses generated stored procedures to delete a record from the database. It takes the value of the key field and calls the stored procedure using that value:

    C#
    public void Delete(string ID)
    {
        SqlConnection conn = new SqlConnection(ConnectionString);
        SqlCommand cmd = new SqlCommand("Delete_City", conn);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue(@ID,ID);
    
        try
        {
            conn.Open();
            cmd.ExecuteNonQuery();
        }
        catch
        {}
        finally
        {
            if (conn.State==ConnectionState.Open)
            {
                conn.Close();
            }
            conn.Dispose();
            cmd.Dispose();
        }
    }
  5. Use this method to select a record set from a table to use later in your application. This method returns a dataset object using the generated stored procedure:

    C#
    private DataSet Select()
    {
        SqlConnection conn = new SqlConnection(ConnectionString);
        SqlDataAdapter cmd = new SqlDataAdapter("Select_City", conn);
        DataSet dts = new DataSet();
        try
        {
            conn.Open();
            cmd.Fill(dts);
            return dts;
        }
        catch
        {
            return null; 
        }
        finally
        {
            if (conn.State==ConnectionState.Open)
            {
                conn.Close();
            }
            conn.Dispose();
            cmd.Dispose();
        }
    }

SQL Transact

This program can generate transact-SQL to reach the nice methodology of "Fat Server and Thin Client". Below is the generated code we talk about:

  1. Insert

    SQL
    CREATE PROCEDURE [insert_City]
    (
        @Code        [int],
        @Title        [nvarchar](50),
        @Province_Code    [int]
    )
    AS INSERT INTO City
    (
        Code,
        Title,
        Province_Code
    )
    VALUES
    (
        @Code,
        @Title,
        @Province_Code
    )
  2. Update

    SQL
    CREATE PROCEDURE [update_City]
    (
        @Code            [int],
        @Title            [nvarchar](50),
        @Province_Code        [int]
    )
    AS UPDATE City
    SET
        Code        =    @Code,
        Title        =    @Title,
        Province_Code    =    @Province_Code
    WHERE
    (
        ID=@ID
    )
  3. Delete

    SQL
    CREATE PROCEDURE [delete_City]
        (@ID     [int])
    AS 
    DELETE [City]
    WHERE
        ([ID]     = @ID)
  4. Select

    SQL
    CREATE PROCEDURE [Select_City]
    AS 
    SELECT    [ID],
        [Code],
        [Title],
        (Select Title FROM Province WHERE Code=Province_Code) as Province
    FROM
        City  

Application Manual

Screenshot - LogIn.jpg

After you run the program, you must choose the destination database in the login form to generate required stored procedures.

After login, you see the main form such as the above picture.

Screenshot - Program_copy.jpg

Left Zone

  1. Database structure contains all tables and their fields. You can right click on each table and choose the desired item to view DAL class or related stored procedures from the context menu.

Content Zone

  1. You see the generated class in this window. To use it later, you should save the content in a *.cs file using toolbar.
  2. You see the generated transact-SQL codes for creating stored procedures in this window. You may parse or execute them immediately or save them for later use.

Right Zone

  1. You can see the properties of a field you select in the left zone.

License

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


Written By
Web 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

 
QuestionInvalid object name 'information_schema.columns' Problem solved :) Pin
Şükrü SAĞLAM24-Jun-17 19:36
Şükrü SAĞLAM24-Jun-17 19:36 
QuestionMenus not working Pin
hari191134-Oct-14 16:55
hari191134-Oct-14 16:55 
AnswerRe: Menus not working Pin
Dave Simon23-Apr-20 9:59
Dave Simon23-Apr-20 9:59 
QuestionThanks Pin
Member 1046299612-Dec-13 9:11
Member 1046299612-Dec-13 9:11 
SuggestionUseful modification Pin
Benny_E15-Jun-12 10:37
Benny_E15-Jun-12 10:37 
GeneralRe: Useful modification Pin
Benny_E15-Jun-12 10:38
Benny_E15-Jun-12 10:38 
QuestionGreat Work Man....But not working properly in windows 7_ 64bit??? Pin
Mahesh Gholap18-Apr-12 23:24
Mahesh Gholap18-Apr-12 23:24 
SuggestionThanks Pin
kzelda6-Dec-11 22:51
kzelda6-Dec-11 22:51 
QuestionError on click the menu Pin
ingenvzla29-Oct-11 7:00
ingenvzla29-Oct-11 7:00 
GeneralMy vote of 4 Pin
ingenvzla29-Oct-11 6:59
ingenvzla29-Oct-11 6:59 
GeneralMy vote of 5 Pin
Jonathan Fernández21-Dec-10 1:24
Jonathan Fernández21-Dec-10 1:24 
GeneralMy vote of 5 Pin
-=barmaley=-17-Dec-10 4:18
-=barmaley=-17-Dec-10 4:18 
GeneralMy vote of 5 Pin
miladrasouli17-Aug-10 23:35
miladrasouli17-Aug-10 23:35 
GeneralError when trying to run the code Pin
mary cappello29-Jun-09 10:55
mary cappello29-Jun-09 10:55 
GeneralYou missed a word Pin
123123man9-May-09 1:52
123123man9-May-09 1:52 
GeneralRe: You missed a word Pin
Hamidreza-Ghasemkhah9-May-09 2:19
Hamidreza-Ghasemkhah9-May-09 2:19 
Generalgenerator class errors fixs Pin
eyal skiba23-Aug-08 3:27
eyal skiba23-Aug-08 3:27 
GeneralGreat Article Pin
rubenben2816-Jun-08 11:11
rubenben2816-Jun-08 11:11 
GeneralNice Pin
ryanoc3339-Jan-08 8:56
ryanoc3339-Jan-08 8:56 
GeneralAnother Solution Pin
Ryan T. Hilton5-Dec-07 6:15
Ryan T. Hilton5-Dec-07 6:15 
Generalcool Pin
Miloš Savara1-Dec-07 5:50
Miloš Savara1-Dec-07 5:50 
GeneralRe: cool Pin
Hamidreza-Ghasemkhah4-Dec-07 19:46
Hamidreza-Ghasemkhah4-Dec-07 19:46 
GeneralVery useful, thank's Pin
Claudio Barca1-Dec-07 1:17
Claudio Barca1-Dec-07 1:17 
GeneralCode for VB .NET Pin
musicinc_261-Dec-07 0:42
musicinc_261-Dec-07 0:42 
GeneralRe: Code for VB .NET Pin
Hamidreza-Ghasemkhah4-Dec-07 19:50
Hamidreza-Ghasemkhah4-Dec-07 19:50 

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.