SSIS 找不到变量

SSIS The variable cannot be found

我想从 visual studio 解决方案

中执行包

代码:

private Microsoft.SqlServer.Dts.Runtime.Package pkgPaquete;
private Application appAplicacion;

public DTSExecResult EjecucionPaquete(string str_Paquete, List < CatVariablesEtl > Vars = null) {

  DTSExecResult respuesta;
  try {
    appAplicacion = new Application();

    appAplicacion.PackagePassword = "pass";

    pkgPaquete = appAplicacion.LoadPackage(str_Paquete, null);
    foreach(CatVariablesEtl item in Vars) {
      pkgPaquete.Variables[item.str_NombreVariable.ToString()].Value = item.str_ValorVariable.ToString();
    }

    respuesta = pkgPaquete.Execute();

    return respuesta;
  } catch (Exception ex) {

    throw new NotImplementedException();
  }

}
}
}

它在抛出 catch 的这一行中停止进入 foreach 语句:

pkgPaquete.Variables[item.str_NombreVariable.ToString()].Value = item.str_ValorVariable.ToString();

str_NombredeVariable 值:

item.str_ValorVariable值:

参数入包:

错误:

The variable cannot be found. This occurs when an attempt is made to retrieve a variable from the Variables collection on a container during execution of the package, and the variable is not there. The variable name may have changed or the variable is not being created.

我阅读了其他相关问题,但没有找到我的问题的正确答案。非常感谢帮助。此致

更新: 作为 Hadi 评论我尝试使用

var setValueParameters = new Collection<PackageInfo.ExecutionValueParameterSet>();

但是我刚得到vs红色标记,我需要导入一些参考吗?

作为Hadi的第二个回答我无法参考pkgPaquete.Variables:

第一次尝试

尝试从变量名

中删除User::
foreach(CatVariablesEtl item in Vars) {
  pkgPaquete.Variables[item.str_NombreVariable.ToString().Replace("User::","")].Value = item.str_ValorVariable.ToString();
}

第二种赋值方法

也试试用这个方法给变量赋值:

Microsoft.SqlServer.Dts.RunTime.Variables myVars = pkgPaquete.Variables;

foreach(CatVariablesEtl item in Vars) {
  myVars[item.str_NombreVariable.ToString().Replace("User::","")].Value = item.str_ValorVariable.ToString();
}

Microsoft.SqlServer.Dts.Runtime.DTSExecResult results = pkgPaquete.Execute(null, myVars, null, null, null);

如果使用参数,看起来它们不能以编程方式修改。尝试使用变量代替它们