C#编程方式执行包的任务 --zt
http://www.cnblogs.com/xuanfeng/archive/2008/06/30/1232543.html
1.相关DLL
相关命名空间包含在
Microsoft.SqlServer.ManagedDTS 中,此DLL包含在SQL SERVER安装分驱的
\Program Files\Microsoft SQL Server\90\SDK\Assemblies目录中
2.下面以代码的方式实现以下内容:
A.创建一个包的执行应用程序
B.新建一个包
C.加载一个包
D.给包参数赋值
E.获取包的执行信息
F.Demo下载
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
//由于此命名空间有些成员跟其它命名空间有些冲突,因此替换一下
using dts=Microsoft.SqlServer.Dts.Runtime;
namespace WinSSIS
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (this.openFileDialog1.ShowDialog().Equals(DialogResult.OK))
{
this.textBox1.Text = this.openFileDialog1.FileName;
Executepackage(this.openFileDialog1.FileName);
}
}
private void Executepackage(string path)
{
string message=null ;
//创建一个DTS执行用应程序
dts.Application app = new Microsoft.SqlServer.Dts.Runtime.Application();
//新建一个包
dts.Package package = new Microsoft.SqlServer.Dts.Runtime.Package();
//加载一个包
package= app.LoadPackage(path,null);
//给包变量赋值
if(package.Variables.Contains("filePath"))
{
package.Variables["filePath"].Value = @"d:\SSIS包路径测试.txt";
}
//执行包
dts.DTSExecResult result= package.Execute();
//获取包的执行信息
if(result.Equals(dts.DTSExecResult.Failure))
{
for (int i = 0; i < package.Errors.Count;i++ )
{
message += package.Errors[i].Description;
}
}
if (result.Equals(dts.DTSExecResult.Success))
{
message = "包执行成功成!";
}
else
{
message = "其它1";
}
MessageBox.Show(message);
}
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
//由于此命名空间有些成员跟其它命名空间有些冲突,因此替换一下
using dts=Microsoft.SqlServer.Dts.Runtime;
namespace WinSSIS
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (this.openFileDialog1.ShowDialog().Equals(DialogResult.OK))
{
this.textBox1.Text = this.openFileDialog1.FileName;
Executepackage(this.openFileDialog1.FileName);
}
}
private void Executepackage(string path)
{
string message=null ;
//创建一个DTS执行用应程序
dts.Application app = new Microsoft.SqlServer.Dts.Runtime.Application();
//新建一个包
dts.Package package = new Microsoft.SqlServer.Dts.Runtime.Package();
//加载一个包
package= app.LoadPackage(path,null);
//给包变量赋值
if(package.Variables.Contains("filePath"))
{
package.Variables["filePath"].Value = @"d:\SSIS包路径测试.txt";
}
//执行包
dts.DTSExecResult result= package.Execute();
//获取包的执行信息
if(result.Equals(dts.DTSExecResult.Failure))
{
for (int i = 0; i < package.Errors.Count;i++ )
{
message += package.Errors[i].Description;
}
}
if (result.Equals(dts.DTSExecResult.Success))
{
message = "包执行成功成!";
}
else
{
message = "其它1";
}
MessageBox.Show(message);
}
}
}