Advertisement
Guest User

Untitled

a guest
Apr 18th, 2016
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.84 KB | None | 0 0
  1. using System;
  2. using Microsoft.SqlServer.Dts.Runtime;
  3. using Microsoft.SqlServer.Dts.Tasks.ScriptTask;
  4. using System.AddIn;
  5. using WinSCP;
  6.  
  7. namespace ST_5a30686e70c04c5a8a93729fd90b8c79.csproj
  8. {
  9. [AddIn("ScriptMain", Version = "1.0", Publisher = "", Description = "")]
  10. public partial class ScriptMain : VSTARTScriptObjectModelBase
  11. {
  12. public void Main()
  13. {
  14. // Setup session options
  15. SessionOptions sessionOptions = new SessionOptions
  16. {
  17. Protocol = Protocol.Sftp,
  18. // To setup these variables, go to SSIS > Variables.
  19. // To make them accessible from the script task, in the context menu of the task,
  20. // choose Edit. On the Script task editor on Script page, select ReadOnlyVariables,
  21. // and tick the below properties.
  22. HostName = (string) Dts.Variables["User::HostName"].Value,
  23. UserName = (string) Dts.Variables["User::UserName"].Value,
  24. Password = (string) Dts.Variables["User::Password"].Value,
  25. SshHostKeyFingerprint = (string) Dts.Variables["User::SshHostKeyFingerprint"].Value
  26. };
  27.  
  28. try
  29. {
  30. using (Session session = new Session())
  31. {
  32. // As WinSCP .NET assembly has to be stored in GAC to be used with SSIS,
  33. // you need to set path to WinSCP.exe explicitly, if using non-default location.
  34. session.ExecutablePath = @"C:\winscp\winscp.exe";
  35.  
  36. // Connect
  37. session.Open(sessionOptions);
  38.  
  39. // Upload files
  40. TransferOptions transferOptions = new TransferOptions();
  41. transferOptions.TransferMode = TransferMode.Binary;
  42.  
  43. TransferOperationResult transferResult;
  44. transferResult = session.PutFiles(@"d:\toupload\*", "/home/user/", false, transferOptions);
  45.  
  46. // Throw on any error
  47. transferResult.Check();
  48.  
  49. // Print results
  50. bool fireAgain = false;
  51. foreach (TransferEventArgs transfer in transferResult.Transfers)
  52. {
  53. Dts.Events.FireInformation(0, null,
  54. string.Format("Upload of {0} succeeded", transfer.FileName),
  55. null, 0, ref fireAgain);
  56. }
  57. }
  58.  
  59. Dts.TaskResult = (int)DTSExecResult.Success;
  60. }
  61. catch (Exception e)
  62. {
  63. Dts.Events.FireError(0, null,
  64. string.Format("Error when using WinSCP to upload files: {0}", e),
  65. null, 0);
  66.  
  67. Dts.TaskResult = (int)DTSExecResult.Failure;
  68. }
  69. }
  70. }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement