我见过很多代码,但显然它们无济于事。
这是我必须遵循的逻辑:
我有一个正在读取的文件,其中有一个金额字段。此金额字段可以有负数。如果我在文件中发现负数,我必须移动文件并使包失败。
到目前为止我做了什么:
添加了一个脚本任务来获取文件路径,添加了循环来读取该路径中的所有文件,在循环内部(添加了一个 DTS 任务来将负数的成员移动到记录集,添加了脚本任务来检查是否有任何行在记录集,如果计数 > 0 则脚本任务失败,因此包失败,否则通过)
scipt 任务的代码:
using System;
using System.Data;
using Microsoft.SqlServer.Dts.Runtime;
using System.Windows.Forms;
using System.Data.OleDb;
using System.IO;
using System.Xml;
// name space call here etc, etc..
enum ScriptResults
{
Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success,
Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure
};
public void Main()
{
// TODO: Add your code here
bool fireAgain = true;
DataTable dtable = new DataTable();
OleDbDataAdapter da = new OleDbDataAdapter();
// Setting this incase there are multiple files being checked
Dts.Variables["InvalidFile"].Value = "False";
da.Fill(dtable, Dts.Variables["NegativeAmountMember"].Value);
// I do get my desired result here of 1
Dts.Events.FireInformation(0, "dtable", dtable.Rows.Count.ToString(), String.Empty, 0, ref fireAgain);
if (dtable.Rows.Count > 0)
{
Dts.Variables["InvalidFile"].Value = "True";
Dts.Events.FireInformation(0, "InvalidFile", Dts.Variables["InvalidFile"].Value.ToString(), String.Empty, 0, ref fireAgain); // This does get displayed but, nothing happens after this step.
Dts.TaskResult = (int)ScriptResults.Failure; // this method never technically executes
return; // added this because someone somewhere said in a post to do so
}
else
{
Dts.TaskResult = (int)ScriptResults.Success;
}
}
我一切正常,除了这个脚本任务失败。
我也尝试过 FireError,即使它没有被调用。
这是怎么回事,谁能解释一下?