Click here to Skip to main content
11,483,003 members (72,508 online)
Click here to Skip to main content

Generate SQL Script Programmatically

, 8 Sep 2009 CPOL 44.2K 36
Rate this:
Please Sign up or sign in to vote.
How to generate SQL object script using C#.NET

Introduction

This article will give you an idea of how easily you can generate the SQL server database object script using C#. NET.

Background

This article will be helpful for developers who want to generate the SQL Server database objects script like stored procedure, views, etc.

Using the Code

It’s a very easy way... I hope that you will agree with me that the flexibility of Microsoft products to integrate with your custom application is outstanding. Microsoft SQL Server provides a special stored procedure "sp_helptext", This stored procedure allows you to get the description of a given object.

Here I write a function to get the SQL script from a given object. You just need to provide three parameters:

  • Connection string
  • Object name
  • Object type (I used this to determine whether it's a Table or not)
Syntax
exec sp_helptext 'object'

A sample code example is given below.

Sample Code

public string GetScript(string strConnectionString
                      , string strObject
                      , int ObjType)
{
    string strScript = null;
    int intCounter = 0;
    if (ObjType != 0)
    {
        ObjSqlConnection = new SqlConnection(strConnectionString.Trim());

        try
        {
            ObjDataSet = new DataSet();
            ObjSqlCommand = new SqlCommand("exec sp_helptext 
				[" + strObject + "]", ObjSqlConnection);
            ObjSqlDataAdapter = new SqlDataAdapter();
            ObjSqlDataAdapter.SelectCommand = ObjSqlCommand;
            ObjSqlDataAdapter.Fill(ObjDataSet);

            foreach (DataRow ObjDataRow in ObjDataSet.Tables[0].Rows)
            {
                strScript += Convert.ToString(ObjDataSet.Tables[0].Rows[intCounter][0]);
                intCounter++;
            }
        }
        catch (Exception ex)
        {
           strScript = ex.Message.ToString();
        }
        finally
        {
            ObjSqlDataAdapter = null;
            ObjSqlCommand = null;
            ObjSqlConnection = null;
        }
    }

    return strScript;
}

Points of Interest

The stored procedure sp_helptext will not allow to give you any table description.

Conclusion

I hope that this article will be helpful to you. Enjoy!

History

  • 8th September, 2009: Initial post

License

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

Share

About the Author

Md. Marufuzzaman
Architect
Bangladesh Bangladesh
A highly experienced leader with successful track record of software development, product innovations, brand management and corporate communication etc. Some successful product innovations have also achieved and awards “Most Valuable Professional” (MVP) at 2010 and 2011 by codeproject.com and also selected as a mentor of codeproject.com. Published over 100 technical articles in various software development resource sites (i.e., codeprojetc.com, Microsoft MSDN, and IEEE & IBM (In progress)) and various IT Forums, Blogs etc.

Over ten years of professional experiences in ICT field having extensive experience in formulating corporate vision and long term strategy. Leading development related functions including design, development, services, data management and analytics, customer experience management, content services, digital analytics and optimization.

An individual with results-driven approach and relentless in pursuit of excellence from a business and organizational standpoint. Believes in transparency, commitment and teamwork.

Expertise: Software/Solution Architect, SaaS platform base application, Large scale win32/web based business software solutions, enterprise applications, integration, etc.

Technologies/Tools: Microsoft.Net version 05/08/10/12, Microsoft SQL Server version 7/2K/05/08/12 , Oracle version 10/11, MySql version 5.1, 5.5, PS2, Visual C#, R, VB.NET, ASP.NET, PHP, API, MVC, WebAPI , Add-In Visual Basic etc.,. I have also more than two years’ of strong experience in mobile-VAS (platform development).

Points of Interest: Technology and research & development especially focused on business functionalities and social business areas as well, few stuff including:

1.R&D on new techniques as required to increase business revenue.
2.Urban and rural sectors to improve people’s lifestyle, better medical facilities, education, social business etc., using innovative technical solutions.
3.Research and innovative product development.
4.Etc.
Follow on   Twitter   LinkedIn

Comments and Discussions

 
QuestionValues to be passed to the function Pin
srsivaprasad2-Aug-13 0:59
membersrsivaprasad2-Aug-13 0:59 
GeneralMy vote of 4 Pin
kishore doni28-Aug-12 23:09
memberkishore doni28-Aug-12 23:09 
GeneralMy vote of 2 Pin
Sami Ciit27-Aug-12 2:41
memberSami Ciit27-Aug-12 2:41 
QuestionThanks Pin
dharm777-Aug-12 21:50
memberdharm777-Aug-12 21:50 
AnswerRe: Thanks Pin
Md. Marufuzzaman7-Aug-12 22:04
mentorMd. Marufuzzaman7-Aug-12 22:04 
QuestionGood work Pin
Sunasara Imdadhusen31-Aug-11 23:46
memberSunasara Imdadhusen31-Aug-11 23:46 
AnswerRe: Good work Pin
Md. Marufuzzaman1-Sep-11 15:08
mvpMd. Marufuzzaman1-Sep-11 15:08 
GeneralKeep going Pin
Andre K11-Apr-11 21:54
memberAndre K11-Apr-11 21:54 
GeneralRe: Keep going Pin
Md. Marufuzzaman1-Sep-11 15:07
mvpMd. Marufuzzaman1-Sep-11 15:07 
GeneralMy vote of 2 Pin
John Brett24-Sep-09 6:49
memberJohn Brett24-Sep-09 6:49 
GeneralRe: My vote of 2 Pin
Md. Marufuzzaman28-Sep-09 8:03
groupMd. Marufuzzaman28-Sep-09 8:03 
QuestionA bit miffed ??? Pin
Ed Bouras15-Sep-09 3:28
memberEd Bouras15-Sep-09 3:28 
GeneralMy vote of 1 Pin
Andrei Rinea14-Sep-09 23:25
memberAndrei Rinea14-Sep-09 23:25 
GeneralMy vote of 1 Pin
Paw Jershauge8-Sep-09 9:36
memberPaw Jershauge8-Sep-09 9:36 
GeneralRe: My vote of 1 Pin
Md. Marufuzzaman28-Sep-09 8:06
groupMd. Marufuzzaman28-Sep-09 8:06 
GeneralGood. Pin
Abhishek Sur8-Sep-09 9:29
memberAbhishek Sur8-Sep-09 9:29 
GeneralRe: Good. Pin
Md. Marufuzzaman8-Sep-09 9:36
groupMd. Marufuzzaman8-Sep-09 9:36 
GeneralRe: Good. Pin
Abhishek Sur8-Sep-09 23:14
memberAbhishek Sur8-Sep-09 23:14 
GeneralMy vote of 1 Pin
voloda28-Sep-09 9:25
membervoloda28-Sep-09 9:25 
GeneralMy vote of 2 Pin
Fatih Birkan8-Sep-09 9:14
memberFatih Birkan8-Sep-09 9:14 
GeneralRe: My vote of 2 Pin
Md. Marufuzzaman8-Sep-09 9:38
groupMd. Marufuzzaman8-Sep-09 9:38 
GeneralMy vote of 1 Pin
brunomsilva8-Sep-09 8:37
memberbrunomsilva8-Sep-09 8:37 
GeneralRe: My vote of 1 Pin
Md. Marufuzzaman8-Sep-09 9:24
groupMd. Marufuzzaman8-Sep-09 9:24 
GeneralMy vote of 1 Pin
hth20008-Sep-09 8:25
memberhth20008-Sep-09 8:25 
GeneralRe: My vote of 1 Pin
Md. Marufuzzaman8-Sep-09 9:20
groupMd. Marufuzzaman8-Sep-09 9:20 

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

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150520.1 | Last Updated 8 Sep 2009
Article Copyright 2009 by Md. Marufuzzaman
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid