【问题标题】:Cannot apply indexing with[] to an expression of type无法使用 [] 将索引应用于类型的表达式
【发布时间】:2014-06-25 13:15:49
【问题描述】:

我正在创建一个 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;
    }
}

【问题讨论】:

  • 奇怪...它似乎在那里:msdn.microsoft.com/en-us/library/…
  • 无需编写该代码。使用 ForEach (file) 枚举器将提取与现有模式匹配的所有文件。如果没有找到文件,假设容器中有一个数据流任务,它不会运行它。我怀疑你的代码有些不对劲,我到办公室后会验证

标签: c# ssis


【解决方案1】:

这是并行安装更高版本的 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

【讨论】:

  • 感谢您的回复。我正是这样做的,它也帮助解决了我的问题。
  • +1 刚遇到同样的问题,这对我有用。接受的答案没有。它在Microsoft.SqlServer.Dts.Runtime.Variables中抱怨Runtime
  • 也为我工作。谢谢!
  • 在 Windows 10 中为我工作,并排安装 SQL 2014。
  • 在 Windows 7 上为我工作,同时安装了 SQL 2008 和 SQL 2012。
【解决方案2】:

奇怪的是,这个索引器does seem to exist。但是,如果它不起作用,您也许可以使用扩展方法:

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

改为。

【讨论】:

  • 感谢您的回复。我是 SSIS 脚本的新手,所以遇到了一些问题。我已更改代码以反映您的答案,现在出现两个错误。第一个是Non-invocable member 'Microsoft.SqlServer.Dts.Tasks.ScriptTask.ScriptObjectModel.Variables' cannot be used like a method,第二个是The type of namespace name 'Variable' could not be found
  • @SearchForKnowledge 添加了明确的完整类型名称
  • 我修改了您的代码(希望没问题)以消除所有错误,但出现另一个错误,Extension methods must be defined in a non-generic static class... 有什么想法吗?
  • @SearchForKnowledge (添加“s”)不是正确的解决方法;已编辑;至于另一个:照它说的做(我也会编辑它)
  • 我用我现在的代码的方式更新了我的问题。请告诉我要解决的问题。
【解决方案3】:

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

【讨论】:

    猜你喜欢
    • 2014-01-09
    • 2018-04-03
    • 2022-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多