Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How do I extract Stored procedures programatically

Posted on 2007-11-26
10
Medium Priority
?
2,200 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
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 
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

Tech or Treat! - Giveaway

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

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

I have a large data set and a SSIS package. How can I load this file in multi threading?
An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
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.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

604 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