【问题标题】:How can I run a SSIS Package using Script Task (C#)?如何使用脚本任务 (C#) 运行 SSIS 包?
【发布时间】:2022-06-23 03:51:59
【问题描述】:

我想要做的是使用这个命令行运行一个 SSIS 包:

/SQL "\"\[PACKAGE_NAME]\"" /SERVER [SERVER NAME] /X86 /CHECKPOINTING OFF /REPORTING E

到目前为止,这是我的代码:

using System;
using System.Data;
using Microsoft.SqlServer.Dts.Runtime;
using System.Windows.Forms;
using System.Collections.Generic;
using System.Data.OleDb;
using System.Threading;
using System.Diagnostics;

namespace blablabla.csproj
{
    [Microsoft.SqlServer.Dts.Tasks.ScriptTask.SSISScriptTaskEntryPointAttribute]
    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

        public void Main()
        {
            // Variables 
            string targetServerName = Dts.Variables["SSIS_SERVER"].Value.ToString();
            string packageExecutionName = "package_folder" + "\"" + "package_name";

            ProcessStartInfo startInfo = new ProcessStartInfo();
            startInfo.WindowStyle = ProcessWindowStyle.Hidden;
            startInfo.UseShellExecute = false;
            startInfo.RedirectStandardOutput = true;
            startInfo.CreateNoWindow = true;
            startInfo.FileName = @"dtexec.exe";
            string arguments = @"/SQL " + "\"" + "\\" + packageExecutionName + "\"" + " / SERVER " + Dts.Variables["SSIS_SERVER"].Value.ToString() + " / X86 / CHECKPOINTING OFF / REPORTING E /CONSOLELOG M";

            Dts.Variables["SSIS_SERVER"].Value.ToString();

            // arguments to dtexec
            startInfo.Arguments = arguments;

            // begin execution
            Process proc = Process.Start(startInfo);
    
        }
    
    }
}

脚本任务运行成功但包本身没有运行,所以代码一定是错误的...

这是我第一次用 C# 编写代码,因此对于任何“微不足道”的错误,我们深表歉意。我在互联网上搜索过,但几乎所有情况都是使用保存包的特定文件夹,然后在那里运行包,但我需要的是使用这个特定的命令行运行。

感谢您的宝贵时间。

【问题讨论】:

    标签: c# sql visual-studio ssis


    【解决方案1】:

    我猜这个包永远不会执行,因为参数的语法有额外的空格,比如“/X86”而不是“/X86”。

    如果您检查错误代码,如果它不为 0,则可以抛出异常。要查看该错误的详细信息,您需要查看 StandardOutput,因为 dtexec 不会将任何内容定向到 StandardError:

    public void Main()
            {
                //wrap the whole thing in a try..catch to report on errors
                try
                {
                    // Variables 
                    string targetServerName = Dts.Variables["SSIS_SERVER"].Value.ToString();
                    string packageExecutionName = "package_folder" + "\"" + "package_name";
    
                    ProcessStartInfo startInfo = new ProcessStartInfo();
                    startInfo.WindowStyle = ProcessWindowStyle.Hidden;
                    startInfo.UseShellExecute = false;
                    startInfo.RedirectStandardOutput = true;
                    startInfo.CreateNoWindow = true;
                    startInfo.FileName = @"dtexec.exe";
    
                    //tell the process to capture standard error
                    startInfo.RedirectStandardOutput = true;
                    string arguments = @"/SQL " + "\"" + "\\" + packageExecutionName + "\"" + " / SERVER " + Dts.Variables["SSIS_SERVER"].Value.ToString() + " / X86 / CHECKPOINTING OFF / REPORTING E /CONSOLELOG M";
    
                    Dts.Variables["SSIS_SERVER"].Value.ToString();
    
                    // arguments to dtexec
                    startInfo.Arguments = arguments;
    
                    // begin execution
                    Process proc = Process.Start(startInfo);
    
                    //capture standard output and wait for exit
                    string error = proc.StandardOutput.ReadToEnd();
                    proc.WaitForExit();
    
                    //Check the exit code and throw an error
                    if(proc.ExitCode != 0)
                    {
                        throw new Exception(error.ToString());
                    }
                    Dts.TaskResult = (int)ScriptResults.Success;
                }
    
                catch(Exception e)
                {
                    //write to the ssis log
                    Dts.Events.FireError(1, "", e.Message, "", 0);
                }
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-07-01
      • 2023-03-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多