问题
I am trying to copy files from an FTP server to a local drive using C# Task script in SSIS. The script ran well in SQL Studio 2008 R2, but there was a version update to 2016 using SQL SSDT (SQL Server Data Tools) 2015, and when I first executed the script it ran OK, but later threw the following error:
Error: 0x1 at 3-Copy and rename EC Files: Exception has been thrown by the target of an invocation. Task failed: 3-Copy and rename EC Files
I read a few post, and learned that the respondent fixed the problem by adding reference to dll version 12.0.0 and changed Target Framework to .Net Framework 4.5.
Currently my Target Framework is .Net Framework 4.5.
How can I stop getting this error?
Where in application would I find the dll reference to do that change?
I would appreciate your help.
My C# program is shown below:
using System;
using System.IO;
namespace ST_d70bfcb8d94b40849d1d525fe3731f14.csproj
{
[Microsoft.SqlServer
.Dts.Tasks.ScriptTask.SSISScriptTaskEntryPointAttribute]
public partial class ScriptMain : Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase
{
#region VSTA generated code
enum ScriptResults
{
Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success,
Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure
};
#endregion
public void Main()
{
string fileDate = string.Format("{0:d4}", DateTime.Today.Year).ToString() + string.Format("{0:d2}", DateTime.Today.Month).ToString() + "13";
string src1FileName = @"\\Slocation03\Reports\SSI224-069_" + fileDate + ".txt";
string des1FileName = @"\\Slocation03\Reports\EContacts\SSI224-069.txt";
string src2FileName = @"\\Slocation03\Reports\SSI224-071_" + fileDate + ".txt";
string des2FileName = @"\\Slocation03\Reports\EContacts\SSI224-071.txt";
if (File.Exists(src1FileName))
{
File.Copy(src1FileName, des1FileName, true);
}
if (File.Exists(src2FileName))
{
File.Copy(src2FileName, des2FileName, true);
}
Dts.TaskResult = (int)ScriptResults.Success;
}
}
}
回答1:
The Error may be cause by a permission issue for reading from UNC path or writing to local file, try adding a try ... catch block to read the real exception since the following exception is generic:
Exception has been thrown by the target of an invocation
Try using the following code:
public void Main()
{
try{
string fileDate = string.Format("{0:d4}", DateTime.Today.Year).ToString() + string.Format("{0:d2}", DateTime.Today.Month).ToString() + "13";
string src1FileName = @"\\Slocation03\Reports\SSI224-069_" + fileDate + ".txt";
string des1FileName = @"\\Slocation03\Reports\EContacts\SSI224-069.txt";
string src2FileName = @"\\Slocation03\Reports\SSI224-071_" + fileDate + ".txt";
string des2FileName = @"\\Slocation03\Reports\EContacts\SSI224-071.txt";
if (File.Exists(src1FileName))
{
File.Copy(src1FileName, des1FileName, true);
}
if (File.Exists(src2FileName))
{
File.Copy(src2FileName, des2FileName, true);
}
Dts.TaskResult = (int)ScriptResults.Success;
}catch(Exception ex){
Dts.FireError(0,"An error occured", ex.Message,String.Empty, 0);
Dts.TaskResult = (int)ScriptResult.Failure;
}
}
References
- SSIS - Script Task error: Exception has been thrown by the target of an invocation
- Copy Files from UNC to Local Drive
来源:https://stackoverflow.com/questions/56299342/error-0x1-at-xx-exception-has-been-thrown-by-the-target-of-an-invocation