在SSIS包中输出变量名称和值

时间:2012-07-26 21:34:36

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

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

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();
    }
    }

    }

1 个答案:

答案 0 :(得分:1)

这对我有用

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

使用你的app和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   
---------------------------
相关问题