Click here to Skip to main content
Licence CPOL
First Posted 8 Sep 2009
Views 20,088
Bookmarked 33 times

Generate SQL Script Programmatically

By | 8 Sep 2009 | Article
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)

About the Author

Md. Marufuzzaman

Architect

Bangladesh Bangladesh

Member

Follow on Twitter Follow on Twitter


Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionGood work PinmemberSunasara Imdadhusen22:46 31 Aug '11  
AnswerRe: Good work PinmvpMd. Marufuzzaman14:08 1 Sep '11  
GeneralKeep going PinmemberAndre K20:54 11 Apr '11  
GeneralRe: Keep going PinmvpMd. Marufuzzaman14:07 1 Sep '11  
GeneralMy vote of 2 PinmemberJohn Brett5:49 24 Sep '09  
GeneralRe: My vote of 2 PingroupMd. Marufuzzaman7:03 28 Sep '09  
QuestionA bit miffed ??? PinmemberEd Bouras2:28 15 Sep '09  
GeneralMy vote of 1 PinmemberAndrei Rinea22:25 14 Sep '09  
GeneralMy vote of 1 PinmemberPaw Jershauge8:36 8 Sep '09  
GeneralRe: My vote of 1 PingroupMd. Marufuzzaman7:06 28 Sep '09  
GeneralGood. PinassociateAbhishek Sur8:29 8 Sep '09  
GeneralRe: Good. PingroupMd. Marufuzzaman8:36 8 Sep '09  
GeneralRe: Good. PinassociateAbhishek Sur22:14 8 Sep '09  
GeneralMy vote of 1 Pinmembervoloda28:25 8 Sep '09  
GeneralMy vote of 2 PinmemberFatih Birkan8:14 8 Sep '09  
GeneralRe: My vote of 2 PingroupMd. Marufuzzaman8:38 8 Sep '09  
GeneralMy vote of 1 Pinmemberbrunomsilva7:37 8 Sep '09  
GeneralRe: My vote of 1 PingroupMd. Marufuzzaman8:24 8 Sep '09  
GeneralMy vote of 1 Pinmemberhth20007:25 8 Sep '09  
GeneralRe: My vote of 1 PingroupMd. Marufuzzaman8:20 8 Sep '09  
GeneralRe: My vote of 1 Pinmemberaspdotnetdev14:06 16 Sep '09  

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.

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