在SQL Server表中转储SSIS USER变量名称和值

目的:获取SSIS包中的所有用户变量,并在SQL Server 2008表中写入变量名称及其值。

我尝试什么:我有一个小的“脚本任务 ”工作来显示变量名称及其值。 该脚本如下。

using System; using System.Data; using Microsoft.SqlServer.Dts.Runtime; using System.Windows.Forms; 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(); Package pkg = app.LoadPackage( @"C:\package.dtsx", null); Variables pkgVars = pkg.Variables; foreach (Variable pkgVar in pkgVars) { if (pkgVar.Namespace.ToString() == "User") { MessageBox.Show(pkgVar.Name); MessageBox.Show(pkgVar.Value.ToString()); } } Console.Read(); } } } 

需要做什么:我需要将此值转储到数据库表中。 我正在尝试为此编写脚本组件 ,但由于缺乏.net脚本的知识,我没有接近终点线。 这就是我在组件方面所做的。

 using System; using System.Data; using Microsoft.SqlServer.Dts.Pipeline.Wrapper; using Microsoft.SqlServer.Dts.Runtime.Wrapper; using Microsoft.SqlServer.Dts; using System.Windows.Forms; [Microsoft.SqlServer.Dts.Pipeline.SSISScriptComponentEntryPointAttribute] public class ScriptMain : UserComponent { public override void CreateNewOutputRows() { #region Class Variables IDTSVariables100 variables; #endregion variables = null; this.VariableDispenser.GetVariables(out variables); foreach(Variable myVar in variables) { MessageBox.Show(myVar.Value.ToString()); if (myVar.Namespace == "User") { Output0Buffer.ColumnName = myVar.Value.ToString(); } } } } 

我现在可以想到解决这个问题的两种方法。

  1. 使用相同的脚本任务将值插入数据库
  2. 使用Foreach循环枚举ssis包变量(正如您所问)

用于插入变量名称和值的表脚本是

 CREATE TABLE [dbo].[SSISVariables] ( [Name] [varchar](50) NULL, [Value] [varchar](50) NULL ) 

1.使用Script task并编写以下代码

 [System.AddIn.AddIn("ScriptMain", Version = "1.0", Publisher = "", Description = "")] public partial class ScriptMain : Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase { private static string m_connectionString = @"Data Source=Server Name; Initial Catalog=Practice;Integrated Security=True"; public void Main() { List _coll = new List(); Microsoft.SqlServer.Dts.Runtime.Application app = new Microsoft.SqlServer.Dts.Runtime.Application(); Package pkg = app.LoadPackage(PackageLocation,Null); Variables pkgVars = pkg.Variables; foreach (Variable pkgVar in pkgVars) { if (pkgVar.Namespace.ToString() == "User") { _coll.Add(new SSISVariable () { Name =pkgVar.Name.ToString(), Val =pkgVar .Value.ToString () }); } } InsertIntoTable(_coll); Dts.TaskResult = (int)ScriptResults.Success; } public void InsertIntoTable(List _collDetails) { using (SqlConnection conn = new SqlConnection(m_connectionString)) { conn.Open(); foreach (var item in _collDetails ) { SqlCommand command = new SqlCommand("Insert into SSISVariables values (@name,@value)", conn); command.Parameters.Add("@name", SqlDbType.VarChar).Value = item.Name ; command.Parameters.Add("@value", SqlDbType.VarChar).Value = item.Val ; command.ExecuteNonQuery(); } } } } public class SSISVariable { public string Name { get; set; } public string Val { get; set; } } 

说明:在这里,我正在创建一个具有Name和Val属性的类。 使用代码检索包变量及其值,并将它们存储在集合List 。然后将此集合传递给方法( InsertIntoTable ),该方法只需通过枚举集合将值插入数据库。

注意 :

 There is a performance issue with the above code ,as for every variable im hitting the database and inserting the value.You can use [TVP][1]( SQL Server 2008) or stored procedure which takes xml( sql server 2005) as input. 

2.使用ForEach循环

设计

在此处输入图像描述

步骤1:创建一个System.Object类型的Variable VariableCollection和另一个String类型的变量Item来存储来自Foreach循环的结果

第2步:对于第一个脚本任务。

VariableCollection用于存储变量名称及其值 在此处输入图像描述

第3步:在脚本任务Main Method编写以下代码

  public void Main() { ArrayList _coll = new ArrayList(); Microsoft.SqlServer.Dts.Runtime.Application app = new Microsoft.SqlServer.Dts.Runtime.Application(); Package pkg = app.LoadPackage(Your Package Location,null); Variables pkgVars = pkg.Variables; foreach (Variable pkgVar in pkgVars) { if (pkgVar.Namespace.ToString() == "User") { _coll.Add(string.Concat ( pkgVar.Name,',',pkgVar.Value )); } } Dts.Variables["User::VariableCollection"].Value = _coll; // TODO: Add your code here Dts.TaskResult = (int)ScriptResults.Success; } 

步骤4:拖动Foreach循环并在表达式中使用Foreach from Variable Enumerator

在此处输入图像描述

步骤5:Foreach循环枚举值并将其存储在变量User::Item

在此处输入图像描述

步骤6:在foreach循环内拖动脚本任务并选择变量

 readonly variables User::Item 

步骤7:在main方法中写下面的代码

  public void Main() { string name = string.Empty; string[] variableCollection; variableCollection = Dts.Variables["User::Item"].Value.ToString().Split(','); using (SqlConnection conn = new SqlConnection(m_connectionString)) { conn.Open(); SqlCommand command = new SqlCommand("Insert into SSISVariables values (@name,@value)", conn); command.Parameters.Add("@name", SqlDbType.VarChar).Value = variableCollection[0]; command.Parameters.Add("@value", SqlDbType.VarChar).Value = variableCollection[1]; command.ExecuteNonQuery(); } // TODO: Add your code here Dts.TaskResult = (int)ScriptResults.Success; } 

说明:在Foreach循环中,我只能枚举一个变量。所以逻辑是,我需要以某种方式将变量名称及其值传递给一个变量。对此,我有连接的名称和值

  string.Concat ( pkgVar.Name,',',pkgVar.Value ) 

foreach循环中的脚本任务只是拆分变量并将其存储到字符串数组中,然后你可以使用数组索引访问名称和值并将其存储在数据库中