I am creating an assembly file so that get files and put files can be called as a Stored Procedure from SQL Server. Below is the method for put files. When I pass SqlBoolean DeleteLocalFolder=true, WinSCP errors out with the below error message:
Error: WinSCP.SessionRemoteException: Error deleting file System Error. Code: 5
[SqlProcedure]
public static void PutFiles(SqlString HostName, SqlString UserName, SqlString Password,
SqlString SshHostKeyFingerprint, SqlString SshPrivateKeyPath,
SqlString SshPrivateKeyPassphrase, SqlString LocalFolderPath,
SqlString RemoteFolderPath,SqlBoolean DeleteLocalFolder,
SqlString FileMask, out SqlString strMessage)
{
// Declare Variables to hold the input parameter values
string hostName = HostName.Value;
string userName = UserName.Value;
string password = Password.Value;
string sshFingerPrint = SshHostKeyFingerprint.Value;
string sshPrivateKey = SshPrivateKeyPath.Value;
string sshPassPhrase = SshPrivateKeyPassphrase.Value;
bool delateLocalFolder = DeleteLocalFolder.Value;
//Local Directory
DirectoryInfo dir = new DirectoryInfo(LocalFolderPath.Value);
string folderName = dir.Name;
//Begin connection to SFTP **Always uses default SFTP port
try
{
string FtpFolderPath = RemoteFolderPath.Value;
SessionOptions options = new SessionOptions()
{
Protocol = Protocol.Sftp,
HostName = hostName,
UserName = userName,
Password = password,
SshHostKeyFingerprint = sshFingerPrint,
SshPrivateKeyPath = sshPrivateKey,
SshPrivateKeyPassphrase = sshPassPhrase
};
//Open the Remote session
using (Session session = new Session())
{
session.ExecutablePath = ExecutablePath+"WinSCP.exe";
session.SessionLogPath = ExecutablePath+"WinscpLog.log";
session.DisableVersionCheck = true;
session.Open(options);
session.ExecutableProcessUserName = "username to log into the sql server instance";
SecureString _Password = new SecureString();
foreach (char _PasswordChar in "password to log in sql instance")
{
_Password.AppendChar(_PasswordChar);
}
session.ExecutableProcessPassword = _Password;
// Upload files
TransferOptions transferOptions = new TransferOptions();
transferOptions.TransferMode = TransferMode.Binary;
transferOptions.FileMask = FileMask.Value;
TransferOperationResult transferoperationalResult;
transferoperationalResult = session.PutFiles(LocalFolderPath.Value, FtpFolderPath, false, transferOptions);
transferoperationalResult.Check();
if (transferoperationalResult.IsSuccess)
{
if (dir.Exists == true)
{
string sourcePath = FtpFolderPath + folderName + "//";
session.MoveFile(sourcePath + "*.*", FtpFolderPath);
session.RemoveFiles(sourcePath);
}
}
}
strMessage = "Upload of files successful";
}
catch (Exception e)
{
//Console.WriteLine("Error: {0}", e);
strMessage = "Error: "+ e;
}
}
I have written custom code in SSIS script task and have called the Session.PutFiles method by passing the argument bool remove = true. It works with out any problem. Can anyone please explain why it errors only in the custom assembly?