从C#应用程序打开MSQL过程(使用System.Diagnostics)

时间:2018-03-22 14:37:17

标签: c# sql-server stored-procedures path

我很难找到直接向MSQL管理工作室打开存储过程的解决方案,以便从我的C#应用程序(winform)中修改新的SQLQuery。

这是我的代码:

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();

执行代码后出现错误:“在命令行中指定了以下文件:dbo.getResults无法找到这些文件,也无法加载。”

我应该如何“指向”C#中的存储过程并显示其定义并准备好在MSQL管理工作室中进行修改?

2 个答案:

答案 0 :(得分:1)

我害怕这是不可能的。如果从命令行运行ssms -?,则可以看到可以传入的所有参数:

enter image description here

一些选项:

  1. 让用户自己编辑过程。毕竟,任何能够做到这一点的人都会理解如何正确使用SSMS。

  2. 制作自己的用户界面。您可以读取存储过程的内容并将其显示在文本框中。缺点是你失去了语法高亮等功能(除非你也构建它)

  3. 您可以下载该程序并将其存储在procedure.sql文件中,然后让SSMS打开它。不要忘记传入服务器,数据库和凭据。

答案 1 :(得分:0)

我找到了一种方法来直接向MSQL管理工作室打开存储过程脚本,以便从我的C#应用​​程序(winform)中修改新的SQLQuery。

  1. 我正在使用EXEC sp_helptext 'procedure_name'
  2. 的程序脚本
  3. 结果集填入DataSet
  4. DataSet将以空的.sql文件编写
  5. 使用System.Diagnostics;
  6. 在MSQL Managment Studio中打开.sql文件

    以下是代码片段的步骤:

     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();          
      }   
    

    第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();
    }