[转]Loading and Running a Local Package Programmatically
本文转自: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 |
or |
|
SSIS Package Store |
|
|
SQL Server |
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
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.
On the Project menu, click Add Reference and add a reference to Microsoft.SqlServer.ManagedDTS.dll. Click OK.
Use the Visual Basic Imports statement or the C# using statement to import the Microsoft.SqlServer.Dts.Runtime namespace.
Add the following code in the main routine. The completed console application should look like the following example.
NoteThe 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.
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
Follow the steps in the preceding example to create a project for this example.
Add the following code in the main routine. The completed console application should look like the following example.
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();
}
}
}
|
[转]Loading and Running a Local Package Programmatically的更多相关文章
- 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 - ...
- 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 ...
- Class loading in JBoss AS 7--官方文档
Class loading in AS7 is considerably different to previous versions of JBoss AS. Class loading is ba ...
- 创建本地yum软件源,为本地Package安装Cloudera Manager、Cloudera Hadoop及Impala做准备
一.包管理工具及CentOS的yum 1.包管理工具如何发现可以用的包 包管理工具依赖一系列软件源,工具下载源的信息存储在配置文件中,其位置随某包管理工具不同而变化 使用yum的RedHat/Cent ...
- CentOS中yum安装软件时报错:No package XXX available
yum 安装软件时,报错:No package XXX available. [root@localhost ~]# yum -y install redis Loaded plugins: fast ...
- 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 ...
- Linux YUM (Yellowdog Updater, Modified) Commands for Package Management
Linux YUM (Yellowdog Updater, Modified) Commands for Package Management In this article, we will lea ...
- yum -y install pip No package pip available. Error: Nothing to do
centos下安装pip时失败: [root@wfm ~]# yum -y install pipLoaded plugins: fastestmirror, refresh-packagekit, ...
- 安装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 ...
随机推荐
- django “如何”系列1:如何使用REMOTE_USER(远程用户)进行认证
这节主要介绍当web服务器使用了REMOTE_USER的时候,该如何在你的django应用中使用外部的认证源,远程用户主要见于企业内部网,主要使用单点登录解决方案. 在django中,REMOTE_U ...
- Fastcgi协议定义解释与说明
1 响应格式如(十六进制方式显示) 序列 0 1 2 3 4 5 6 7 ... 数值 01 06 00 01 01 1D 03 00... 序列0(值01)为version,固定取1即可序列1(值0 ...
- C# 中从程序中下载Excel模板
方法一: #region 下载模板 /// <summary> /// 下载模板 /// </summary> /// <param name="sender& ...
- 给定一列数字将其平移n位
原题的意思是给定一个指定长度的数组,然后接受一个数字m,将原数组前m位移动到最后,且顺序不变. 看到这个题,想到的第一个方法就是在用一个数组来储存改变后的数字,代码如下 int func(){ int ...
- Eclipse的工程名有红色的感叹号,工程里面没有显示编译错误
在导入其他人或配套光盘中的工程时,经常会出现这种错误. 问题的原因: 通常是JRE的版本不同造成的. 解决的办法: 是选择工程名,然后通过在右键菜单中选择build path->configue ...
- AC日记——[USACO06FEB]奶牛零食Treats for the Cows 洛谷 P2858
[USACO06FEB]奶牛零食Treats for the Cows 思路: 区间DP: 代码: #include <bits/stdc++.h> using namespace std ...
- 禁止网页右键和复制,ctrl+a都不行。取消页面默认事件【全】。
document.oncontextmenu=new Function("event.returnValue=false");document.onselectstart=new ...
- Django2.x版本在生成数据库表初始化文件报错
1.待创建的表信息 from django.db import models # Create your models here. class Book(models.Model): name=mod ...
- 洛谷——P1724 东风谷早苗
P1724 东风谷早苗 题目描述 在幻想乡,东风谷早苗是以高达控闻名的高中生宅巫女.某一天,早苗终于入手了最新款的钢达姆模型.作为最新的钢达姆,当然有了与以往不同的功能了,那就是它能够自动行走,厉害吧 ...
- VM虚拟机安装kali linux
点击文件,新建虚拟机新建一个虚拟机 点击后出现这个,选择典型 点击下一步,然后选择安装程序光盘映像文件(iso),然后浏览,找到你下载的镜像 点击下一步,选择linux,选择最高Debian版本的,6 ...
Stay Up to Date with Integration Services