简体   繁体   English

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

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

I am working in SSIS (BIDS) under SQL Server 2008 R2. 我正在SQL Server 2008 R2下的SSIS(BIDS)中工作。 I have a package that imports a flat file to an OLE DB. 我有一个将平面文件导入OLE DB的程序包。 Before the import data flow task, I have a script task (written in C#, not VB) to test if the file exists. 在导入数据流任务之前,我有一个脚本任务(用C#编写,不是VB编写)来测试文件是否存在。 I have 2 precedence constraints off of the script task. 我的脚本任务有2个优先约束。 The first one is my success path (Evaluation Operation = 'Constraint' and Value = 'Success'), which goes to a data flow task. 第一个是我的成功路径(评估操作=“约束”和值=“成功”),该路径用于数据流任务。 The second one is my failure path (Evaluation Operation = 'Constraint' and Value = 'Failure'), which goes to a dummy task (a SQL task), just so that the package doesn't fail when the file doesn't exist. 第二个是我的失败路径(Evaluation Operation ='Constraint'和Value ='Failure'),它转到一个虚拟任务(一个SQL任务),以使该包在文件不存在时不会失败。 。

In debugging, I confirmed that, when the file exists, it goes all the way through the data flow task (as expected). 在调试中,我确认当文件存在时,它会一直执行数据流任务(如预期的那样)。 However, when the file doesn't exist, the package fails; 但是,当文件不存在时,程序包将失败。 in particular, it fails at the first step (ie, the script task). 特别是,它在第​​一步(即脚本任务)失败。 I don't know what I'm doing wrong. 我不知道我在做什么错。 Below is my code for the script task: 以下是我的脚本任务代码:

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;
            }
        }

    }
}

As far as I can tell, it's behaving exactly as it is supposed to; 据我所知,它的行为完全符合预期。 it fails the script if the file doesn't exist. 如果文件不存在,它将使脚本失败。

I would use a variable instead to report the existence of a file. 我会改用一个变量来报告文件的存在。

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;
}

You want the script itself to succeed, unless it encounters an error. 您希望脚本本身成功,除非遇到错误。

Better yet would be: 更好的是:

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;
     }
}

Edit: 编辑:

Forgot to mention that you need to switch the precedence constraints from CONSTRAINT to Expression and CONSTRAINT, where the expression evaluates the @file_exists variable. 忘了提及您需要将优先级约束从CONSTRAINT切换到Expression和CONSTRAINT,其中表达式计算@file_exists变量。 You should have two success paths, one with the variabled evaluating to true and the other to false. 您应该有两条成功的道路,一条将变量的评估结果设为true,将另一条评估为false。

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

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