在网上搜索了有关如何列出所有 SSIS 组件的信息后,我发现编写 C# 程序可能是解决该问题的最佳解决方案。因此,下面我写了一个 VS 2008 兼容的程序来完成我的任务。下面编写的程序将列出所有与 SSIS 相关的组件,并将结果写入 excel 文件。该解决方案将为您节省大量时间单击VS 2008中显示的组件以逐个查看组件属性。由于我不是 C# 专家,因此该程序可能没有很好地编码。但至少它有效!
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using Excel = Microsoft.Office.Interop.Excel;
using System.Reflection;
using Microsoft.SqlServer.Dts.Runtime;
using Microsoft.SqlServer.Dts.Pipeline;
using Microsoft.SqlServer.Dts.Pipeline.Wrapper;
namespace Project1
{
public class SSISFinder
{
public static void Main()
{
// Set ssis app
Microsoft.SqlServer.Dts.Runtime.Application ssisApp = new Microsoft.SqlServer.Dts.Runtime.Application();
ssisApp.PackagePassword = "admin_monkey4ccc";
// Loading dtsx package
Package pkg = ssisApp.LoadPackage("D:\\SummaryETL.dtsx", null);
// Open Excel Sheet
Excel.Application oXL = new Excel.Application();
Excel.Workbook oWB;
Excel.Worksheet oSheet;
string fileName = "D:\\test.xls";
oWB = oXL.Workbooks.Add(Missing.Value);
oSheet = (Excel.Worksheet)oWB.ActiveSheet;
// List data flow package
List<DtsContainer> containers = FindExecutablesByType((IDTSSequence)pkg, "PIPELINE");
int counter = 1;
foreach (DtsContainer exec in containers)
{
TaskHost th = exec as TaskHost;
MainPipe pipe = (MainPipe)th.InnerObject;
foreach (IDTSComponentMetaData100 comp in pipe.ComponentMetaDataCollection)
{
if (comp.Description == "OLE DB Source")
{
oSheet.Cells[counter, 1] = comp.Description;
oSheet.Cells[counter, 2] = th.Properties["Name"].GetValue(th).ToString();
oSheet.Cells[counter, 3] = comp.Name;
oSheet.Cells[counter, 4] = comp.CustomPropertyCollection["SqlCommand"].Value;
Console.WriteLine(" Component Name = " + comp.Name);
counter++;
}
else if (comp.Description == "OLE DB Destination")
{
oSheet.Cells[counter, 1] = comp.Description;
oSheet.Cells[counter, 2] = th.Properties["Name"].GetValue(th).ToString();
oSheet.Cells[counter, 3] = comp.Name;
oSheet.Cells[counter, 4] = comp.CustomPropertyCollection["OpenRowset"].Value;
Console.WriteLine(" Component Name = " + comp.Name);
counter++;
}
}
}
oWB.SaveAs(fileName, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Excel.XlSaveAsAccessMode.xlShared, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
oWB = null;
oXL.Quit();
oXL = null;
}
static List<DtsContainer> FindExecutablesByType(IDTSSequence sequence, string typeName)
{
string typeNameUpper = typeName.ToUpper();
List<DtsContainer> matchingExecutable = new List<DtsContainer>();
foreach (Executable e in sequence.Executables)
{
if (e.GetType().ToString().ToUpper().Contains(typeNameUpper))
{
matchingExecutable.Add((DtsContainer)e);
}
if (e is TaskHost)
{
TaskHost taskHost = (TaskHost)e;
if ((typeNameUpper.Contains("DATA FLOW")
|| typeNameUpper.Contains("DATAFLOW")
|| typeNameUpper.Contains("MAINPIPE")
|| typeNameUpper.Contains("PIPELINE")
) && taskHost.InnerObject is IDTSPipeline100
)
{
matchingExecutable.Add((DtsContainer)e);
}
else if (taskHost.InnerObject.GetType().ToString().ToUpper().Contains(typeNameUpper))
{
matchingExecutable.Add((DtsContainer)e);
}
}
if (e is IDTSSequence)
{
matchingExecutable.AddRange(FindExecutablesByType((IDTSSequence)e, typeNameUpper));
}
}
return matchingExecutable;
}
}
}