Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ssis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 无法将带[]的索引应用于类型为的表达式_C#_Ssis - Fatal编程技术网

C# 无法将带[]的索引应用于类型为的表达式

C# 无法将带[]的索引应用于类型为的表达式,c#,ssis,C#,Ssis,我正在创建一个SSIS包,并希望包含一个脚本,该脚本在检索文件并将数据保存到表之前检查文件是否存在 我设置了三个单独的变量: fileExistFlag Int32 0 fileName String check.txt folderPath字符串C:\ 我的C#代码如下所示,我正在检查: public void Main() { // TODO: Add your code here String fp = Dts.Variables["User::folderPath"].V

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

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

fileExistFlag Int32 0

fileName String check.txt

folderPath字符串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;
    }
}
奇怪的是,这个索引器。但是,如果它不起作用,您可以使用扩展方法:

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之后,这是SQL Server 2005/2008投标中的一个已知错误。例如,如果您正在开发SSIS 2008软件包,然后安装SSIS 2012

解决方法是移动路径中的文件“Microsoft.SQLServer.ManagedDTS.dll”: 将“C:\Program Files(x86)\Microsoft SQL Server\110\SDK\assembly”保存到备份文件夹中,然后投标文件从路径“C:\Windows\assembly\GAC_MSIL\Microsoft.SqlServer.ManagedTs\10.0.0\uu 89845dcd8080cc91”中获取参考

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

资料来源:


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

奇数。。。它似乎就在那里:没有必要编写代码。使用ForEach(file)枚举器将提取与现有模式匹配的所有文件。如果没有找到文件,假设容器中有一个数据流任务,它就不会运行它。我怀疑你的代码有点不对劲,我一到办公室就会核实答案。我是全新的SSIS脚本,所以有一些问题。我已更改代码以反映您的答案,现在我收到两个错误。第一个是,
非发票成员“Microsoft.SqlServer.Dts.Tasks.ScriptTask.ScriptObjectModel.Variables”不能像方法一样使用
,第二个是,
找不到命名空间名称“Variable”的类型
@SearchForkKnowledge显式完整类型名称添加修改了您的代码(希望没问题)为了消除所有错误,但我遇到了另一个错误,
扩展方法必须在非泛型静态类中定义。。。有什么想法吗?@SearchForKnowledge(添加一个“s”)不是很好的解决方案;编辑;至于另一个:照它说的去做(我也会编辑它),我用我现在的代码更新了我的问题。请让我知道要修复什么。谢谢您的回复。我确实做到了,这也帮助解决了我的问题。+1也有同样的问题,这对我很有效。被接受的答案没有。它抱怨
Microsoft.SqlServer.Dts.Runtime.Variables
中的
Runtime
也对我起了作用。谢谢在Windows 10中为我并行安装SQL 2014。在Windows 7中为我并行安装SQL 2008和SQL 2012。