Execute sql packager package in a automated update program .

Forum for users of SQL Packager database archive utility

Execute sql packager package in a automated update program .

Postby Warrenla » Fri Dec 03, 2010 11:38 pm

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....
Posts: 3
Joined: Tue Jul 14, 2009 10:45 pm

Postby Brian Donahue » Sun Dec 05, 2010 10:23 am

Hello Warren,

It's really easy to start a process in C#. Basically this:
Code: Select all
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;

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.
Code: Select all
// 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;
    this.onStatusUpdate(process.Id.ToString(), -1);
    StreamReader standardError = process.StandardError;
    string msg = null;
        while (true)
            msg = standardError.ReadLine();
            if (msg.Length > 0)
                this.onStatusUpdate(msg, 0);
    catch (Exception)
    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.
Brian Donahue
Posts: 6590
Joined: Mon Aug 23, 2004 9:48 am

Postby Warrenla » Mon Dec 06, 2010 5:12 pm

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...

Posts: 3
Joined: Tue Jul 14, 2009 10:45 pm

Return to SQL Packager Previous Versions

Who is online

Users browsing this forum: No registered users and 0 guests