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

Generate SQL Script Programmatically

, 8 Sep 2009 CPOL
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 Pinmembersrsivaprasad2-Aug-13 0:59 
GeneralMy vote of 4 Pinmemberkishore doni28-Aug-12 23:09 
GeneralMy vote of 2 PinmemberSami Ciit27-Aug-12 2:41 
QuestionThanks Pinmemberdharm777-Aug-12 21:50 
AnswerRe: Thanks PinmentorMd. Marufuzzaman7-Aug-12 22:04 
QuestionGood work PinmemberSunasara Imdadhusen31-Aug-11 23:46 
AnswerRe: Good work PinmvpMd. Marufuzzaman1-Sep-11 15:08 
GeneralKeep going PinmemberAndre K11-Apr-11 21:54 
GeneralRe: Keep going PinmvpMd. Marufuzzaman1-Sep-11 15:07 
GeneralMy vote of 2 PinmemberJohn Brett24-Sep-09 6:49 
GeneralRe: My vote of 2 PingroupMd. Marufuzzaman28-Sep-09 8:03 
QuestionA bit miffed ??? PinmemberEd Bouras15-Sep-09 3:28 
GeneralMy vote of 1 PinmemberAndrei Rinea14-Sep-09 23:25 
GeneralMy vote of 1 PinmemberPaw Jershauge8-Sep-09 9:36 
GeneralRe: My vote of 1 PingroupMd. Marufuzzaman28-Sep-09 8:06 
GeneralGood. PinmemberAbhishek Sur8-Sep-09 9:29 
GeneralRe: Good. PingroupMd. Marufuzzaman8-Sep-09 9:36 
GeneralRe: Good. PinmemberAbhishek Sur8-Sep-09 23:14 
GeneralMy vote of 1 Pinmembervoloda28-Sep-09 9:25 
GeneralMy vote of 2 PinmemberFatih Birkan8-Sep-09 9:14 
GeneralRe: My vote of 2 PingroupMd. Marufuzzaman8-Sep-09 9:38 
GeneralMy vote of 1 Pinmemberbrunomsilva8-Sep-09 8:37 
GeneralRe: My vote of 1 PingroupMd. Marufuzzaman8-Sep-09 9:24 
GeneralMy vote of 1 Pinmemberhth20008-Sep-09 8:25 
GeneralRe: My vote of 1 PingroupMd. 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
Web02 | 2.8.141223.1 | Last Updated 8 Sep 2009
Article Copyright 2009 by Md. Marufuzzaman
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid