Simple way to programmatically get all stored procedures

后端 未结 12 2136
没有蜡笔的小新
没有蜡笔的小新 2020-12-24 09:22

Is there a way to get stored procedures from a SQL Server 2005 Express database using C#? I would like to export all of this data in the same manner that you can script it o

相关标签:
12条回答
  • 2020-12-24 09:33

    You can use:

    DataTable  dtProcs = sqlConn.GetSchema("Procedures", new string[] { databaseName });
    DataTable  dtProcParams = sqlConn.GetSchema("ProcedureParameters", new string[] { databaseName });
    

    You can also get all sorts of other schema info like tables, indexes etc. if you need them.

    You can get info on GetSchema() here and info on the SQL Server Schema Collections here

    Edit: Sorry, this doesn't help with actually scripting the info, but I guess it's useful info to have.

    0 讨论(0)
  • 2020-12-24 09:35

    You can use SMO for that. First of all, add references to these assemblies to your project:

    • Microsoft.SqlServer.ConnectionInfo
    • Microsoft.SqlServer.Smo
    • Microsoft.SqlServer.SmoEnum

    They are located in the GAC (browse to C:\WINDOWS\assembly folder).

    Use the following code as an example of scripting stored procedures:

    using System;
    using System.Collections.Generic;
    using System.Data;
    using Microsoft.SqlServer.Management.Smo;
    
    class Program
    {
       static void Main(string[] args)
       {
          Server server = new Server(@".\SQLEXPRESS");
          Database db = server.Databases["Northwind"];
          List<SqlSmoObject> list = new List<SqlSmoObject>();
          DataTable dataTable = db.EnumObjects(DatabaseObjectTypes.StoredProcedure);
          foreach (DataRow row in dataTable.Rows)
          {
             string sSchema = (string)row["Schema"];
             if (sSchema == "sys" || sSchema == "INFORMATION_SCHEMA")
                continue;
             StoredProcedure sp = (StoredProcedure)server.GetSmoObject(
                new Urn((string)row["Urn"]));
             if (!sp.IsSystemObject)
                list.Add(sp);
          }
          Scripter scripter = new Scripter();
          scripter.Server = server;
          scripter.Options.IncludeHeaders = true;
          scripter.Options.SchemaQualify = true;
          scripter.Options.ToFileOnly = true;
          scripter.Options.FileName = @"C:\StoredProcedures.sql";
          scripter.Script(list.ToArray());
       }
    }
    

    See also: SQL Server: SMO Scripting Basics.

    0 讨论(0)
  • 2020-12-24 09:38

    I think this is what you're really looking for:

    select SPECIFIC_NAME,ROUTINE_DEFINITION from information_schema.routines
    

    There are a ton of other useful columns in there too...

    0 讨论(0)
  • 2020-12-24 09:39

    Assuming you have SqlConnection object called sqlCon, simplest way is to call sqlCon.GetSchema("Procedures")

    0 讨论(0)
  • 2020-12-24 09:40

    This blog post suggests running this against your database:

    select * from sys.procedures
    
    0 讨论(0)
  • 2020-12-24 09:41

    Just read the output of SELECT NAME from SYS.PROCEDURES , then call EXEC sp_HelpText SPNAME for each stored procedure, you'll get a record set with one line of text per row.

    0 讨论(0)
提交回复
热议问题