Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/27.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# 使用重定向时SSIS包中出现空引用错误_C#_Sql Server_Ssis - Fatal编程技术网

C# 使用重定向时SSIS包中出现空引用错误

C# 使用重定向时SSIS包中出现空引用错误,c#,sql-server,ssis,C#,Sql Server,Ssis,我正在开发一种允许用户使用SSIS包创建和执行各种操作的产品,我希望能够支持SQL Server 2012、2014和2016版本的客户端工具。以前,我们支持SQL Server 2012(版本11.0.0.0)客户端工具,并要求用户安装SQL Server 2012客户端工具,而不管他们使用的SQL Server版本如何 我希望能够删除用户必须安装2012客户端工具的要求,但是遇到了空引用问题。我可以根据给定的SQL版本编译项目,当用户系统上存在该版本时,代码工作得非常好。如果然后尝试在配置文

我正在开发一种允许用户使用SSIS包创建和执行各种操作的产品,我希望能够支持SQL Server 2012、2014和2016版本的客户端工具。以前,我们支持SQL Server 2012(版本11.0.0.0)客户端工具,并要求用户安装SQL Server 2012客户端工具,而不管他们使用的SQL Server版本如何

我希望能够删除用户必须安装2012客户端工具的要求,但是遇到了空引用问题。我可以根据给定的SQL版本编译项目,当用户系统上存在该版本时,代码工作得非常好。如果然后尝试在配置文件中使用程序集绑定重定向在具有不同SQL版本的系统上运行该项目,则会出现空引用错误

将host.InnerObject强制转换为MainPipe时,会发生NULL引用。下一行在尝试访问ComponentMetaDataCollection时导致空引用。我已经检查了2012->2016的MainPipe类文档,不同版本的类的结构似乎没有任何明显的差异,这会导致强制转换失败

在下面的示例中,我针对2012年(v11)进行了编译,并重定向到2016年(v13),但我尝试了使用3个不同版本的所有组合:

using System;
using System.Collections;
using System.Diagnostics;
using Microsoft.SqlServer.Dts.Pipeline.Wrapper;
using Microsoft.SqlServer.Dts.Runtime;

namespace TestConsole
{
    class Program
    {
        static void Main(string[] args)
        {
            Stopwatch sw = new Stopwatch();
            Console.WriteLine("Test started at {0}", DateTime.Now);
            sw.Start();

            TestMinimumPackageTest();

            sw.Stop();
            Console.WriteLine("\nTest completed at {0}", DateTime.Now);
            Console.WriteLine("Total Elapsed Time: {0}", sw.Elapsed);
            Console.WriteLine("\nPress Enter to exit.");
            Console.Read();
        }

        static void TestMinimumPackageTest()
        {
            string selectCommand = "Select [IDColumn] From [dbo].[TestTable]";

            string targetTable = "TestTable";
            Package package = new Microsoft.SqlServer.Dts.Runtime.Package();
            var task = new DataFlowSelect();

            TaskHost host = package.Executables.Add("STOCK:PipelineTask") as TaskHost;
            task.MainPipe = host.InnerObject as MainPipe;

            IDTSPipeline100 pipeline = task.MainPipe;

            IDTSComponentMetaData100 adapter = task.MainPipe.ComponentMetaDataCollection.New();

        }
    }
    public class DataFlowSelect
    {
        public DataFlowSelect() { }
        public IDTSPipeline100 MainPipe { get; set; }
        public string SourceObject { get; set; }
        public string TargetObject { get; set; }
        public Package Package { get; set; }
        public bool CreateTarget { get; set; }
        public Hashtable ColumnMappings { get; set; }
    }
}
以及App.config:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <startup> 
      <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
  </startup>

  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.SqlServer.DTSRuntimeWrap" publicKeyToken="89845dcd8080cc91" culture="neutral"/>
        <bindingRedirect  oldVersion="11.0.0.0" newVersion="13.0.0.0"/>
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.SqlServer.DTSPipelineWrap" publicKeyToken="89845dcd8080cc91" culture="neutral"/>
        <bindingRedirect  oldVersion="11.0.0.0" newVersion="13.0.0.0"/>
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.SqlServer.PipelineHost" publicKeyToken="89845dcd8080cc91" culture="neutral"/>
        <bindingRedirect  oldVersion="11.0.0.0" newVersion="13.0.0.0"/>
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.SqlServer.ManagedDTS" publicKeyToken="89845dcd8080cc91" culture="neutral"/>
        <bindingRedirect  oldVersion="11.0.0.0" newVersion="13.0.0.0"/>
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>


您是否使用了调试器。。您可能没有正确创建某个对象的实例。我在成功运行(无重定向)和不成功运行(使用重定向)时都使用了调试器,唯一的区别似乎是强制转换失败。其他对象看起来相同。