使用 C# 执行 SSIS 包时出错

Error executing SSIS package using C#

我尝试使用 C# 执行一个非常简单的 SSIS 包。

此包在 Visual Studio 2015 年直接启动时工作正常。 SSIS 包的名称是 "Lesson 1.dtsx".

我尝试使用 C# 和以下代码开始此过程:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace run_ssis_project
{
    public class ExecuteSSIS
    {
        public void exePackage()
        {
            String pkgLocation = @"C:\SSIS Tutorial\Lesson 1.dtsx";
            Microsoft.SqlServer.Dts.Runtime.Package ssisPackage;
            Microsoft.SqlServer.Dts.Runtime.Application app;
            Microsoft.SqlServer.Dts.Runtime.DTSExecResult result;

            app = new Microsoft.SqlServer.Dts.Runtime.Application();
            ssisPackage = app.LoadPackage(pkgLocation,null);

            result = ssisPackage.Execute();

            if(result == Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success)
            {
                Console.WriteLine("Success");
            }
            else
            {
                Console.WriteLine("Failure");
            }

        }
    }
}

执行这段代码时,出现异常:

"Microsoft.SqlServer.Dts.Runtime.DtsRuntimeException", The package failed to load due to error 0xC0011008 "Error loading from XML. No further detailed error information.

The exception occurs in line: ssisPackage = app.LoadPackage(pkgLocation,null);

我在这个项目中添加了两个 DLL 作为参考:

Microsoft.SqlServer.DTSRuntimeWrap.dll

Microsoft.SqlServer.ManagedDTS.dll

有人可以帮我吗?

我没有遇到任何问题,除了我收到关于混合模式的错误,因为它是 运行 版本 2.0 与版本 4.0 的框架。因此,如果这不起作用,则可能是您的 ssis 包中有错误。否则尝试制作一个基本上什么都不做的新 ssis-packages,看看你是否成功。

我的代码是这样的:

using Microsoft.SqlServer.Dts.Runtime;


namespace ConsoleApplication8
{
    class Program
    {
        static void Main(string[] args)
        {

            string pkgLocation;
            Package pkg;
            Application app;
            DTSExecResult pkgResults;
            MyEventListener eventListener = new MyEventListener();

            pkgLocation =
              @"C:\Users\thoje\Documents\Visual Studio 2015\Projects\Integration Services Project8\Integration Services Project8\Package37.dtsx";
            app = new Application();
            pkg = app.LoadPackage(pkgLocation, eventListener);
            pkgResults = pkg.Execute(null,null,eventListener,null,null);

            Console.WriteLine(pkgResults.ToString());
            Console.ReadKey();
        }
    }


    class MyEventListener : DefaultEvents
    {
        public override bool OnError(DtsObject source, int errorCode, string subComponent,
               string description, string helpFile, int helpContext, string idofInterfaceWithError)
        {
            // Output Error Message
            Console.WriteLine("Error in {0}/{1} : {2}", source, subComponent, description);
            return false;
        }
    }
}

这就是我需要在 app.Config 中更正的内容:

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