April 7, 2022 at 10:04 am
I consulted several forums which generally explains 2 possible solutions:
1- a paid SSIS component that does the job and configure it as a task ssis
2- the passage by a third party software which makes the sending (the most used is WinSCP) and in SSIS:
Use "execute task" to trigger an execution of a script in WinSCP
But within the company they force us to look for a 100% SSIS solution with a C# script (script task) or other: how to do that?
Below is a C# code that has errors now: do you offer corrections or a C# code that works to send local files to SFTP server or Stored procedure ?
CODE C#
#region Help: Introduction to the script task
/* The Script Task allows you to perform virtually any operation that can be accomplished in
* a .Net application within the context of an Integration Services control flow.
*
* Expand the other regions which have "Help" prefixes for examples of specific ways to use
* Integration Services features within this script task. */
#endregion
#region Namespaces
using System;
using System.Data;
using Microsoft.SqlServer.Dts.Runtime;
using System.Windows.Forms;
using Renci.SshNet;
using System.IO;
#endregion
namespace ST_d27fb029ddab4957bc327e8fc4c35c18
{
/// <summary>
/// ScriptMain is the entry point class of the script. Do not change the name, attributes,
/// or parent of this class.
/// </summary>
[Microsoft.SqlServer.Dts.Tasks.ScriptTask.SSISScriptTaskEntryPointAttribute]
public partial class ScriptMain : Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase
{
#region Help: Using Integration Services variables and parameters in a script
/* To use a variable in this script, first ensure that the variable has been added to
* either the list contained in the ReadOnlyVariables property or the list contained in
* the ReadWriteVariables property of this script task, according to whether or not your
* code needs to write to the variable. To add the variable, save this script, close this instance of
* Visual Studio, and update the ReadOnlyVariables and
* ReadWriteVariables properties in the Script Transformation Editor window.
* To use a parameter in this script, follow the same steps. Parameters are always read-only.
*
* Example of reading from a variable:
* DateTime startTime = (DateTime) Dts.Variables["System::StartTime"].Value;
*
* Example of writing to a variable:
* Dts.Variables["User::myStringVariable"].Value = "new value";
*
* Example of reading from a package parameter:
* int batchId = (int) Dts.Variables["$Package::batchId"].Value;
*
* Example of reading from a project parameter:
* int batchId = (int) Dts.Variables["$Project::batchId"].Value;
*
* Example of reading from a sensitive project parameter:
* int batchId = (int) Dts.Variables["$Project::batchId"].GetSensitiveValue();
* */
#endregion
#region Help: Firing Integration Services events from a script
/* This script task can fire events for logging purposes.
*
* Example of firing an error event:
* Dts.Events.FireError(18, "Process Values", "Bad value", "", 0);
*
* Example of firing an information event:
* Dts.Events.FireInformation(3, "Process Values", "Processing has started", "", 0, ref fireAgain)
*
* Example of firing a warning event:
* Dts.Events.FireWarning(14, "Process Values", "No values received for input", "", 0);
* */
#endregion
#region Help: Using Integration Services connection managers in a script
/* Some types of connection managers can be used in this script task. See the topic
* "Working with Connection Managers Programatically" for details.
*
* Example of using an ADO.Net connection manager:
* object rawConnection = Dts.Connections["Sales DB"].AcquireConnection(Dts.Transaction);
* SqlConnection myADONETConnection = (SqlConnection)rawConnection;
* //Use the connection in some code here, then release the connection
* Dts.Connections["Sales DB"].ReleaseConnection(rawConnection);
*
* Example of using a File connection manager
* object rawConnection = Dts.Connections["Prices.zip"].AcquireConnection(Dts.Transaction);
* string filePath = (string)rawConnection;
* //Use the connection in some code here, then release the connection
* Dts.Connections["Prices.zip"].ReleaseConnection(rawConnection);
* */
#endregion
/// <summary>
/// This method is called when this script task executes in the control flow.
/// Before returning from this method, set the value of Dts.TaskResult to indicate success or failure.
/// To open Help, press F1.
/// </summary>
public void Main()
{
// TODO: Add your code here
string Server = Dts.Variables["FTPServer"].Value.ToString();
string User = Dts.Variables["FTPUser"].Value.ToString();
string Password = Dts.Variables["FTPPassword"].Value.ToString();
int port = (int) Dts.Variables["FTPPort"].Value;
// string Folder = Dts.Variables["FTPFolder"].Value.ToString();
string localFolder = Dts.Variables["ExportEgypte_LocalFolderPath"].Value.ToString(); ;
string fileName = Dts.Variables["fileName"].Value.ToString();
// string absoluteFileName = Path.GetFileName(XMLFileName);
// string FullName = localFolder + "SubFolderName." + fileName;
string FullName = localFolder + fileName;
string remotePath = Dts.Variables["FTPRemoteFolder"].Value.ToString() + fileName; ;
uploadFile(Server, port, Password, User, FullName,remotePath);
Dts.TaskResult = (int)ScriptResults.Success;
}
void uploadFile(string FTPServer,int port,string FTPPassword,string FTPUser,string localFilePath,string remotePath)
{
try
{
using (var client = new SftpClient(FTPServer, port, FTPUser, FTPPassword))
{
client.Connect();
if (!client.IsConnected)
{
throw new Exception("Not Connected");
}
using (FileStream fs = File.OpenRead(localFilePath))
{
client.UploadFile(fs, remotePath, true, null);
}
client.Disconnect();
}
}
catch (Exception exception)
{
Console.WriteLine(exception);
throw;
}
}
#region ScriptResults declaration
/// <summary>
/// This enum provides a convenient shorthand within the scope of this class for setting the
/// result of the script.
///
/// This code was generated automatically.
/// </summary>
enum ScriptResults
{
Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success,
Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure
};
#endregion
}
}
April 7, 2022 at 1:37 pm
WinSCP can be called from an SSIS C# task (link).
We can't debug a large chunk of C# code for you. Especially if you do not tell us what errors you are getting!
The absence of evidence is not evidence of absence
- Martin Rees
The absence of consumable DDL, sample data and desired results is, however, evidence of the absence of my response
- Phil Parkin
April 7, 2022 at 3:35 pm
Hello Phil,
thanks for answer. I have 2 mains errors :
1- on my code c# i have this error : not identifing this class , i see videos how to add renci packages with NUGET packages and when i close the window o script task (based on c#) , it is not able to save this class and he forget this identification of class , so the code still with errors
--> the solution required by my company is to use tools and components in SSIS and not other software program , that will be very nice to correct this code or if you have another that i can test it.
2- i try to use the second solution , WinSCP , only to show in my Next call with team , but by beginning :
--> the application bash not supported (a problem from the step install) and my script .bat not working to test (a script that make put file from local to sftp server)
Thanks for sharing and it will be nice if i found a script C# in SSIS , maybe a solution 100%.
Kind regards,
Ahmed
April 7, 2022 at 3:39 pm
Referencing assemblies using NUGET does not work in SSIS script tasks. Instead, you need to register the assembly in the global assembly cache.
Please read the following page and see whether that helps you: http://binaryworld.net/blogs/reference-assembly-in-ssis-script-task-net-custom-assembly/
The absence of evidence is not evidence of absence
- Martin Rees
The absence of consumable DDL, sample data and desired results is, however, evidence of the absence of my response
- Phil Parkin
April 11, 2022 at 8:47 pm
Hello Phil, how are you . thanks for your post : it's helpful !
The syntax seems to be correct in C# (script task) when i follow the steps shown in your link
http://binaryworld.net/blogs/reference-assembly-in-ssis-script-task-net-custom-assembly/
but still having this error now and it should be easy to solve :
it talks about not founding the file or assembly (ddl) : i paste the file ddl in 2 folders but not solving the problem , sure it's a simple config because he don't found the file assembly to check the code and run renci.ssh the rest :
C:\Program Files (x86)\Microsoft Visual Studio\2017\SQL\Common7\IDE
C:\Program Files (x86)\Microsoft SQL Server\150\SDK\Assemblies
April 12, 2022 at 10:42 am
Can you confirm that you also ran GACUtil to add the assembly (DLL) to the Global Assembly Cache?
The absence of evidence is not evidence of absence
- Martin Rees
The absence of consumable DDL, sample data and desired results is, however, evidence of the absence of my response
- Phil Parkin
April 14, 2022 at 1:19 pm
Hello Phil ,
Really i'm writing to say "Many thanks" for you : So the problem is sloved in Desktop , it's a DLL files that should be installed in the global cache with Commands Prompts of visual studio , after we can call the specified references Renci in the C# Code . This is a helpful link that describes the solution : https://markedcode.com/index.php/2020/04/27/how-to-access-sftp-with-ssis-using-ssh-net/
But we still have the problem in cloud , my project is deplyed in Azure server (DEV) and i should install gacutil and these file in the specified folder , and i tried to modify the main.cmd in Azure storage now :
the code that i inseted in main.cmd :
gacutil /i Renci.SshNet.dll /f
gacutil /i SshNet.Security.Cryptography.dll /f
REM error handling
if %ERRORLEVEL% neq 0 (
echo Failed with ExitCode %ERRORLEVEL%
exit /b %ERRORLEVEL%)
echo Successfully installed Analysis Services DLL Task.
--> it can have errors , So if you have any links to help, please reply for me , thanks .
Best regards,
AHMED
April 14, 2022 at 4:06 pm
also I see these links about install dll files with powershell to run my ssis package in server (located in Azure) , but still error : renci dll not installed on IR SSIS
https://stackoverflow.com/questions/45593418/register-dll-in-gac-cmd-or-powershell https://www.andrewcbancroft.com/2015/12/16/using-powershell-to-install-a-dll-into-the-gac/
SSIS error on the execution (SSIS calatlog located on server - azure) :
Could not load file or assembly 'Renci.SshNet, Version=2016.0.0.0, Culture=neutral,
PublicKeyToken=1cee9f8bde3db106' or one of its dependencies. The system cannot find the file specified.
my powershell code :
Set-location (Get-Location).Path
[System.Reflection.Assembly]::Load("System.EnterpriseServices, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")
$publish = New-Object System.EnterpriseServices.Internal.Publish
$publish.GacInstall((Get-Location).Path + "\Renci.SshNet.dll")
April 15, 2022 at 8:57 am
I used to use the Azure SSIS Integration Runtime, but stopped using it because of some limitations (it was expensive too). It was more than a year ago, so I have forgotten what the limitations were, but installing third-party assemblies to the GAC may be one of them.
The absence of evidence is not evidence of absence
- Martin Rees
The absence of consumable DDL, sample data and desired results is, however, evidence of the absence of my response
- Phil Parkin
April 19, 2022 at 11:14 am
Thanks Phil for your comments and help , I'll search more for a solution.
I think it'll be a simple modification in my cmd code (or powershell code) OR the code is correct and juste we need an administrator permission to execute this file.cmd in IR SSIS. It's not complex but need an official documentation and being in contact with someone from Microsoft
April 28, 2022 at 3:20 pm
the problem is solved with IR SSIS following this link about customized install in IR
https://docs.microsoft.com/en-us/azure/data-factory/how-to-configure-azure-ssis-ir-custom-setup
my main.cmd script works very well, it was the missing file that I added to unblock the situation! (the gacutil folder should contain: gacutil.exe, gacutil.exe.config, and 1033\gacutlrc.dll)
and when I run the deployed package on the server that uses IR , it works perfectly. Many thanks to Phil
Viewing 11 posts - 1 through 10 (of 10 total)
You must be logged in to reply to this topic. Login to reply