2011-06-13 119 views
0

我试图找到一种方法来统计来自平面文件的列。实际上,我的所有专栏都以一个单元格连接在一起,并用“|” ,
经过各种尝试,似乎只有一个脚本任务可以处理这个。 有人可以帮助我吗?我很可惜没有C#或VB中的脚本经验。平面文件中的SSIS列数

非常感谢 灵光

为了更好地理解,下面是我所想要达到的输出。例如包含来自FF的所有报头的单个单元。问题是,为了得到这个结果,我在前一步(派生列)中手动附加了所有列名,以便将它们与'|'连接起来。分隔器。 现在,如果我的FF源代码布局发生变化,它将不再工作,因为这个手动过程。所以我想我将不得不使用脚本来代替基本上返回我的列数(变量),并将允许删除派生列转换中的硬编码部分,例如

回答

0

请参考我的答案以下Stack Overflow问题。这些答案可能会让您了解如何加载包含不同列数的平面文件。在以下问题

  1. 实施例读取包含由特殊字符Ç (c-cedilla)分离的数据的文件。在你的情况下,分隔符是Vertical Bar (|) UTF-8 flat file import to SQL Server 2008 not recognizing {LF} row delimiter

  2. 举例下列问题读取包含有不同的列数不同部分的EDI文件。该包读取文件将其相应的父子关系加载到SQL表中。 how to load a flat file with header and detail parent child relationship into SQL server

基于在这些答案中使用的逻辑,也可以由列分隔符(Vertical Bar |)拆分文件中的行数列数。

希望有所帮助。

+0

好的非常感谢你的回答,但什么我目前正试图做的是算我的头被用分隔符 – largo68 2011-06-13 12:18:38

+0

我知道我的解释是,也许不是完美的分开了,但我怎么可以把屏幕在这里拍摄?,这将有助于definetely更好地理解我的问题 – largo68 2011-06-13 12:53:50

+0

我增加了更多的文字,但我不知道我的照片已经上传 – largo68 2011-06-13 13:23:10

1

这是一个非常古老的线程;然而,我只是偶然发现了一个类似的问题。里面有许多不同记录“格式”的平面文件。许多不同的格式,没有任何特定的顺序,这意味着你可能在一行中有57个字段,然后是下一个1000中的59个,然后是下一个10000中的56个,回到57 ......好吧,认为你有这个想法。

由于缺乏更好的想法,我决定根据每行中的逗号数来打破该文件,然后使用每种类型的SSIS包导入不同的记录类型(现在聚在一起)。

所以这个问题的答案就在那里,用更多的代码来生成文件。

希望这可以帮助有同样问题的人。

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.IO; 

namespace OddFlatFile_Transformation 
{ 
    class RedistributeLines 
    { 
    /* 
    * This routine opens a text file and reads it line by line 
    * for each line the number of "," (commas) is counted 
    * and then the line is written into a another text file 
    * based on that number of commas found 
    * For example if there are 15 commas in a given line 
    * the line is written to the WhateverFileName_15.Ext 
    * WhaeverFileName and Ext are the same file name and 
    * extension from the original file that is being read 
    * The application tests WhateverFileName_NN.Ext for existance 
    * and creates the file in case it does not exist yet 
    * To Better control splited records a sequential identifier, 
    * based on the number of lines read, is added to the beginning 
    * of each line written independently of the file and record number 
    */ 
     static void Main(string[] args) 
     { 
      // get full qualified file name from console 
      String strFileToRead; 
      strFileToRead = Console.ReadLine(); 

      // create reader & open file 
      StreamReader srTextFileReader = new StreamReader(strFileToRead); 

      string strLineRead = ""; 
      string strFileToWrite = ""; 
      string strLineIdentifier = ""; 
      string strLineToWrite = ""; 
      int intCountLines = 0; 
      int intCountCommas = 0; 
      int intDotPosition = 0; 
      const string strZeroPadding = "00000000"; 

      // Processing begins 
      Console.WriteLine("Processing begins: " + DateTime.Now); 

      /* Main Loop */ 
      while (strLineRead != null) 
      { 
       // read a line of text count commas and create Linde Identifier 
       strLineRead = srTextFileReader.ReadLine(); 
       if (strLineRead != null) 
       { 
        intCountLines += 1; 
        strLineIdentifier = strZeroPadding.Substring(0, strZeroPadding.Length - intCountLines.ToString().Length) + intCountLines; 
        intCountCommas = 0; 
        foreach (char chrEachPosition in strLineRead) 
        { 
         if (chrEachPosition == ',') intCountCommas++; 
        } 

        // Based on the number of commas determined above 
        // the name of the file to be writen to is established 
        intDotPosition = strFileToRead.IndexOf("."); 
        strFileToWrite = strFileToRead.Substring (0,intDotPosition) + "_"; 
        if (intCountCommas < 10) 
        { 
         strFileToWrite += "0" + intCountCommas; 
        } 
        else 
        { 
         strFileToWrite += intCountCommas; 
        } 
        strFileToWrite += strFileToRead.Substring(intDotPosition, (strFileToRead.Length - intDotPosition)); 

        // Using the file name established above the line captured 
        // during the text read phase is written to that file 

        StreamWriter swTextFileWriter = new StreamWriter(strFileToWrite, true); 
        strLineToWrite = "[" + strLineIdentifier + "] " + strLineRead; 
        swTextFileWriter.WriteLine (strLineToWrite); 
        swTextFileWriter.Close(); 
        Console.WriteLine(strLineIdentifier); 
       } 
      } 

      // close the stream 
      srTextFileReader.Close(); 
      Console.WriteLine(DateTime.Now); 
      Console.ReadLine(); 
     } 
    } 


}