SSIS 包在脚本任务 file.delete 上失败

SSIS package fails on file.delete of script task

我有一个 SSIS 包,zip.dtsx。此程序包在服务器 A 上成功 运行s。我在 serverB 中复制了这个包。但是,当我尝试在 serverB 上 运行 zip.dtsx 时,它失败了。

zip.dtsx只是读取源文件夹中的文件,压缩它,将压缩文件保存到不同的文件夹,然后删除源文件夹中的原始文件。

经过一些调查,我发现 如果我注释掉 C# 脚本任务中删除源文件夹中文件的部分。包 运行 成功。

我需要删除源文件夹中的文件。否则,这个文件只会重复加载到数据库中。我已经按照建议 here 重新添加了脚本任务引用,但我仍然无法使 file.delete 运行 成功。

public void Main()
        {
            String sourcePath = Convert.ToString(Dts.Variables["SourcePath"].Value);
            String namePart = Convert.ToString(Dts.Variables["NamePart"].Value);
            String destinationPath = Convert.ToString(Dts.Variables["DestinationPath"].Value);
            FileStream sourceFile = File.OpenRead(@sourcePath + namePart);
            FileStream destFile = File.Create(@destinationPath + namePart);

            GZipStream compStream = new GZipStream(destFile, CompressionMode.Compress);

            try
            {
                int theByte = sourceFile.ReadByte();
                while (theByte != -1)
                {
                    compStream.WriteByte((byte)theByte);
                    theByte = sourceFile.ReadByte();
                }
            }
            finally
            {
                compStream.Dispose();
                sourceFile.Close();
                destFile.Close();
                File.Delete(@sourcePath + namePart);
            } 

            Dts.TaskResult = (int)ScriptResults.Success;
        }

更新:

尝试完全相同的代码后 here。并发现这段代码删除了我在源文件夹中的文件,我尝试更新我的代码以遵循文件在 link 中删除的方式。但是,它仍然没有用。以下是我更新代码的方式。

    String sourcePath = Convert.ToString(Dts.Variables["SourcePath"].Value);
    String namePart = Convert.ToString(Dts.Variables["NamePart"].Value);
    String destinationPath = Convert.ToString(Dts.Variables["DestinationPath"].Value);
    FileStream sourceFile = File.OpenRead(@sourcePath + namePart);
    FileStream destFile = File.Create(@destinationPath + namePart);

    GZipStream compStream = new GZipStream(destFile, CompressionMode.Compress);

    try
    {
        int theByte = sourceFile.ReadByte();
        while (theByte != -1)
        {
            compStream.WriteByte((byte)theByte);
            theByte = sourceFile.ReadByte();
        }
    }
    finally
    {
        compStream.Dispose();
        sourceFile.Close();
        destFile.Close();
        FileInfo currFileInfo = new FileInfo(@sourcePath + namePart);
        currFileInfo.Delete();

我终于弄明白了。

为了解释整个情况,我们有一个完整的 运行 sqlserver,serverA。我们想在 serverB 上复制它,以便我们可以在 serverB 上有一个测试环境。 serverB 上已经恢复了必要的数据库,剩下的就是 SSIS 包和 SQL 服务器代理作业。

将文件内容加载到数据库的 主程序包 (main.dtsx) 失败。

从 Integration Services Catalog -> Catalog 文件夹 -> 右键单击​​ -> Reports -> All Executions,我了解到它在从 main.dtsx 调用的 Zip.dtsx 上失败。 Zip.dtsx 压缩访问的文件,将其存档并将其从源文件夹中删除。

在 Zip.dtsx(我从 Kannan Kandasamy's comments 得到的想法)中尝试了脚本任务后,我发现它在 File.delete() 部分我的脚本任务失败了。瞬间,有人会认为这是一个权限问题。

我的 第一个错误 是我在执行 Zip.dtsx 时继续玩我的脚本任务,方法是右键单击 -> 在 Visual Studio 上执行任务。我一直在 my previous post 中获取运行时错误屏幕截图,却没有意识到我正在获取它,因为我正在使用由 main.dtsx 传递给 zip.dtsx 的包变量。在我弄清楚为什么当我用我的硬编码路径名替换变量时脚本任务成功运行之前,我一直挂断了这个。

我的 第二个错误 是用我的硬编码路径替换 Zip.dtsx 的包变量。直到最后,我才意识到 Zip.dtsx 访问的文件夹是 serverB 中的一个本地文件夹,我是 运行 我本地计算机中的 SQL 服务器代理作业 ,说 machineA。因此,我将 serverB 本地文件夹共享到我的用户帐户。出于某种原因,这严重破坏了我的包,它不再看到文件夹中的文件,因此我的包成功了 运行,因为它发现文件夹是空的。

主要解决方案:

我改回了对 Zip.dtsx 所做的更改,并删除了服务器 B 的本地文件夹到我的用户帐户的共享,而是添加了 NT Service\SQL$Instance 以完全控制源文件夹脚本任务删除文件的位置。请参阅此 link 了解如何将 SQLServer$Instance 添加到文件夹权限设置。

我遇到的其他问题是:

  1. 当从不同的服务器传输包并且包失败时,我认为this is also important。我也做了这一步。但是在serverB中,我找不到Microsoft.SQLServer.ManagedDTS.dll。我所做的是 将此 dll 从 serverA 复制到 serverB。还要检查您的系统路径以查看您默认使用的 Microsoft SQL 服务器工具版本,并确保这是您在脚本任务中引用的内容。

  2. 在调查的某个时间点,我在查看 Integration Services Catalog's All Executions 报告时遇到错误。我通过转到 C:\Temp 文件夹解决了这个问题。当我双击它来访问它时,会出现一个对话框,提示我目前无权访问。我继续单击对话框中的 'continue' 按钮。之后,所有执行报告再次 运行。 我从 SSMS 得到的错误是,

TITLE: Microsoft SQL Server Management Studio

An error occurred during local report processing. (Microsoft.ReportViewer.WinForms)

------------------------------ ADDITIONAL INFORMATION:

The definition of the report '' is invalid. (Microsoft.ReportViewer.Common)


An unexpected error occurred while compiling expressions. Native compiler return value: ‘[BC2001] file 'C:\Windows\TEMP\mpgc21o3.0.vb' could not be found’. (Microsoft.ReportViewer.Common)

------------------------------ BUTTONS:

OK

我不确定这对我的情况有多大影响,但我会分享它以防有人遇到同样的问题。我是 MS SQL 服务器的初学者,所以我认为这可能会对像我这样也是初学者的人有所帮助。