Comment exécuter un package SSIS à partir de .NET?

J'ai un packageage SSIS qui finalement je voudrais passer des parameters aussi, ces parameters viendront d'une application .NET (VB ou C #) donc j'étais curieux si quelqu'un sait comment faire cela, ou mieux encore un site web avec des conseils utiles sur la façon de le faire. Donc, fondamentalement, je veux exécuter un package SSIS à partir de .NET en passant les parameters du packageage SSIS qu'il peut utiliser à l'intérieur. Par exemple, le package SSIS utilisera l'import de file plat dans une database SQL. Cependant, le path et le nom du file peuvent être le paramètre transmis depuis l'application .Net.

Voici comment définir les variables dans le package à partir du code –

using Microsoft.SqlServer.Dts.Runtime; private void Execute_Package() { ssortingng pkgLocation = @"c:\test.dtsx"; Package pkg; Application app; DTSExecResult pkgResults; Variables vars; app = new Application(); pkg = app.LoadPackage(pkgLocation, null); vars = pkg.Variables; vars["A_Variable"].Value = "Some value"; pkgResults = pkg.Execute(null, vars, null, null, null); if (pkgResults == DTSExecResult.Success) Console.WriteLine("Package ran successfully"); else Console.WriteLine("Package failed"); } 

Voici comment le faire avec le catalogue SSDB qui a été introduit avec SQL Server 2012 …

 using System.Collections.Generic; using System.Collections.ObjectModel; using System.Data.SqlClient; using Microsoft.SqlServer.Management.IntegrationServices; public List<ssortingng> ExecutePackage(ssortingng folder, ssortingng project, ssortingng package) { // Connection to the database server where the packages are located SqlConnection ssisConnection = new SqlConnection(@"Data Source=.\SQL2012;Initial Catalog=master;Integrated Security=SSPI;"); // SSIS server object with connection IntegrationServices ssisServer = new IntegrationServices(ssisConnection); // The reference to the package which you want to execute PackageInfo ssisPackage = ssisServer.Catalogs["SSISDB"].Folders[folder].Projects[project].Packages[package]; // Add a parameter collection for 'system' parameters (ObjectType = 50), package parameters (ObjectType = 30) and project parameters (ObjectType = 20) Collection<PackageInfo.ExecutionValueParameterSet> executionParameter = new Collection<PackageInfo.ExecutionValueParameterSet>(); // Add execution parameter (value) to override the default asynchronized execution. If you leave this out the package is executed asynchronized executionParameter.Add(new PackageInfo.ExecutionValueParameterSet { ObjectType = 50, ParameterName = "SYNCHRONIZED", ParameterValue = 1 }); // Add execution parameter (value) to override the default logging level (0=None, 1=Basic, 2=Performance, 3=Verbose) executionParameter.Add(new PackageInfo.ExecutionValueParameterSet { ObjectType = 50, ParameterName = "LOGGING_LEVEL", ParameterValue = 3 }); // Add a project parameter (value) to fill a project parameter executionParameter.Add(new PackageInfo.ExecutionValueParameterSet { ObjectType = 20, ParameterName = "MyProjectParameter", ParameterValue = "some value" }); // Add a project package (value) to fill a package parameter executionParameter.Add(new PackageInfo.ExecutionValueParameterSet { ObjectType = 30, ParameterName = "MyPackageParameter", ParameterValue = "some value" }); // Get the identifier of the execution to get the log long executionIdentifier = ssisPackage.Execute(false, null, executionParameter); // Loop through the log and do something with it like adding to a list var messages = new List<ssortingng>(); foreach (OperationMessage message in ssisServer.Catalogs["SSISDB"].Executions[executionIdentifier].Messages) { messages.Add(message.MessageType + ": " + message.Message); } return messages; } 

Le code est une légère adaptation de http://social.technet.microsoft.com/wiki/contents/articles/21978.execute-ssis-2012-package-with-parameters-via-net.aspx?CommentPosted=true#commentmessage

Il y a aussi un article similaire sur http://domwritescode.com/2014/05/15/project-deployment-model-changes/

Pour append à la réponse @Craig Schwarze,

Voici quelques liens MSDN connexes:

Chargement et exécution d'un package local par programme:

Chargement et exécution d'un package distant par programme

Capture d'events à partir d'un package en cours d'exécution:

 using System; using Microsoft.SqlServer.Dts.Runtime; namespace RunFromClientAppWithEventsCS { class MyEventListener : DefaultEvents { public override bool OnError(DtsObject source, int errorCode, ssortingng subComponent, ssortingng description, ssortingng helpFile, int helpContext, ssortingng idofInterfaceWithError) { // Add application-specific diagnostics here. Console.WriteLine("Error in {0}/{1} : {2}", source, subComponent, description); return false; } } class Program { static void Main(ssortingng[] args) { ssortingng pkgLocation; Package pkg; Application app; DTSExecResult pkgResults; MyEventListener eventListener = new MyEventListener(); pkgLocation = @"C:\Program Files\Microsoft SQL Server\100\Samples\Integration Services" + @"\Package Samples\CalculatedColumns Sample\CalculatedColumns\CalculatedColumns.dtsx"; app = new Application(); pkg = app.LoadPackage(pkgLocation, eventListener); pkgResults = pkg.Execute(null, null, eventListener, null, null); Console.WriteLine(pkgResults.ToSsortingng()); Console.ReadKey(); } } }