2015-12-22 160 views
2

我試圖通過SSIS包連接到SFTP服務器。然而,包不保持能夠連接失敗通過SSIS連接到SFTP

open sftp://username:fc$#[email protected]:22 

:包執行WinSCP在一個.txt文件下面的連接字符串。這與密碼中的特殊字符有關嗎?

如果我替換字符串,我能夠連接到不同的SFTP,所以我知道它必須與上面的語法有關。我試圖把雙引號括起來的周圍如下沒有任何成功:

open "sftp://username:fc$#[email protected]:22" 
+1

將'/ log = c:\ path \ to \ winscp.log'添加到WinSCP命令行並向我們顯示日誌。 –

回答

2

我不得不最近就此別過,我的工作項目之一。我們在SSIS腳本任務中使用了WinSCP .NET程序集,因爲這是WinSCP也推薦的在SSIS中使用WinSCP實現SFTP的方式。

參見本指南 - Using WinSCP .NET Assembly from SQL Server Integration Services (SSIS)。它引導您完成安裝和設置,並且還包含工作示例代碼(當然,您根據需要更改腳本!)。

示例代碼 - 在您參考WinSCPnet.dll程序集後 - 位於下方。

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; 
      } 
     } 
    } 
}