Execute Sql Server 2005 Integration Services package from C#--zt
2008-09-23 12:31
525 查看
http://www.codeproject.com/KB/database/CallSSISFromCSharp.aspx
Example SSIS Package
For the purposes of demonstration, I created a simple package that takes some data out of a Sql Server AdvantureWorks database and dumps it into a flatfile. The package also has one variable.In any kind of "real world" scenario, your package will usually be driven by a configuration file. A SSIS configuration file is an XML file with a .dtsConfig extension that contains settings you can apply to a package (without actually changing or editing the package). In my example files, you can edit the configuration file with your Sql Server and flat file connection information and run the package. You'll never have to edit the actual package file. Here's a very good tutorial on configuration file syntax. Configurations can also be stored in a Sql Server table, but I won't cover that here.
Let's start coding...
You need to add a referrence toMicrosoft.SQLServer.ManagedDTS.dll. I believe that this dll is only installed on a machine that has Sql Server components installed.
The amount of code to execute a SSIS package is surprisingly small and concise. Notice that I added a
usingdirective for the
Microsoft.SqlServer.Dts.Runtime namespace.
Collapse
using System; using System.Collections.Generic; using System.Text; using Microsoft.SqlServer.Dts.Runtime; namespace ExecuteSSIS { class Program { static void Main(string[] args) { Application app = new Application(); // // Load package from file system // Package package = app.LoadPackage("c:\\ExamplePackage.dtsx", null); package.ImportConfigurationFile("c:\\ExamplePackage.dtsConfig"); Variables vars = package.Variables; vars["MyVariable"].Value = "value from c#"; DTSExecResult result = package.Execute(); Console.WriteLine("Package Execution results: {0}",result.ToString()); // // Load package from Sql Server // Package package2 = app.LoadFromSqlServer( "ExamplePackage","server_name", "sa", "your_password", null); package2.ImportConfigurationFile("c:\\ExamplePackage.dtsConfig"); Variables vars2 = package2.Variables; vars2["MyVariable"].Value = "value from c# again"; DTSExecResult result2 = package2.Execute(); Console.WriteLine("Package Execution results: {0}", result2.ToString()); } } }
First, you create an
Applicationobject, which provides access to the DTS (Integration Services) runtime. Then you use the
Applicationobject to load a packge from either the file system or from Sql Server, I've demonstrated both. Once you have the package loaded into a
Packageobject, you call the
ImportConfigurationFile()method to load and apply the configuration file to the package. The
Packageobject also has a
Variablescollection that provides access to the package's variable. Finally, to actually execute a package, call the
Execute()method.
Conclusion
相关文章推荐
- Beginners Guide to SQL Server Integration Services Using Visual Studio 2005
- Beginners Guide to SQL Server Integration Services Using Visual Studio 2005
- SQL Server 2005的又一强大功能---(SQL Server Integration Services,SSIS)
- Configuring Internet Information Services for SQL Server 2005 Compact Edition Remote Data Access and Merge Replication
- Error message when you try to install a SQL Server 2005 service pack or a SQL Server 2005 hotfix package: "Error 29528. The setup has encountered an unexpected error while Setting Internal Properties"
- HOW TO: Troubleshoot Application Performance with SQL Server -zt from MS
- SQL Server Integration Services教程4: 添加日志记录
- ETL学习之四:SQL Server Integration Services入门
- Microsoft SQL Server 2005提示未能加载包"Microsoft SQL Management Studio Package "
- Professional SQL Server Analysis Services 2005 with MDX
- SQL Server Integration Services报错
- SQL Server 2005 Service Pack 2 安全性更新(KB948109) 后 SQL Server Reporting Services 2005服务不能启动 解决方案
- SQL Server Integration Services (SSIS)的体系结构图
- Importing Excel data with SQL Server Integration Services and dealing with unicode and non-unicode data issues
- SQL Server Integration Services
- SQL Server Integration Services教程3 :添加包配置
- SQL SERVER Integration Services功能概述
- SQL Server Analysis Services Port (SQL 2005 / 2008)
- Sql server 导入、导出时出现一个错误,但 SQL Server Integration Services 向导未做好处理
- Reporting Service 和 SQL Server Integration Services 服务不能启动