在Dynamics AX2009中启动SSIS包
在AX中需要读取文本文件里的数据然后进行处理,先讲文本文件写入到SQL Sever的表里然后再进行处理会方便些,通过TextIo,TextBuffer逐条读取然后插入到SQL表里,速度是种折磨。SSIS读取文本文件然后写入SQL表的速度非常快,所以用SSIS包导入数据是很好的一个方法。
如果用户需要实时查看导入是否成功以及导入数据的情况,就需要在AX中同步调用执行SSIS包才行。
http://msdn.microsoft.com/zh-cn/library/ms403355(v=SQL.100).aspx
这篇MSDN中介绍了两种方式可以调用执行SSIS包。
其中第一种是通过存储过程启动Job来实现的,这个好处是客户端不需要安装SSIS就可以直接调用,包运行在服务器端,但是这种方式存储过程启动job后就返回了,包有没有正常执行,它就不管了,控制没那么好。
第二种方式可以直接加载包并执行,但是必须在安装了SSIS的服务器上运行。好处是同步执行,可以得到包运行的结果。
如果AOS运行在服务器端,可以在AX中用如下代码启动SSIS包:
代码
InterOpPermission interOp = new InterOpPermission(InterOpKind::ClrInterop);
Microsoft.SqlServer.Dts.Runtime.Application app;
Microsoft.SqlServer.Dts.Runtime.Package pkg;
Microsoft.SqlServer.Dts.Runtime.IDTSEvents event;
Microsoft.SqlServer.Dts.Runtime.DTSExecResult result;
;
interOp.assert();
event = CLRInterop::Null("Microsoft.SqlServer.Dts.Runtime.IDTSEvents");
app = new Microsoft.SqlServer.Dts.Runtime.Application();
pkg = app.LoadFromSqlServer("InventTable","","","",event);
result = pkg.Execute();
if(result == Microsoft.SqlServer.Dts.Runtime.DTSExecResult::Success)
box::info("Success!");
else
if(result == Microsoft.SqlServer.Dts.Runtime.DTSExecResult::Failure)
box::info("failure!");
else
box::info("I don't know!");
CodeAccessPermission::revertAssert();
Microsoft.SqlServer.Dts.Runtime.Application app;
Microsoft.SqlServer.Dts.Runtime.Package pkg;
Microsoft.SqlServer.Dts.Runtime.IDTSEvents event;
Microsoft.SqlServer.Dts.Runtime.DTSExecResult result;
;
interOp.assert();
event = CLRInterop::Null("Microsoft.SqlServer.Dts.Runtime.IDTSEvents");
app = new Microsoft.SqlServer.Dts.Runtime.Application();
pkg = app.LoadFromSqlServer("InventTable","","","",event);
result = pkg.Execute();
if(result == Microsoft.SqlServer.Dts.Runtime.DTSExecResult::Success)
box::info("Success!");
else
if(result == Microsoft.SqlServer.Dts.Runtime.DTSExecResult::Failure)
box::info("failure!");
else
box::info("I don't know!");
CodeAccessPermission::revertAssert();
这段代码看上去如此笨拙,用switch case多美观?唉,不知道X++咋整的,用if判断好好的,如果换成switch case来判断枚举类型,如下所示:
代码
static server void main(Args _args)
{
InterOpPermission interOp = new InterOpPermission(InterOpKind::ClrInterop);
Microsoft.SqlServer.Dts.Runtime.Application app;
Microsoft.SqlServer.Dts.Runtime.Package pkg;
Microsoft.SqlServer.Dts.Runtime.IDTSEvents event;
Microsoft.SqlServer.Dts.Runtime.DTSExecResult result;
;
interOp.assert();
event = CLRInterop::Null("Microsoft.SqlServer.Dts.Runtime.IDTSEvents");
app = new Microsoft.SqlServer.Dts.Runtime.Application();
pkg = app.LoadFromSqlServer("InventTable","","","",event);
result = pkg.Execute();
/*
if(result == Microsoft.SqlServer.Dts.Runtime.DTSExecResult::Success)
box::info("Success!");
else
if(result == Microsoft.SqlServer.Dts.Runtime.DTSExecResult::Failure)
box::info("failure!");
else
box::info("I don't know!");
*/
switch(result)
{
case Microsoft.SqlServer.Dts.Runtime.DTSExecResult::Success:
box::info("Success!");
break;
case Microsoft.SqlServer.Dts.Runtime.DTSExecResult::Failure:
box::info("Failure!");
break;
default:
box::info("I don't know!");
break;
}
CodeAccessPermission::revertAssert();
}
{
InterOpPermission interOp = new InterOpPermission(InterOpKind::ClrInterop);
Microsoft.SqlServer.Dts.Runtime.Application app;
Microsoft.SqlServer.Dts.Runtime.Package pkg;
Microsoft.SqlServer.Dts.Runtime.IDTSEvents event;
Microsoft.SqlServer.Dts.Runtime.DTSExecResult result;
;
interOp.assert();
event = CLRInterop::Null("Microsoft.SqlServer.Dts.Runtime.IDTSEvents");
app = new Microsoft.SqlServer.Dts.Runtime.Application();
pkg = app.LoadFromSqlServer("InventTable","","","",event);
result = pkg.Execute();
/*
if(result == Microsoft.SqlServer.Dts.Runtime.DTSExecResult::Success)
box::info("Success!");
else
if(result == Microsoft.SqlServer.Dts.Runtime.DTSExecResult::Failure)
box::info("failure!");
else
box::info("I don't know!");
*/
switch(result)
{
case Microsoft.SqlServer.Dts.Runtime.DTSExecResult::Success:
box::info("Success!");
break;
case Microsoft.SqlServer.Dts.Runtime.DTSExecResult::Failure:
box::info("Failure!");
break;
default:
box::info("I don't know!");
break;
}
CodeAccessPermission::revertAssert();
}
运行时它就会告诉你:
看来X++和CLR的整合问题还是有些问题,但愿6.0会好一些,或者直接改成.NET得了。