本文转自:http://msdn.microsoft.com/en-us/library/ms136090.aspx

You can run Integration Services packages as needed or at predetermined times by using the methods described in Running Packages. However, with only a few lines of code, you can also run a package from a custom application such as a Windows Forms application, a console application, an ASP.NET Web form or Web service, or a Windows service.

This topic discusses:

  • Loading a package programmatically

  • Running a package programmatically

All of the methods used in this topic to load and run packages require a reference to the Microsoft.SqlServer.ManagedDTS assembly. After adding the reference in a new project, import the Microsoft.SqlServer.Dts.Runtime namespace with a using orImports statement.

To load a package programmatically on the local computer, whether the package is stored locally or remotely, call one of the following methods:

 

Storage Location

Method to Call

File

LoadPackage

or

LoadPackage

SSIS Package Store

LoadFromDtsServer

SQL Server

LoadFromSqlServer

 Important

The methods of the Application class for working with the SSIS Package Store only support ".", localhost, or the server name for the local server. You cannot use "(local)".

Developing a custom application in managed code that runs a package on the local computer requires the following approach. The steps summarized here are demonstrated in the sample console application that follows.

To run a package on the local computer programmatically

  1. Start the Visual Studio development environment, and create a new application in your preferred development language. This example uses a console application; however you can also run a package from a Windows Forms application, an ASP.NET Web form or Web service, or a Windows service.

  2. On the Project menu, click Add Reference and add a reference to Microsoft.SqlServer.ManagedDTS.dll. Click OK.

  3. Use the Visual Basic Imports statement or the C# using statement to import the Microsoft.SqlServer.Dts.Runtime namespace.

  4. Add the following code in the main routine. The completed console application should look like the following example.

     Note

    The sample code demonstrates loading the package from the file system by using the LoadPackage method. However you can also load the package from the MSDB database by calling the LoadFromSqlServer method, or from the Integration Services package store by calling the LoadFromDtsServer method.

  5. Run the project. The sample code executes the CalculatedColumns sample package that is installed with the SQL Server samples. The result of package execution is displayed in the console window.

Sample Code

 
using System;
using Microsoft.SqlServer.Dts.Runtime; namespace RunFromClientAppCS
{
class Program
{
static void Main(string[] args)
{
string pkgLocation;
Package pkg;
Application app;
DTSExecResult pkgResults; 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, null);
pkgResults = pkg.Execute(); Console.WriteLine(pkgResults.ToString());
Console.ReadKey();
}
}
}

When you run a package programmatically as shown in the preceding sample, you may also want to capture errors and other events that occur as the package executes. You can accomplish this by adding a class that inherits from the DefaultEventsclass, and by passing a reference to that class when you load the package. Although the following example captures only the OnError event, there are many other events that the DefaultEvents class lets you capture.

To run a package on the local computer programmatically and capture package events

  1. Follow the steps in the preceding example to create a project for this example.

  2. Add the following code in the main routine. The completed console application should look like the following example.

  3. Run the project. The sample code executes the CalculatedColumns sample package that is installed with the SQL Server samples. The result of package execution is displayed in the console window, along with any errors that occur.

Sample Code

 
using System;
using Microsoft.SqlServer.Dts.Runtime; namespace RunFromClientAppWithEventsCS
{
class MyEventListener : DefaultEvents
{
public override bool OnError(DtsObject source, int errorCode, string subComponent,
string description, string helpFile, int helpContext, string idofInterfaceWithError)
{
// Add application-specific diagnostics here.
Console.WriteLine("Error in {0}/{1} : {2}", source, subComponent, description);
return false;
}
}
class Program
{
static void Main(string[] args)
{
string 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.ToString());
Console.ReadKey();
}
}
}
 
  Stay Up to Date with Integration Services

For the latest downloads, articles, samples, and videos from Microsoft, as well as selected solutions from the community, visit the Integration Services page on MSDN:

For automatic notification of these updates, subscribe to the RSS feeds available on the page.

