Click here to Skip to main content
15,893,487 members
Articles / Database Development / SQL Server

SQL-DMO: Search and Regenerate Stored Procedures in Seconds

Rate me:
Please Sign up or sign in to vote.
3.12/5 (9 votes)
22 Sep 2007CPOL 35K   579   37   4
Use SQL-DMO Object Library for quick searching and generating stored procedures script
Screenshot - ScriptInput.jpg

Screenshot - ScriptOutput.jpg

Screenshot - ScriptSearch.jpg

Introduction

Creating, generating and altering stored procedures are common tasks when we are working with database applications. These tasks won't take up our time if our database has a small number of stored procedures.

But for databases with thousands of stored procedures, it takes up our time to identify the stored procedure name in the list and then do some clicks to generate its script. To prevent these boring tasks, I spent a couple of hours to create a very simple tool for quick searching and regenerating stored procedure scripts using SQL-DMO Object Library.

Using the Code

The code is very simple and this sample is not a perfect one, but it helps my work. ;)

C#
using System;
using System.Collections;
using SQLDMO;
using System.Text;

namespace SQLScriptGen
{
    /// <summary>
    /// The Utility class present for a SQL Server instant
    /// </summary>
    public class SQLServerUtil
    {
        private SQLServer myServer;
        private bool isConnected = false;
        public SQLServerUtil()
        {
            myServer = new SQLServer();
        }

        public delegate void delegateSendText (string info);
        public event delegateSendText OnInfo;
        public event delegateSendText OnSearchFound;

        /// <summary>
        /// Get List of available SQL Server on network
        /// </summary>
        /// <returns />
        public  ArrayList GetServers()
        {
            ArrayList ret = new ArrayList();
            try
            {
                NameList nl = myServer.Application.ListAvailableSQLServers();
                int i, n = nl.Count;
                for (i = 0; i< n; i++)
                {
                    string sn = nl.Item(i);
                    if (sn != null)
                        ret.Add(sn);
                }
            }
            catch (System.Exception ex)
            {
                if (OnInfo != null)
                    OnInfo(ex.Message);
            }

            return ret;
        }

        /// <summary>
        /// Connect to a SQL Server using Windows Authentication
        /// </summary>
        public bool Connect(string serverName)
        {
            Disconnect();
            try
            {
                myServer.LoginTimeout = 15;
                myServer.LoginSecure = true;
                myServer.Connect(serverName,null , null);
            }
            catch (Exception ex)
            {
                isConnected = false;
                if (OnInfo != null)
                    OnInfo(ex.Message);
            }
            isConnected = true;
            return isConnected;
        }

        /// <summary>
        /// Disconnect from the server 
        /// </summary>
        private void Disconnect()
        {
            try
            {
                myServer.DisConnect();
            }
            catch (Exception ex)
            {
                if (OnInfo != null)
                    OnInfo(ex.Message);
            }
        }

        /// <summary>
        /// Connect to a SQL Server using SQL Authentication
        /// </summary>
        public bool Connect(string serverName, string userName, string password)
        {
            Disconnect();
            try
            {
                myServer.LoginSecure = false;
                myServer.Connect(serverName, userName, password);
            }
            catch (Exception ex)
            {
                isConnected = false;
                if (OnInfo != null)
                    OnInfo(ex.Message);
            }
            isConnected = true;
            return isConnected;
        }

        /// <summary>
        /// Retrieve list of available Database names
        /// </summary>
        public ArrayList GetDatabaseNames()
        {
            ArrayList ret = new ArrayList();
            try
            {
                foreach (_Database db in myServer.Databases)
                {
                    if (db != null)
                        ret.Add(db.Name);
                }

            }
            catch (System.Exception ex)
            {
                if (OnInfo != null)
                    OnInfo(ex.Message);
            }
            return ret;
        }

        /// <summary>
        /// Get script of a store procedure
        /// </summary>
        public string GetSPScript(string dbName, string spName, bool createFile)
        {
            string ret = "";
            try
            {
                string fileName = "output\\" + spName + ".sql";
                if (!createFile)
                    fileName = null;
                ret = myServer.Databases.Item
		  (dbName, "dbo").StoredProcedures.Item(spName, "dbo")
                    .Script(SQLDMO.SQLDMO_SCRIPT_TYPE.SQLDMOScript_Drops 
                    | SQLDMO.SQLDMO_SCRIPT_TYPE.SQLDMOScript_IncludeHeaders
                    | SQLDMO.SQLDMO_SCRIPT_TYPE.SQLDMOScript_Default
                    , fileName,
                    SQLDMO.SQLDMO_SCRIPT2_TYPE.SQLDMOScript2_Default );
            }
            catch (Exception ex)
            {
                if (OnInfo != null)
                    OnInfo(ex.Message);

            }
            return ret;
        }

        /// <summary>
        /// Simple searching method
        /// </summary>
        public void SearchSP(string dbName, string searchText, bool inNameOnly)
        {
            try
            {
                StoredProcedures colSP = myServer.Databases.Item
					(dbName, "dbo").StoredProcedures;
            
                if (inNameOnly)
                {
                    foreach (_StoredProcedure sp in colSP)
                    {

                        if (sp.Name.IndexOf(searchText) >=0)
                        {
                            if (OnSearchFound != null)
                            {
                                OnSearchFound(sp.Name);
                                System.Windows.Forms.Application.DoEvents();
                            }
                        }
                    }
                }
                else
                {
                    foreach (_StoredProcedure sp in colSP)
                    {

                        if (sp.Text.IndexOf(searchText) >=0)
                        {
                            if (OnSearchFound != null)
                            {
                                OnSearchFound(sp.Name);
                            }
                        }
                    }
                }
            }
            catch (System.Exception ex)
            {
                if(OnInfo != null)
                {
                    OnInfo(ex.Message);
                }
            }
        }
    }
} 

Have fun with this!

License

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


Written By
Software Developer (Senior)
Vietnam Vietnam
A FCG Vietnam's staff and a fan of Microsoft too Wink | ;)



Comments and Discussions

 
GeneralGreat Article + Diff Approach to finding SPs Pin
mnachu10-Jul-08 5:36
mnachu10-Jul-08 5:36 
QuestionIs it possible to do this using SQL Management Objects? Pin
Dale Thompson18-Sep-07 4:57
Dale Thompson18-Sep-07 4:57 
AnswerRe: Is it possible to do this using SQL Management Objects? Pin
Triet Ho18-Sep-07 5:55
Triet Ho18-Sep-07 5:55 
AnswerRe: Is it possible to do this using SQL Management Objects? [modified] Pin
Triet Ho23-Sep-07 2:02
Triet Ho23-Sep-07 2:02 

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.