1

I'm trying to call SQlPackage from a WinForms project using System.Diagnostics.process

Below is the code snippet I'm using to call Sqlpacakage with DACPAC path and connection string as argument

    ProcessStartInfo procStartInfo = new ProcessStartInfo();
    procStartInfo.FileName = @"C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\Common7\IDE\Extensions\Microsoft\SQLDB\DAC\130\sqlpackage.exe";
    procStartInfo.Arguments = @"/Action:Publish /SourceFile:""C:\Myfiles\Samples\TestDatabse\Snapshots\TestDatabse_20191201_12 - 59 - 10.dacpac"" /TargetConnectionString:""Data Source = Server; Integrated Security = False; Initial Catalog = AdventureWorksDW2014; User ID = User; Password = Password; "" /p:DropObjectsNotInSource=False /p:BlockOnPossibleDataLoss=True /dsp:""C:\sqlOutput\Deploy.sql""";;
    procStartInfo.RedirectStandardOutput = true;
    procStartInfo.UseShellExecute = false;
    procStartInfo.CreateNoWindow = true;

    using (Process process = new Process())
    {
        process.StartInfo = procStartInfo;
        process.Start();

        process.WaitForExit();

        StreamReader reader = process.StandardOutput;
        string output = reader.ReadToEnd();
      }
        
        

When I execute the snippet the process is not waiting till the deployment of database. If I read the output I'm getting the first line of the output like Publishing to database 'AdventureWorksDW2014' on server ''. The process is getting exited after this and the database is not getting updated to given DACPAC.

Is there any suggested way where I can wait till the deployment of given DACPAC is done and DACPAC gets published in server.

1 Answer 1

2

From what it looks like, the exe is failing to process the install/publish. I might be wrong but it would be helpful to output the errors if there is any by redirecting the error output as well. Once you have your error, you should be able to troubleshoot from there.

    procStartInfo.RedirectStandardOutput = true;
    procStartInfo.UseShellExecute = false;
    procStartInfo.CreateNoWindow = true;
    procStartInfo.RedirectStandardError = true; // <--- Add this line

    using (Process process = new Process())
    {
        process.StartInfo = procStartInfo;
        process.Start();
        process.WaitForExit();


        // ---> I would add this here...
        var result = process.StandardOutput.ReadToEnd();
        string err = process.StandardError.ReadToEnd(); // <-- Capture errors

        if (!string.IsNullOrEmpty(err))
        {
           Console.WriteLine(err); // <---- Print any errors for troubleshooting
        }
        Console.WriteLine(result);
        // ----------------        
      }
Sign up to request clarification or add additional context in comments.

3 Comments

thanks @MarcusLai the problem is with the permission associated with file. process.StandardError helped to troubleshoot the issue
be aware of the deadlock possibility: "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" - learn.microsoft.com/en-us/dotnet/api/….
also "A deadlock condition results if the parent process calls p.StandardOutput.ReadToEnd followed by p.StandardError.ReadToEnd and the child process writes enough text to fill its error stream" - learn.microsoft.com/en-us/dotnet/api/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.