[转]Loading and Running a Local Package Programmatically的更多相关文章

  1. mysql初始化/usr/local/mysql/bin/mysqld: error while loading shared libraries: libnuma.so.1: cannot open shared object file: No such file or directory

    [root@test153 ~]# /usr/local/mysql/bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql - ...

  2. Running CMD.EXE as Local System(转)

    Many times in the past I had to run an interactive command-line shell under the Local SYSTEM account ...

  3. Class loading in JBoss AS 7--官方文档

    Class loading in AS7 is considerably different to previous versions of JBoss AS. Class loading is ba ...

  4. 创建本地yum软件源,为本地Package安装Cloudera Manager、Cloudera Hadoop及Impala做准备

    一.包管理工具及CentOS的yum 1.包管理工具如何发现可以用的包 包管理工具依赖一系列软件源,工具下载源的信息存储在配置文件中,其位置随某包管理工具不同而变化 使用yum的RedHat/Cent ...

  5. CentOS中yum安装软件时报错:No package XXX available

    yum 安装软件时,报错:No package XXX available. [root@localhost ~]# yum -y install redis Loaded plugins: fast ...

  6. mysql初始化时报错bin/mysqld: error while loading shared libraries: libnuma.so.1: cannot open shared object file: No such file or directory的处理

    问题描述: 今天新安装了一个linux虚拟机,然后安装mysql 5.7.21,在进行初始化的时候,报错 bin/mysqld: error : cannot open shared object f ...

  7. Linux YUM (Yellowdog Updater, Modified) Commands for Package Management

    Linux YUM (Yellowdog Updater, Modified) Commands for Package Management In this article, we will lea ...

  8. yum -y install pip No package pip available. Error: Nothing to do

    centos下安装pip时失败: [root@wfm ~]# yum -y install pipLoaded plugins: fastestmirror, refresh-packagekit, ...

  9. 安装mysql报错:Can't find messagefile '/usr/share/mysql/english/errmsg.sys'和/usr/bin/mysqladmin: error while loading shared libraries: libmysqlclient.so.16: cannot open shared object file: No such file or

    使用yum安装mysql服务端: [root@centos ~]# yum -y install mysql-server Loaded plugins: fastestmirror, securit ...

随机推荐

  1. selenium+python自动化80-文件下载(不弹询问框)【转载】

    转至博客:上海-悠悠 前言 上一篇是点弹出框上的按钮去保存文件,本篇介绍一种更加优雅的方法,加载Firefox和Chrome的配置文件,不弹出询问框后台下载. 一.FirefoxProfile 1.点 ...

  2. hdu 1024(滚动数组+动态规划)

    Max Sum Plus Plus Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...

  3. Palindrome Partitioning——回溯算法的又一经典

    Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...

  4. opencv python基本操作

    Python usage crop frame: croppedframe = frame[ymin:ymax, xmin:xmax] resize frame: reszframe = cv2.re ...

  5. [python] windows文件迁移

    目的:   处理windows系统文件迁移,文件格式包含特殊字符(空格,括号,全角等) 语言: python 模块: shutil 代码: #coding:utf-8 import os,sys im ...

  6. 兼容python3的SSDB客户端

    SSDB.py import socket class SSDB_Response(object): def __init__(self, code='', data_or_message=None) ...

  7. Go语言有缓冲和无缓冲通道实现样例

    感觉可以,但不好用. 应该有封装程序更高的包包吧. package main import ( "math/rand" "fmt" "time&quo ...

  8. oracle 导入dmp文件

    /*第1步:创建临时表空间  */ create temporary tablespace webdata_temp tempfile 'D:\oracle\product\10.2.0\oradat ...

  9. Openstack 网络服务 Neutron介绍和控制节点部署 (十)

    Neutron介绍 neutron是openstack重要组件之一,在以前是时候没有neutron项目. 早期的时候是没有neutron,早期所使用的网络的nova-network,经过版本改变才有个 ...

  10. UVA 548.Tree-fgets()函数读入字符串+二叉树(中序+后序遍历还原二叉树)+DFS or BFS(二叉树路径最小值并且相同路径值叶子节点权值最小)

    Tree UVA - 548 题意就是多次读入两个序列,第一个是中序遍历的,第二个是后序遍历的.还原二叉树,然后从根节点走到叶子节点,找路径权值和最小的,如果有相同权值的就找叶子节点权值最小的. 最后 ...