Red Gate forums :: View topic - Execute sql packager package in a automated update program .
Return to www.red-gate.com RSS Feed Available

Search  | Usergroups |  Profile |  Messages |  Log in  Register 
Go to product documentation
SQL Packager 6
SQL Packager 6 forum

Execute sql packager package in a automated update program .

Search in SQL Packager 6 forum
Post new topic   Reply to topic
Jump to:  
Author Message
Warrenla



Joined: 14 Jul 2009
Posts: 4

PostPosted: Sat Dec 04, 2010 12:38 am    Post subject: Execute sql packager package in a automated update program . Reply with quote

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....
Back to top
View user's profile Send private message
Brian Donahue



Joined: 23 Aug 2004
Posts: 6678

PostPosted: Sun Dec 05, 2010 11:23 am    Post subject: Reply with quote

Hello Warren,

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

// 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.
Back to top
View user's profile Send private message
Warrenla



Joined: 14 Jul 2009
Posts: 4

PostPosted: Mon Dec 06, 2010 6:12 pm    Post subject: Reply with quote

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
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic All times are GMT + 1 Hour
Page 1 of 1

 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group