| Author |
Message |
Warrenla
Joined: 14 Jul 2009 Posts: 4
|
Posted: Sat Dec 04, 2010 12:38 am Post subject: Execute sql packager package in a automated update program . |
|
|
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 |
|
 |
Brian Donahue
Joined: 23 Aug 2004 Posts: 6369 Location: Red Gate Software
|
Posted: Sun Dec 05, 2010 11:23 am Post subject: |
|
|
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. _________________ Brian Donahue
Technical Support
Red Gate Software Ltd.
44 (0)870 160 0037 ext 8521
US and CAN 1-866-RED GATE ext 8521 |
|
| Back to top |
|
 |
Warrenla
Joined: 14 Jul 2009 Posts: 4
|
Posted: Mon Dec 06, 2010 6:12 pm Post subject: |
|
|
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 |
|
 |
|
|
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