Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* A seguinte acção (DTS), faz download de um ficheiro CSV e importa-o para uma BD.
- 1. No Microsoft Visual Studio 2008, criar um novo SSIS
- 2. Na Connections Manager criar uma ligação New Connection
- 3. Seleccionar HTTP - Connection manager for HTTP connections e criar uma nova ligação (nome standard HTTP Connection Manager)
- 4. Colocar o endereço do ficheiro (exemplo: "http://www.hello_world.com/ficheiro.csv") na nova ligação
- 5. Colocar um objecto Script Task no Control Flow e colocar o código C# (abaixo indicado [necessário login para visualizar o código]) no script
- 6. Fazer download do ficheiro e colocar na pasta que vai servir de repositório
- 7. Na Connections Manager criar uma ligação New Flat File Connection
- 8. No Flat File Connection Manager Editor, colocar na Connection manager name: the_downloaded_file e localizar o ficheiro (ponto 6) usando o botão Browse.
- Ter atenção ao tamanho das colunas atribuido automaticamente pelo sistema.
- 9. Colocar um objecto Data Flow no Control Flow
- 10. Dentro do objecto Data Flow colocar um objecto Flat File Source e ligar Flat file connection manager: the_downloaded_file
- 11. Colocar uma ligação a uma BD e ligar o objecto Flat File Source a esta. */
- using System;
- using System.Data;
- using Microsoft.SqlServer.Dts.Runtime;
- using System.Windows.Forms;
- namespace ST_6799d08685cb4ad78633d035fab12178.csproj
- {
- [System.AddIn.AddIn("ScriptMain", Version = "1.0", Publisher = "", Description = "")]
- public partial class ScriptMain : Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase
- {
- #region VSTA generated code
- enum ScriptResults
- {
- Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success,
- Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure
- };
- #endregion
- public void Main()
- {
- try
- {
- // Logging start of download
- bool fireAgain = true;
- // http://colinkirkby.blogspot.com/2007/01/ssis-logging-within-script-tasks.html
- Dts.Events.FireInformation(0, "Download File", "Start downloading " + Dts.Connections["HTTP Connection Manager"].ConnectionString, string.Empty, 0, ref fireAgain);
- // Get your newly added HTTP Connection Manager
- Object mySSISConnection = Dts.Connections["HTTP Connection Manager"].AcquireConnection(null);
- // Create a new connection
- HttpClientConnection myConnection = new HttpClientConnection(mySSISConnection);
- // Download file and use the Flat File Connectionstring (D:\SourceFiles\Products.csv)
- // to save the file (and replace the existing file)
- myConnection.DownloadFile(Dts.Connections["the_downloaded_file"].ConnectionString, true);
- // Logging end of download
- Dts.Events.FireInformation(0, "Download File", "Finished downloading " + Dts.Connections["the_downloaded_file"].ConnectionString, string.Empty, 0, ref fireAgain);
- // Quit Script Task succesful
- Dts.TaskResult = (int)ScriptResults.Success;
- }
- catch (Exception ex)
- {
- // Logging why download failed
- Dts.Events.FireError(0, "Download File", "Download failed: " + ex.Message, string.Empty, 0);
- // Quit Script Task unsuccesful
- Dts.TaskResult = (int)ScriptResults.Failure;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement