c# - 错误 : 0x1 at XX: Exception has been thrown by the target of an invocation

标签 c# sql-server ssis etl script-task

我正在尝试使用 SSIS 中的 C# 任务脚本将文件从 FTP 服务器复制到本地驱动器。该脚本在 SQL Studio 2008 R2 中运行良好,但使用 SQL SSDT (SQL Server Data Tools) 2015 将版本更新到 2016,当我第一次执行该脚本时它运行正常,但后来抛出以下错误:

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

看了几个帖子,得知答主通过添加dll版本12.0.0的引用,并将Target Framework改为.Net Framework 4.5,解决了问题。

目前我的目标框架是 .Net Framework 4.5。

我怎样才能停止收到这个错误?

在应用程序中的什么地方可以找到执行该更改的 dll 引用?

非常感谢您的帮助。

我的C#程序如下图所示:

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

}

最佳答案

错误可能是由从 UNC 路径读取或写入本地文件的权限问题引起的,尝试添加一个 try ... catch block 来读取真正的异常,因为以下异常是通用的:

Exception has been thrown by the target of an invocation

尝试使用以下代码:

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;

    }

}

引用资料

关于c# - 错误 : 0x1 at XX: Exception has been thrown by the target of an invocation,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56299342/

相关文章:

c# - 如果文件不存在,试图让我的 SSIS 继续运行

sql-server - 即使使用 Unicode 源和目标 (SSIS),字符也会显示不正确

c# - 使用 Convert.ChangeType 在字符串和枚举之间进行转换

c# - 与 "system-wide"媒体播放器交互

SQL - 将非空邻接列表转换为路径

sql - 查找并删除表中允许多次出现记录的重复行

c# - 为管理员用户角色呈现未 bundle 的 Assets

c# - 主要: not all code paths return a value

mysql - SQL按学生年级排序

sql-server - SSIS:请求的 OLE DB 提供程序 SQLNCLI10.1 未注册