Advertisement
paulogp

SSIS: Leitura de ficheiro CSV a partir de um Script Task

Jul 28th, 2011
343
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.27 KB | None | 0 0
  1. // paulogp
  2.  
  3. /* read a csv file using Script Task object */
  4.  
  5. using System;
  6. using System.Data;
  7. using Microsoft.SqlServer.Dts.Runtime;
  8. using System.Windows.Forms;
  9. using System.IO;
  10.  
  11. namespace ST_0aa122bddcbe4dcbb10c58ee3defe98d.csproj
  12. {
  13.     [System.AddIn.AddIn("ScriptMain", Version = "1.0", Publisher = "", Description = "")]
  14.     public partial class ScriptMain : Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase
  15.     {
  16.  
  17.         #region VSTA generated code
  18.         enum ScriptResults
  19.         {
  20.             Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success,
  21.             Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure
  22.         };
  23.         #endregion
  24.  
  25.         /*
  26.         The execution engine calls this method when the task executes.
  27.         To access the object model, use the Dts property. Connections, variables, events,
  28.         and logging features are available as members of the Dts property as shown in the following examples.
  29.  
  30.         To reference a variable, call Dts.Variables["MyCaseSensitiveVariableName"].Value;
  31.         To post a log entry, call Dts.Log("This is my log text", 999, null);
  32.         To fire an event, call Dts.Events.FireInformation(99, "test", "hit the help message", "", 0, true);
  33.  
  34.         To use the connections collection use something like the following:
  35.         ConnectionManager cm = Dts.Connections.Add("OLEDB");
  36.         cm.ConnectionString = "Data Source=localhost;Initial Catalog=AdventureWorks;Provider=SQLNCLI10;Integrated Security=SSPI;Auto Translate=False;";
  37.  
  38.         Before returning from this method, set the value of Dts.TaskResult to indicate success or failure.
  39.        
  40.         To open Help, press F1.
  41.     */
  42.  
  43.         public void Main()
  44.         {
  45.             Boolean the_fire_again = true;
  46.             string the_path = "D:\\web_files\\wk_total_factura.csv";
  47.  
  48.             // log
  49.             Dts.Events.FireInformation(0, "Info: ", "Begin process!", string.Empty, 0, ref the_fire_again);
  50.  
  51.             if (File.Exists(the_path))
  52.             {
  53.                 string the_line_content = "";
  54.                 string[] _values = null;
  55.                 // int the_line_no = 0;
  56.  
  57.                 // streaming
  58.                 StreamReader the_stream = new StreamReader(the_path); // @the_path
  59.                 // for set encoding
  60.                 // StreamReader sr = new StreamReader(@"file.csv", Encoding.GetEncoding(1250));
  61.  
  62.                 while (!the_stream.EndOfStream)
  63.                 {
  64.                     // the_line_no++;
  65.                     the_line_content = the_stream.ReadLine();
  66.                     _values = the_line_content.Split(';');
  67.  
  68.                     // retrieve 7th column to output standard log
  69.                     Dts.Events.FireInformation(0, "Row: ", _values[7] + " " + _values.Length, string.Empty, 0, ref the_fire_again);
  70.                 }
  71.                 the_stream.Close();
  72.             }
  73.             else
  74.             {
  75.                 // MessageBox.Show("File not found!");
  76.                 Dts.Events.FireInformation(0, "Error: ", "File not found!", string.Empty, 0, ref the_fire_again);
  77.             }
  78.  
  79.             // log
  80.             Dts.Events.FireInformation(0, "Info: ", "End process!", string.Empty, 0, ref the_fire_again);
  81.  
  82.             // standard
  83.             Dts.TaskResult = (int)ScriptResults.Success;
  84.         }
  85.     }
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement