c# - 无法将带有 [] 的索引应用于类型的表达式

标签 c# ssis

我正在创建一个 SSIS 包并希望包含一个脚本,该脚本在检索文件并将该数据保存到表之前检查文件是否存在。

我设置了三个独立的变量:

fileExistFlag Int32 0

fileName String check.txt

文件夹路径字符串 C:\

我的 C# 代码如下所示,我正在检查的地方:

public void Main()
{
    // TODO: Add your code here
    String fp = Dts.Variables["User::folderPath"].Value.ToString() + Dts.Variables["User::fileName"].Value.ToString();
    if (File.Exists(fp))
    {
        Dts.Variables["User::fileExistFlag"].Value = 1;
    }
    MessageBox.Show(fp);
    MessageBox.Show(Dts.Variables["User::fileExistFlag"].Value.ToString());
    Dts.TaskResult = (int)ScriptResults.Success;
}

当我尝试编译我的脚本时,我收到以下错误:

无法将带有 [] 的索引应用于所有四个实例的类型为“Microsoft.SqlServer.Dts.Runtime.Variables”的表达式。

我该如何解决这个问题?

更新代码:

/*
   Microsoft SQL Server Integration Services Script Task
   Write scripts using Microsoft Visual C# 2008.
   The ScriptMain is the entry point class of the script.
*/

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

namespace ST_04f6fa3ba49a4ddeac3d3d7fc29f04f2.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

        /*
        The execution engine calls this method when the task executes.
        To access the object model, use the Dts property. Connections, variables, events,
        and logging features are available as members of the Dts property as shown in the following examples.

        To reference a variable, call Dts.Variables["MyCaseSensitiveVariableName"].Value;
        To post a log entry, call Dts.Log("This is my log text", 999, null);
        To fire an event, call Dts.Events.FireInformation(99, "test", "hit the help message", "", 0, true);

        To use the connections collection use something like the following:
        ConnectionManager cm = Dts.Connections.Add("OLEDB");
        cm.ConnectionString = "Data Source=localhost;Initial Catalog=AdventureWorks;Provider=SQLNCLI10;Integrated Security=SSPI;Auto Translate=False;";

        Before returning from this method, set the value of Dts.TaskResult to indicate success or failure.

        To open Help, press F1.
    */

        public void Main()
        {
            // TODO: Add your code here
            String fp = Dts.Variables.Get("User::folderPath").Value.ToString() + Dts.Variables.Get("User::fileName").Value.ToString();
            if (File.Exists(fp))
            {
                Dts.Variables.Get("User::fileExistFlag").Value = 1;
            }
            MessageBox.Show(fp);
            MessageBox.Show(Dts.Variables.Get("User::fileExistFlag").Value.ToString());
            Dts.TaskResult = (int)ScriptResults.Success;
        }
    }
    public static Microsoft.SqlServer.Dts.Runtime.Variable Get(
        this Microsoft.SqlServer.Dts.Runtime.Variables variables, string name)
    {
        foreach(Microsoft.SqlServer.Dts.Runtime.Variable item in variables)
        {
            if(item.Name == name) return item;
        }
        return null;
    }
}

最佳答案

这是并排安装更高版本的 SSIS 后 SQL Server BIDS 2005/2008 中的一个已知 BUG。例如,如果您正在开发 SSIS 2008 包,然后安装 SSIS 2012。

解决方法 是移动位于路径中的 “Microsoft.SQLServer.ManagedDTS.dll” 文件: “C:\Program Files (x86)\Microsoft SQL Server\110\SDK\Assemblies” 到备份文件夹,然后投标从路径“C:\Windows\程序集\GAC_MSIL\Microsoft.SqlServer.ManagedDTS\10.0.0.0__89845dcd8080cc91\"

但它似乎并不适用于所有报告的案例。

来源:

https://connect.microsoft.com/SQLServer/feedback/details/744390/ssis-any-pre-2012-error-cannot-apply-indexing-with-to-an-expression-of-type-microsoft-sqlserver-dts-runtime-variables

http://support.microsoft.com/kb/938608/en-us

关于c# - 无法将带有 [] 的索引应用于类型的表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24409771/

相关文章:

sql - 调度多个相互依赖的 SQL Server 代理作业的做法是什么?

c# - 从非托管 unicode 字符串创建 SecureString

java - 类似于 Java 中的 ASP.NET WebAPI

c# - 如何查找具有与另一个集合中的任何值匹配的属性的所有对象?

SSIS 2012将变量从子包传递到父包

带脚本组件的 SSIS 平衡数据分发器

c# - 如何调整 ObservableCollection 的大小?

c# - 从两个字符串值之间获取一个字符串

performance - Kingswaysoft 处理大量数据的 SSIS 速度缓慢且性能不佳

c# - 作为 SSIS 脚本任务的一部分,在 C# 中打开、保存然后关闭 xls 文件