簡體   English   中英

如何使用腳本任務 (C#) 運行 SSIS 包?

[英]How can I run a SSIS Package using Script Task (C#)?

我想要做的是使用這個命令行運行一個 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# 編碼,因此對任何“瑣碎”的錯誤表示歉意。 我在互聯網上搜索過,但幾乎所有情況下它都使用保存包的特定文件夾,然后在那里運行包,但我需要的是使用這個特定的命令行運行。

謝謝你的時間。

我猜這個包永遠不會執行,因為參數的語法有額外的空格,比如“/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);
            }
        }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM