获取平面文件连接的字段列表?

时间:2015-06-13 06:55:39

标签: c# sql-server powershell ssis

如何编写函数(外部函数,c#,f#或powershell脚本等)

List<string> GetFields(string ssisPackageName, string fileSourceName);

获取SSIS包的字段列表?由于包是一个Xml文件,可以使用xquery来获取列表吗?

甚至更好,获取更多信息,

class Field 
{ 
  public string Name { get; set; } 
  public string Type { get; set; } 
}
List<Field> GetFields(string ssisPackageName, string fileSourceName);

1 个答案:

答案 0 :(得分:0)

@billinkc是对的,你应该记住数据输入问题。也就是说,您最多可以检索平面文件连接管理器本身的代码页和Unicode值。以下代码应该开始,您可能需要对代码页和数据类型属性进行一些查找。

string path = @"MyPathTo\Package.dtsx";
XNamespace dts = "www.microsoft.com/SqlServer/Dts";
XDocument doc = XDocument.Load(path);

// get all connections
var connections = from ele in doc.Descendants(dts + "ConnectionManager")
                  where ele.Attributes(dts + "ObjectName").Count() != 0
                  select ele;

foreach (var connection in connections)
{
    // look for your flat file connection
    if (connection.Attribute(dts + "ObjectName").Value == "Flat File Connection Manager")
    {
        var connectionDetails = connection.Element(dts + "ObjectData").Element(dts + "ConnectionManager");
        Console.WriteLine("CodePage: " + connectionDetails.Attribute(dts + "CodePage").Value);
        Console.WriteLine("Unicode: " + connectionDetails.Attribute(dts + "Unicode").Value);
        var columnList = connection.Descendants(dts + "FlatFileColumn");
        foreach (var column in columnList)
        {
            Console.WriteLine("Column name: " + column.Attribute(dts + "ObjectName").Value);
            Console.WriteLine("Column type: " + column.Attribute(dts + "DataType").Value);
        }
    }
}