在C#脚本任务中影响SSIS对象变量

时间:2016-10-28 13:02:37

标签: c# ssis sql-server-2012

我创建了一个无法调试的简单脚本。这是我的问题: 我希望使用Visual Studio 2015将目录的内容存储到SSIS中的变量中。

我在SSIS包中创建了一个变量,并将其数据类型设置为Object。 我已经在我的包中添加了一个脚本任务,其中包含以下代码:

#region Namespaces
using System;
using System.Data;
using Microsoft.SqlServer.Dts.Runtime;
using System.Windows.Forms;
using System.IO;
using System.Collections.Generic;
#endregion

namespace ST_c6399821104c42c2859b7b2481055848 {

[Microsoft.SqlServer.Dts.Tasks.ScriptTask.SSISScriptTaskEntryPointAttribute]
public partial class ScriptMain : Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase {

    public void Main() {

        string CSVFilesCompletePath;

        if (Dts.Variables.Contains("User::CSVFilesPathAbsolute") == true
            && Dts.Variables.Contains("User::CSVFilesPathRelativeCountry") == true
            && Dts.Variables.Contains("User::CSVFilesCountryObject") == true) {

            CSVFilesCompletePath = Dts.Variables["User::CSVFilesPathAbsolute"].ToString() + Dts.Variables["User::CSVFilesPathRelativeCountry"].ToString();


            String[] fileListTable = Directory.GetFiles(CSVFilesCompletePath, "*.xlsx");
            List<string> fileList = new List<string>(fileListTable);
            Dts.Variables["User::CSVFilesCountryObject"].Value = fileList;
        }

        Dts.TaskResult = (int)ScriptResults.Success;

    }
    #region ScriptResults declaration
    /// <summary>
    /// This enum provides a convenient shorthand within the scope of this class for setting the
    /// result of the script.
    /// 
    /// This code was generated automatically.
    /// </summary>
    enum ScriptResults
    {
        Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success,
        Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure
        };
        #endregion
    }
}

如下所述: SSIS Script Task Get File Names and Store to an SSIS Object Variable

但是当我尝试通过SSIS作业启动它时,此代码返回以下错误:

SSIS C# Script Error

在脚本向导中将变量正确设置为ReadWriteVariables。

当我尝试影响SSIS变量并尝试将String[]放入其中时,我的代码会显示此错误。

2 个答案:

答案 0 :(得分:1)

尝试在

中添加try-catch
Directory.GetFiles

调用或在整个主体中,然后将错误消息和/或异常的堆栈跟踪设置为写SSIS变量以进行调试。

编辑:看起来像@ H.Fadlallah指出了问题所在。您应该使用DtsVariable的Value属性,而不是ToString()

答案 1 :(得分:0)

我的错误是:

CSVFilesCompletePath = Dts.Variables["User::CSVFilesPathAbsolute"].ToString() + Dts.Variables["User::CSVFilesPathRelativeCountry"].ToString();

我必须使用:Dts.Variables[].Value.ToString()而不是Dts.Variables[].ToString()

由于我使用的是变量的名称而不是变量的内容,因此GetFiles返回了一个空对象,并且它无法存储在我的变量中,从而产生了不同的错误。