How to execute an .SQL script file using c#

后端 未结 10 901
忘了有多久
忘了有多久 2020-11-22 17:03

I\'m sure this question has been answered already, however I was unable to find an answer using the search tool.

Using c# I\'d like to run a .sql file. The sql file

10条回答
  •  不要未来只要你来
    2020-11-22 17:48

    There are two points to considerate.

    1) This source code worked for me:

    private static string Execute(string credentials, string scriptDir, string scriptFilename)
    { 
      Process process = new Process();
      process.StartInfo.UseShellExecute = false;
      process.StartInfo.WorkingDirectory = scriptDir;
      process.StartInfo.RedirectStandardOutput = true;
      process.StartInfo.FileName = "sqlplus";
      process.StartInfo.Arguments = string.Format("{0} @{1}", credentials, scriptFilename);
      process.StartInfo.CreateNoWindow = true;
    
      process.Start();
      string output = process.StandardOutput.ReadToEnd();
      process.WaitForExit();
    
      return output;
    }
    

    I set the working directory to the script directory, so that sub scripts within the script also work.

    Call it e.g. as Execute("usr/pwd@service", "c:\myscripts", "script.sql")

    2) You have to finalize your SQL script with the statement EXIT;

提交回复
热议问题