This is an old revision of the document!

Using WinSCP .NET Assembly from SQL Server Integration Services (SSIS)

Advertisement

Installing

First, you need to install the WinSCP .NET assembly.

You also need to install the assembly to the GAC1.

Using from SSIS

You use WinSCP .NET assembly from SSIS as any other .NET assembly:

  • In Microsoft Visual Studio2, in your “Integration Services Project”, choose your “SSIS Package” (e.g. the default Package.dtsx);
  • Drag Script task from Toolbox onto Control flow view of the package;
  • In the context menu of the task, choose Edit;
  • On the Script task editor on Script page, click Edit script button;
  • Visual Studio Tools for Applications is run to edit the script;
  • Use Project > Add Reference > Browse to add reference to winscpnet.dll;
  • Place your C# or VB.NET code into ScriptMain.Main method (see example below);
  • Close Visual Studio Tools for Applications and Script task editor with OK button.

Deploying WinSCP .NET Assembly

When deploying your SSIS package, WinSCP .NET assembly should be installed to GAC to be accessible. It is possible to redirect the assembly elsewhere, however the location must be accessible by the user that the package is running as, and a Static Constructor handler needs to be attached inside the script (See This MSDC blog article for more detailed information).

Advertisement

Some sample code (Add inside the class):

public partial class ScriptMain : Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase
{
   static ScriptMain()
   {
       AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(CurrentDomain_AssemblyResolve);
      //The rest of your code
   }
   static System.Reflection.Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
   {
       if (args.Name.Contains("winscpnet.dll"))
       {
           string path = @"c:\temp\";
           return System.Reflection.Assembly.LoadFile(System.IO.Path.Combine(path, "winscpnet.dll"));
       }
       return null;
   }
}

Example C# Script Task Code

using System;
using Microsoft.SqlServer.Dts.Runtime;
using Microsoft.SqlServer.Dts.Tasks.ScriptTask;
using System.AddIn;
using WinSCP;
 
namespace ST_5a30686e70c04c5a8a93729fd90b8c79.csproj
{
    [AddIn("ScriptMain", Version = "1.0", Publisher = "", Description = "")]
    public partial class ScriptMain : VSTARTScriptObjectModelBase
    {
        public void Main()
        {
            // Setup session options
            SessionOptions sessionOptions = new SessionOptions
            {
                Protocol = Protocol.Sftp,
                // To setup these variables, go to SSIS > Variables.
                // To make them accessible from the script task, in the context menu of the task,
                // choose Edit. On the Script task editor on Script page, select ReadOnlyVariables,
                // and tick the below properties.
                HostName = (string) Dts.Variables["User::HostName"].Value,
                UserName = (string) Dts.Variables["User::UserName"].Value,
                Password = (string) Dts.Variables["User::Password"].Value,
                SshHostKeyFingerprint = (string) Dts.Variables["User::SshHostKeyFingerprint"].Value
            };
 
            try
            {
                using (Session session = new Session())
                {
                    // As WinSCP .NET assembly has to be stored in GAC to be used with SSIS,
                    // you need to set path to WinSCP.exe explicitly, if using non-default location.
                    session.ExecutablePath = @"C:\winscp\winscp.exe";
 
                    // Connect
                    session.Open(sessionOptions);
 
                    // Upload files
                    TransferOptions transferOptions = new TransferOptions();
                    transferOptions.TransferMode = TransferMode.Binary;
 
                    TransferOperationResult transferResult;
                    transferResult = session.PutFiles(@"d:\toupload\*", "/home/user/", false, transferOptions);
 
                    // Throw on any error
                    transferResult.Check();
 
                    // Print results
                    bool fireAgain = false;
                    foreach (TransferEventArgs transfer in transferResult.Transfers)
                    {
                        Dts.Events.FireInformation(0, null, 
                            string.Format("Upload of {0} succeeded", transfer.FileName),
                            null, 0, ref fireAgain);
                    }
                }
 
                Dts.TaskResult = (int)DTSExecResult.Success;
            }
            catch (Exception e)
            {
                Dts.Events.FireError(0, null,
                    string.Format("Error when using WinSCP to upload files: {0}", e),
                    null, 0);
    
                Dts.TaskResult = (int)DTSExecResult.Failure;
            }
        }
    }
}

Advertisement

  1. SSIS can only use assemblies installed to GACBack
  2. This guide is for Microsoft Visual Studio 2008. The steps may differ for other versions. Do not hesitate to update this document for other versions.Back