How to execute an .SQL script file using c#

后端 未结 10 923
忘了有多久
忘了有多久 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:30

    I managed to work out the answer by reading the manual :)

    This extract from the MSDN

    The code example avoids a deadlock condition by calling p.StandardOutput.ReadToEnd before p.WaitForExit. A deadlock condition can result if the parent process calls p.WaitForExit before p.StandardOutput.ReadToEnd and the child process writes enough text to fill the redirected stream. The parent process would wait indefinitely for the child process to exit. The child process would wait indefinitely for the parent to read from the full StandardOutput stream.

    There is a similar issue when you read all text from both the standard output and standard error streams. For example, the following C# code performs a read operation on both streams.

    Turns the code into this;

    Process p = new Process();
    p.StartInfo.UseShellExecute = false;
    p.StartInfo.RedirectStandardOutput = true;
    p.StartInfo.FileName = "sqlplus";
    p.StartInfo.Arguments = string.Format("xxx/xxx@{0} @{1}", in_database, s);
    
    bool started = p.Start();
    // important ... read stream input before waiting for exit.
    // this avoids deadlock.
    string output = p.StandardOutput.ReadToEnd();
    
    p.WaitForExit();
    
    Console.WriteLine(output);
    
    if (p.ExitCode != 0)
    {
        Console.WriteLine( string.Format("*** Failed : {0} - {1}",s,p.ExitCode));
        break;
    }
    

    Which now exits correctly.

提交回复
热议问题