簡體   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