不能用[]对类型的表达式应用索引

本文关键字:表达式 应用 索引 类型 不能 | 更新日期: 2023-09-27 18:10:10

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

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

fileExistFlag Int32 0

fileName String check.txt

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

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

Cannot apply indexing with [] to an expression of type '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;
    }
}

不能用[]对类型的表达式应用索引

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

解决方法是移动位于路径中的文件"Microsoft.SQLServer.ManagedDTS.dll":"C:'Program Files (x86)'Microsoft SQL Server'110'SDK'Assemblies"到备份文件夹,然后从路径"C:'Windows'assembly'GAC_MSIL'Microsoft. sqlserver . manageddts '10.0.0.0__89845dcd8080cc91'"

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

源:

https://connect.microsoft.com/sqlserver/feedback/details/744390/ssis -任何pre - 2012错误不能-应用-索引- - -一个表达式的类型-微软- dts -运行时变量状态"置疑"

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

奇怪的是,这个索引器似乎确实存在。但是,如果它不起作用,您可以使用扩展方法:

public static class MyExtensionMethods
{
    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;
    }
}

和使用:

... Dts.Variables.Get("User::folderPath").Value ...

在添加引用窗口中使用浏览并查找此dll: C:'Program Files (x86)'Microsoft SQLServer '100'SDK'Assemblies'Microsoft.SQLServer.ManagedDTS.dll