Condividi tramite


Run SSIS Package programatically

here is a small sample that show how to run an existing package from a .Net program.

This sample show how to replace target and source connection string and give a value to a package variable.

It show too how to register package event.

using

Runtime = Microsoft.SqlServer.Dts.Runtime;

private PackageEvents packageEvents;

public event PackageEventEventHandler packageEvent = null;

public event PackageProgressEventHandler packageProgress = null;

packageEvents =

new PackageEvents();

packageEvents.packageEvent +=

new PackageEventEventHandler(packageEvents_packageEvent);

packageEvents.packageProgress +=

new PackageProgressEventHandler(packageEvents_packageProgress);

public bool Execute(string packagePath, string sourceConnectionString, string targetConnectionString, string packageVariable)

{

Runtime.

Application app = new Runtime.Application();

Runtime.

Package package = app.LoadPackage(packagePath, null);

package.InteractiveMode =

false;

// Target is the name of the target connection in the SSIS package

package.Connections[

"Target"].ConnectionString = targetConnectionString;

package.Connections[

"Source"].ConnectionString = sourceConnectionString;

// The package have a variable, we give it the value

package.Variables[

"myVar"].Value = packageVariable;

Runtime.

DTSExecResult executionResult = package.Execute(null, null, packageEvents, null, null);

if (executionResult == Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure)

{

return false;

}

return true;

}

void packageEvents_packageProgress(int percentComplete)

{

if (packageProgress != null)

packageProgress(percentComplete);

}

void packageEvents_packageEvent(EventType type, string message)

{

if (packageEvent != null)

packageEvent(type, message);

}