Q:

Open MSQL procedure from C# application (using System. Diagnostics)

I am having difficulties in finding a solution for opening a stored procedure straight to MSQL management studio for modifying in a new SQLQuery from my C# application (winform).  
Here is my code:
Process openSQL = new Process();
openSQL.StartInfo. FileName = "Ssms.exe";
openSQL.StartInfo. Arguments = "dbo.getResults"; //name of the stored procedure I want to open 
openSQL.Start();

I am getting error after executing the code :"The following files were specified on the command line: dbo.getResults These files could not be found and will not be loaded." 
How am I supposed to "point" to the stored procedure in C# and get its definition displayed and ready to get modifications in MSQL management studio? 

A:

I found a way to open a stored procedure script straight to MSQL management studio for modifying in a new SQLQuery from my C# application (winform). 

I am taking the script of the procedure with EXEC sp_helptext 'procedure_name' 
The result set is filled in a DataSet
And the DataSet is getting written in an empty .sql file
The .sql file is getting opened in MSQL Managment Studio with System. Diagnostics;

Here are the steps with code snippets: 
 private void saveProcToAFile()
 {
     StreamWriter log;

     if (! File. Exists("procedureToBeLoaded.sql"))
     {
         log = new StreamWriter("procedureToBeLoaded.sql");
     }
     else
     {        
         log = new StreamWriter(File. Create("procedureToBeLoaded.sql"));
     }           

     SqlConnection conn = new SqlConnection(conString);
     SqlDataAdapter da = new SqlDataAdapter();
     SqlCommand cmd = conn. CreateCommand();
     cmd. CommandText = string. Format("EXEC sp_helptext '{0}'", "procedure_name"); //Step 1. 
     da. SelectCommand = cmd;
     DataSet ds = new DataSet();

     conn. Open();
     da. Fill(ds); //Step 2. 
     conn. Close();

     foreach (DataRow dr in ds. Tables[0].Rows)
     {
         log. WriteLine(dr[0]); //Step 3. 
     }

     log. Close();          
  }   

Step 4. 
private void contextMenuStripOpenInSqlStudio_Click(object sender, EventArgs e)
{
    saveProcToAFile();
    Process openSQL = new Process();
    openSQL.StartInfo. FileName = "Ssms.exe";
    openSQL.StartInfo. Arguments = "procedureToBeLoaded.sql";
    openSQL.Start();
} 

