Execute sql packager package in a automated update program .

WarrenlaWarrenla Posts: 5
edited December 6, 2010 12:12PM in SQL Packager Previous Versions
I have a windows service that runs on a DB server of a customer.
This service can download the sqlpackage.exe that sql packager creates.

I want to execute the package via my program passing in the correct command line parameters via c# and wait until it finishes..

Has anyone attempted something similar and where could I find an example....

Comments

  • Brian DonahueBrian Donahue Posts: 6,590 Bronze 1
    Hello Warren,

    It's really easy to start a process in C#. Basically this:
    using System.Diagnostics;
    ...
               ProcessStartInfo psi=new ProcessStartInfo("SqlPackage.exe",
                    "/database:MyDatabase /quiet");
                psi.UseShellExecute = false;
                psi.CreateNoWindow = false;
                Process p = new Process();
                p.StartInfo = psi;
                p.Start();
    

    You probably also want to plumb in some logic to wait for the process and capture the output from the package. You can use p.WaitForExit() to block your service until the package is finished and then check the p.ExitCode property to see what the numeric result was. In a service, nobody will see the output, though, so you have to hide the window and optionally send the package output somewhere else like a log file, etc.
    // in your namespace...
    public delegate void PackageStatusHandler(object o, StatusEventArgs e);
    public class StatusEventArgs : EventArgs
    {
        public string Message;
        public int Percentage;
        public StatusEventArgs(string message, int percentage);
    }
    // in your class...
    // When you subscribe to this event, every line of output will be sent to the
    // subscriber so you can log it, etc
    public event PackageStatusHandler StatusUpdate;
    // your method to run the package
    public int RunPackage()
    {
        Process process = new Process();
            process.StartInfo.FileName = "sqlpackage.exe");
            process.StartInfo.Arguments = "/database:MyDatabase /quiet";
        process.StartInfo.CreateNoWindow = true;
        process.StartInfo.UseShellExecute = false;
        process.StartInfo.RedirectStandardError = true;
        process.Start();
        this.onStatusUpdate(process.Id.ToString(), -1);
        StreamReader standardError = process.StandardError;
        string msg = null;
        try
        {
            while (true)
            {
                msg = standardError.ReadLine();
                if (msg.Length > 0)
                {
                    this.onStatusUpdate(msg, 0);
                }
            }
        }
        catch (Exception)
        {
        }
        process.WaitForExit();
        this.onStatusUpdate("Command Returned " + process.ExitCode.ToString(), 0);
        return process.ExitCode;
    }
    // Method to invoke the delegate
    private void onStatusUpdate(string msg, int percentage)
    {
        if (this.StatusUpdate != null)
        {
            StatusEventArgs e = new StatusEventArgs(msg, percentage);
            this.StatusUpdate(null, e);
        }
    }
    
    Maybe the latter is a bit complicated, but I had the source handy. Hope it helps.
  • Thanks for the pointers! I will try this out and see if I get the desired results and I really appreciate the extra advice on p.waitforexit...


    Thanks
Sign In or Register to comment.