3

I have .dat FIle. I need to do some encoding on this file and need o save into a .txt file I am using script component in SSIS.

I have used SSIS Script component to impement this .

//have declared 2 read write variable FIleLoc and FileWrite
string fileIn = Variables.FileLoc;
string fileOut = Variables.FileWrite;
var x = System.IO.File.ReadAllText(fileIn);
byte[] encodedBytes = System.Text.Encoding.UTF8.GetBytes(x);
byte[] unicodeBytes=
Encoding.Convert(Encoding.UTF8,Encoding.Unicode,encodedBytes);
System.IO.File.WriteAllBytes(fileOut, unicodeBytes);
Output0Buffer.Col = Variables.FileWrite;

When i try to debug the code its showing package executed successfully and the .txt file is not generated.

Yahfoufi
  • 2,220
  • 1
  • 22
  • 41
user3035090
  • 57
  • 10

1 Answers1

1

You can read .dat file using a Flat File Connection Manager and select the UTF-8 encoding from the Code page drop down list as mentioned in the picture below. Then instead of writing a script to convert .dat file to .txt you can simply use a Data Flow Task that contains a Flat File Source (.dat) and a Flat File Destination (.txt) (each one must have a seperate connection manager).

enter image description here

If you need to use a Script Task then I would suggest to read the file path from the Flat File connection manager similar to:

public void Main()
  {
      ConnectionManager conn = Dts.Connections["file.dat"];
      var path = conn.ConnectionString;
      //your encoding code 
      // if you want to access package variables use Dts.Variables["User::NeededVar"].Value;
      Dts.TaskResult = ScriptResults.Success;
  }
Hadi
  • 36,233
  • 13
  • 65
  • 124
  • Thanks for your reply.The .dat file which i am having doesn't have any column headers.How will i load this into a destination text file using data flow task.Sorry to ask this i am new to SSIS – user3035090 Apr 01 '19 at 04:34
  • When you create a new Flat File Connection Manager for source you will have to uncheck "Column Names in the first data row" -> this will insert into destination with no headers. Otherwise, if you need to insert into a file with headers, check that checkbox and under Advanced section add column headers. – Ana Iuliana Tuhasu Apr 01 '19 at 11:09
  • @user3035090 in the flat file connection manager go to advanced tab and add columns names manually. – Yahfoufi Apr 01 '19 at 12:26