2011-06-13 118 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(); 
     } 
    } 


}