繁体   English   中英

SSIS平面文件存在-没有文件时脚本任务失败

[英]SSIS flat file exists — script task fails when no file exists

我正在SQL Server 2008 R2下的SSIS(BIDS)中工作。 我有一个将平面文件导入OLE DB的程序包。 在导入数据流任务之前,我有一个脚本任务(用C#编写,不是VB编写)来测试文件是否存在。 我的脚本任务有2个优先约束。 第一个是我的成功路径(评估操作=“约束”和值=“成功”),该路径用于数据流任务。 第二个是我的失败路径(Evaluation Operation ='Constraint'和Value ='Failure'),它转到一个虚拟任务(一个SQL任务),以使该包在文件不存在时不会失败。 。

在调试中,我确认当文件存在时,它会一直执行数据流任务(如预期的那样)。 但是,当文件不存在时,程序包将失败。 特别是,它在第​​一步(即脚本任务)失败。 我不知道我在做什么错。 以下是我的脚本任务代码:

using System;
using System.Data;
using Microsoft.SqlServer.Dts.Runtime;
using System.Windows.Forms;
using System.IO;

namespace ST_2f8cf79f6fe0443b9c09c453433a0258.csproj
{
    [System.AddIn.AddIn("ScriptMain", Version = "1.0", Publisher = "", Description = "")]
    public partial class ScriptMain : Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase
    {

        #region VSTA generated code
        enum ScriptResults
        {
            Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success,
            Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure
        };
        #endregion

        public void Main()
        {
            if (File.Exists(Dts.Variables["PRC_file_path"].Value.ToString()))
            {
                Dts.TaskResult = (int)ScriptResults.Success;
            }
            else
            {
                Dts.TaskResult = (int)ScriptResults.Failure;
            }
        }

    }
}

据我所知,它的行为完全符合预期。 如果文件不存在,它将使脚本失败。

我会改用一个变量来报告文件的存在。

public void Main()
{ 
string targetfile = Dts.Variables["PRC_file_path"].Value.ToString();

 if (File.Exists(targetfile))
            {
                Dts.Variables["file_exists"].Value = true;
            }
            else
            {
                 Dts.Variables["file_exists"].Value = false;
            }
Dts.TaskResult = (int)ScriptResults.Success;
}

您希望脚本本身成功,除非遇到错误。

更好的是:

public void Main()
{ 
string targetfile = Dts.Variables["PRC_file_path"].Value.ToString();
  try{
 if (File.Exists(targetfile))
            {
                Dts.Variables["file_exists"].Value = true;
            }
            else
            {
                 Dts.Variables["file_exists"].Value = false;
            }

     Dts.TaskResult = (int)ScriptResults.Success;
     }
 catch (Exception Ex)
     {
     Dts.TaskResult = (int)ScriptResults.Failure;
     }
}

编辑:

忘了提及您需要将优先级约束从CONSTRAINT切换到Expression和CONSTRAINT,其中表达式计算@file_exists变量。 您应该有两条成功的道路,一条将变量的评估结果设为true,将另一条评估为false。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM