【问题标题】:Output variable name and value in SSIS packageSSIS包中的输出变量名和值
【发布时间】:2012-07-25 13:47:49
【问题描述】:

我正在尝试输出所有用户变量的名称及其值,然后将这些值存储在一个表中。我设法解析变量及其值,但现在它给了我所有的变量(系统和用户)。另外,我似乎无法弄清楚如何将值分配给表。任何帮助将不胜感激..以下是我到目前为止的想法。

using System;
using System.Data;
using Microsoft.SqlServer.Dts.Runtime;
using System.Windows.Forms;
//using System;
////using Microsoft.SqlServer.Dts.Runtime;
namespace ST_81ec2398155247148a7dad513f3be99d.csproj
{
    [System.AddIn.AddIn("ScriptMain", Version = "1.0", Publisher = "", Description = "")]
    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()
    {


        Microsoft.SqlServer.Dts.Runtime.Application app = new Microsoft.SqlServer.Dts.Runtime.Application();
        // Load a sample package that contains a variable that sets the file name.
        Package pkg = app.LoadPackage(
          @"C:\PackagePath" +
          @"Template0719.dtsx",
          null);
        Variables pkgVars = pkg.Variables;
        foreach (Variable pkgVar in pkgVars)
        {

            MessageBox.Show(pkgVar.Name);
            MessageBox.Show(pkgVar.Value.ToString());
        }
        Console.Read();
    }
    }

    }

【问题讨论】:

  • 此脚本仅识别范围为包级别的变量。这是你的意图吗?
  • 是的。包级别的所有 USER 变量(仅)。

标签: sql-server-2008 ssis custom-component


【解决方案1】:

这对我有用

我创建了一个包含每种类型变量的包,因此以 SSIS 数据类型命名,

使用您的应用程序和 pkg 声明/实例,我使用了以下内容

        boolean interactiveMode = true;
        foreach (Variable item in pkg.Variables)
        {
            try
            {
                if (!item.Namespace.Equals("System"))
                {
                    // item.Value.ToString()
                    string value = (item.Value == null) ? string.Empty : item.Value.ToString();
                    string name = item.Name;
                    string scope = item.Namespace;
                    string type = item.DataType.ToString();
                    string output = string.Format("Name {0, -20} | Scope {1, -10} | Data Type {2, -10} | Value {3, -20}", name, scope, type, value);
                    if (interactiveMode)
                    {
                        MessageBox.Show(output);
                    }
                    else
                    {
                        bool fireAgain = true;
                        Dts.Events.FireInformation(0, "Variable enumeration", output, string.Empty, 0, ref fireAgain);
                    }

                }

            }
            catch (Exception ex)
            {

                Dts.Events.FireError(0, "enumerate", ex.ToString(), string.Empty, 0);
            }
        }

生成预期值

---------------------------

---------------------------
Name VariableDbNull       | Scope User       | Data Type Empty      | Value                     
---------------------------
OK   
---------------------------

---------------------------

---------------------------
Name VariableDouble       | Scope User       | Data Type Double     | Value 0                   
---------------------------
OK   
---------------------------

【讨论】:

  • 感谢您的回复。如何扩展此脚本以将数据转储到目标表?
  • 我发布了一个关于我想要实现的目标的新问题。请看一下,如果您需要任何其他信息,或者是否有任何其他方式可以做我想做的事情,请告诉我。问题链接:stackoverflow.com/questions/11695821/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-02
  • 1970-01-01
  • 1970-01-01
  • 2016-08-17
  • 1970-01-01
相关资源
最近更新 更多