需求如下,把异常数据从sqlserver数据库中取出来,然后导入到xls中去,然后再发邮件给用户,把xls作为附件发送。

需要的示例数据表: 

/*

create table abnormaldata(rowid bigint , rowname nvarchar(20) ,rowvalue nvarchar(100));

insert into abnormaldata(rowid,rowname,rowvalue) values(1,'studentname','studentA');

insert into abnormaldata(rowid,rowname,rowvalue) values(1,'age','24');

insert into abnormaldata(rowid,rowname,rowvalue) values(2,'studentname','studentB');

insert into abnormaldata(rowid,rowname,rowvalue) values(2,'age','25');

insert into abnormaldata(rowid,rowname,rowvalue) values(3,'studentname','studentC');

insert into abnormaldata(rowid,rowname,rowvalue) values(3,'age','26');

insert into abnormaldata(rowid,rowname,rowvalue) values(4,'studentname','studentD');

insert into abnormaldata(rowid,rowname,rowvalue) values(4,'age','27');

insert into abnormaldata(rowid,rowname,rowvalue) values(5,'studentname','studentE');

insert into abnormaldata(rowid,rowname,rowvalue) values(5,'age','28');

insert into abnormaldata(rowid,rowname,rowvalue) values(6,'studentname','studentF');

insert into abnormaldata(rowid,rowname,rowvalue) values(6,'age','29');

*/

 

我的机器环境是win7 64bit,默认会报如下的错误,是因为oledb没有64位的driver.错误的提示非常明显,而且也指出了如何改正,就是在32位下运行。

[Connection manager "Excel Connection Manager"] Error: The requested OLE DB provider Microsoft.Jet.OLEDB.4.0 is not registered. If the 64-bit driver is not installed, run the package in 32-bit mode. Error code: 0x00000000.

An OLE DB record is available. Source: "Microsoft OLE DB Service Components" Hresult: 0x80040154 Description: "Class not registered".

当然,这只是在开发了阶段debug的选 项,如果我们使用jobagent进行调用的时候,有一个类似的选项可以进行选定。

但如果我们是使用第三方的调度工具,把package写在一个batch文件里面使用Dtexec进行调用的时候,我们则需要注意了。

我们默认在cmd实用程序中调用的dtexec与你的OS版本是一致的,譬如你的os是64位的,一般情况下你调用的则是64位的。

这样仍是会报错的,而你直接双击package跳出来的执行应用程序DTEXECUI默认则是32位的,所以你可以执行成功。

需要注意的是如果是使用batch调用,则需要指定x86文件夹下的32位实用程序。你需要在batch file中这样写:

"C:\Program Files (x86)\Microsoft SQL Server\110\DTS/Binn\DTEXEC.EXE" /FILE "\"C:\Users\Student\Documents\Visual Studio 2010\Projects\Integration Services Project1\Integration Services Project1\Excel demo.dtsx\"" /CHECKPOINTING OFF /REPORTING EWCDI

   

因为我的需求是需要把这个xls作为附件通过邮件发送出去的,我原来直接使用send mail task.来做这件事,当我的xls达到3M的时候,基本上就超时了,因为send mail task默认是100秒,如果发不出去,就报超时的错误,而且不可更改这个参数。

所以我就使用了script task然后通过c#代码来做这件事,但是需要指定一下clienttimeout指定个合适的值,注意它是以millionsencod为基准的。

剩下的就是清除xls中的内容,以供下一次使用。可以使用drop table '…'注意不是delete. 这个drop 不会把sheet去除,只是清除所有的内容,所以需要使用create table再做一件schema.

   

   

完整的SSIS package是这样的:fx是因为我使用了一个变量来判断rowcount是否大于0,否则就不做下面的事了。

posted on 2020-11-06 11:10  湖东  阅读(336)  评论(0编辑  收藏  举报