Execute sql packager package in a automated update program .

Packages and compresses schema and contents of SQL Server databases

Moderators: JonathanWatts, Chris Auckland, David Atkinson, david connell, Anu Deshpande, Michelle Taylor, james.billings, Marianne

Execute sql packager package in a automated update program .

Postby Warrenla » Sat Dec 04, 2010 12:38 am

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....
Warrenla
 
Posts: 4
Joined: Tue Jul 14, 2009 11:45 pm

Postby Brian Donahue » Sun Dec 05, 2010 11: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;
            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.
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;
    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.
Brian Donahue
 
Posts: 6670
Joined: Mon Aug 23, 2004 10:48 am

Postby Warrenla » Mon Dec 06, 2010 6: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...


Thanks
Warrenla
 
Posts: 4
Joined: Tue Jul 14, 2009 11:45 pm


Return to SQL Packager 6

Who is online

Users browsing this forum: No registered users and 0 guests