Click here to Skip to main content
15,885,757 members
Articles / Database Development / SQL Server

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 133K   13.7K   96  
Automatically generates the required class and stored procedure to work with SQL databases
using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

namespace SUNCodeGenerator.Classes
{
    class DataBaseInformation
    {

        private string conStr = "";
        public DataBaseInformation(string ConnectionString)
        {
            conStr = ConnectionString;
        }

        public ArrayList LoadTable()
        {
            SqlConnection conn = new SqlConnection(conStr);
            SqlCommand cmd = new SqlCommand("SELECT Distinct Table_Name From information_Schema.columns", conn);
            SqlDataReader rdr;
            ArrayList arr = new ArrayList();
            try
            {
                conn.Open();
                rdr = cmd.ExecuteReader();
                while (rdr.Read())
                {
                    arr.Add(rdr[0].ToString());
                }
            }
            catch (SqlException ex)
            {
                MessageBox.Show(ex.Message, "DataBase Information", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            finally
            {
                if (conn.State == ConnectionState.Open)
                    conn.Close();
                conn.Dispose();
                cmd.Dispose();
            }
            return arr;
        }


        public ArrayList LoadFileds(string TableName)
        {
            SqlConnection conn = new SqlConnection(conStr);
            SqlCommand cmd = new SqlCommand("SELECT Column_Name From information_Schema.columns Where Table_Name='" + TableName + "'", conn);
            SqlDataReader rdr;
            ArrayList arr = new ArrayList();
            try
            {
                conn.Open();
                rdr = cmd.ExecuteReader();
                while (rdr.Read())
                {
                    arr.Add(rdr[0].ToString());
                }
            }
            catch (SqlException ex)
            {
                MessageBox.Show(ex.Message, "DataBase Information", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            finally
            {
                if (conn.State == ConnectionState.Open)
                    conn.Close();
                conn.Dispose();
                cmd.Dispose();
            }
            return arr;
        }

    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

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