2012-07-26 32 views
0

我想輸出所有用戶變量及其值的名稱,然後將值存儲在表中。我設法得到了解析變量和它們的值的時刻,但現在它給了我所有的變量(系統和用戶)。此外,我似乎不知道如何將值分配給一個表。任何幫助將不勝感激..以下是我一直想到現在。SSIS包中的輸出變量名稱和值

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

    } 
+0

此腳本僅標識範圍爲包級別的變量。這是你的意圖嗎? – billinkc 2012-07-27 13:58:00

+0

是的。所有用戶變量(僅)在包級別上。 – rvphx 2012-07-27 15:11:51

回答

1

這對我的作品

我創建了每種類型的變量的包,之後SSIS數據類型由此而得名, variable per data type

使用你的應用程序和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 
--------------------------- 
+0

謝謝你的迴應。如何擴展此腳本以將數據轉儲到目標表? – rvphx 2012-07-27 19:57:53

+0

我發佈了一個我試圖實現的新問題。如果您需要任何其他信息或者有任何其他方式來做我想做的事,請看看並告訴我。鏈接到問題:http://stackoverflow.com/questions/11695821/dump-ssis-user-variable-name-and-value-in-sql-server-table – rvphx 2012-07-27 21:32:29

相關問題