?
Solved

How do I extract Stored procedures programatically

Posted on 2007-11-26
10
Medium Priority
?
2,191 Views
Last Modified: 2012-08-13
Experts,

As a background, I'm fairly new (or obsolete) on DB development. I'm using C# to build an application that would extract the list stored procedures found in a particular DB (SQL Sever 2K5). The question is, how do I do it programatically? and by doing so, will I be able to see the whole stored procedure, names and paramenters, or only names?
Thanks
0
Comment
Question by:Trancos
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 35

Expert Comment

by:YZlat
ID: 20353025
to select the names of stored procedures in a particular database, use the following sql:

select name from sysobjects where type='P'
0
 
LVL 35

Expert Comment

by:YZlat
ID: 20353053
to select only user-defined stored procedures, use

select name from sysobjects where type='P' and category=0
0
 
LVL 32

Expert Comment

by:Brendt Hess
ID: 20353113
There are a few ways that may work to perform this task, and, depending on which way you use, you may be able to get parameter information.  One of the easiest method to get both the stored procedures and the parameters would be to use the query provided by YZlat above.  Then, using the information in that query, you can use an ADO command object, set it up to access each individual stored procedure, then use a Parameters.Refresh action to load the parameters into the .Parameters collection.  Spin through the collection, and you have all of the parameters (and their in/out status)
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 35

Expert Comment

by:YZlat
ID: 20353160
oh, and to retrieve the text of the stored procedure, use

SELECT text    
FROM syscomments    
WHERE id = (SELECT id FROM sysobjects WHERE name = 'YOUR STORED PROC NAME HERE')    
ORDER BY colid
0
 
LVL 35

Accepted Solution

by:
YZlat earned 1400 total points
ID: 20353265
copy those 2 functions to your code:

public static SqlConnection GetConnection(string strConn)
            {
                  SqlConnection conn=null;
                  try
                  {
                        conn = new SqlConnection(strConn);
                        conn.Open();
                  }
                  catch (SqlException ex)
                  {
                        Console.Write("SQL ERROR: " + ex.Message);
                  }
                  catch (Exception ex)
                  {
                        Console.Write("ERROR: " + ex.Message);
                  }
                  return conn;
            }

            public static DataSet GetDataSet(string strConn, string query)
            {
                  SqlConnection conn=null;

                  DataSet dset = new DataSet();
                  try
                  {
                        conn = GetConnection(strConn);
                        SqlDataAdapter da = new SqlDataAdapter(query, conn);
                        da.SelectCommand.CommandTimeout = 0;
                        da.Fill(dset);
                  }
                  catch (SqlException ex)
                  {
                        Console.Write("SQL ERROR: " + ex.Message);
                  }
                  catch (Exception ex)
                  {
                        Console.Write("ERROR: " + ex.Message);
                  }
                  finally
                  {
                        if (conn.State == ConnectionState.Open)
                        {
                              conn.Close();
                        }
                  }
                  return dset;
            }


then call GetDataSet:

DataSet ds=new DataSet();
string query="select name from sysobjects where type='P' and category=0";
string strConn="YOUR DATABASE CONNECTION STRING HERE";
ds=GetDataSet(strConn,query);

if (ds.Tables.Count > 0)
{
      if(ds.Tables[0].Rows.Count > 0)
      {
            for(int i=0; i<=ds.Tables[0].Rows.Count -1;i++)
            {
                  string proc_name=ds.Tables[0].Rows[i].ItemArray[0].ToString();
                  //here get stored proc text using procedure name
                  string sql="SELECT text FROM syscomments WHERE id=(SELECT id FROM sysobjects WHERE name = '" + proc_name +"')";
                  DataSet ds2=new DataSet();
                  ds2=GetDataSet(strConn, sql);

                  //your code here
            }//end for
      }//end if
}//end if
0
 
LVL 12

Expert Comment

by:kselvia
ID: 20353964
You can also use SQLDMO.

Here is an example in .ASP

http://www.developerfusion.co.uk/show/1820/4/
0
 
LVL 1

Author Comment

by:Trancos
ID: 20354199
YZLat.
I'll try your solution and get back later with the results.
Thanks all for your help.
0
 
LVL 1

Author Comment

by:Trancos
ID: 20354766

OK,

code works perfectly, however I'm unable to follow bhess1 recommendation to obtain the list of parameters. I have the code shown below, but I'm unable to call a refresh or update or whatever method would allow me to obtaint the parameters for the stored procedure:

SqlConnection tempConnection=GetConnection(strConn);
SqlCommand adoCmd = new SqlCommand(proc_name, tempConnection);
adoCmd.CommandType = CommandType.StoredProcedure;

Open in new window

0
 
LVL 32

Assisted Solution

by:Brendt Hess
Brendt Hess earned 600 total points
ID: 20354837
Try adding this instead - it has changed in .Net, and it took me a bit of tracking to find the new syntax

SqlCommandBuilder.DeriveParameters(adoCmd);

This should populate the .Parameters collection of the SqlCommand object
0
 
LVL 1

Author Closing Comment

by:Trancos
ID: 31411074
Ignorance is expensive. Thanks guys for enlightening the path.
0

Featured Post

Percona Live Europe 2017 | Sep 25 - 27, 2017

The Percona Live Open Source Database Conference Europe 2017 is the premier event for the diverse and active European open source database community, as well as businesses that develop and use open source database software.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Viewers will learn how the fundamental information of how to create a table.
Suggested Courses

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question