Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ssis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server 2005 在SSI中的某些特定错误上继续流_Sql Server 2005_Ssis_Ftp - Fatal编程技术网

Sql server 2005 在SSI中的某些特定错误上继续流

Sql server 2005 在SSI中的某些特定错误上继续流,sql-server-2005,ssis,ftp,Sql Server 2005,Ssis,Ftp,在SSIS 2005中,我使用FTP任务。我有一个流程,当包运行时,它会将特定文件夹中的任何文件从FTP检索到本地文件夹 远程文件夹路径由/root/abc/*abc.txt等变量设置 如果该文件夹中有符合此条件的文件,则该任务工作正常。如果没有文件,任务将失败,并出现“找不到文件”错误 如果由于远程文件夹没有匹配的文件而出现此特定文件未找到错误,我如何使SSI不中断任务 但是,如果出现诸如FTP服务器无法登录等错误,则任务应抛出预期错误。可能您现在已经找到了问题的答案。以下是实现这一目标的一种

在SSIS 2005中,我使用FTP任务。我有一个流程,当包运行时,它会将特定文件夹中的任何文件从FTP检索到本地文件夹

远程文件夹路径由/root/abc/*abc.txt等变量设置

如果该文件夹中有符合此条件的文件,则该任务工作正常。如果没有文件,任务将失败,并出现“找不到文件”错误

如果由于远程文件夹没有匹配的文件而出现此特定文件未找到错误,我如何使SSI不中断任务


但是,如果出现诸如FTP服务器无法登录等错误,则任务应抛出预期错误。

可能您现在已经找到了问题的答案。以下是实现这一目标的一种可能方法。脚本任务可用于查找给定模式(例如*.txt)的FTP文件夹路径中存在的文件列表。下面的示例显示了如何做到这一点

逐步过程:

在SSIS包上,创建名为FTP的FTP连接,并创建5个变量,如屏幕截图1所示。变量RemotePath包含FTP文件夹路径;LocalPath包含文件将下载到的文件夹;FilePattern包含查找要从FTP服务器下载的文件列表的文件模式;文件名将由Foreach循环容器填充,但为了避免FTP任务设计时错误,可以使用/或将FTP任务上的DelayValidation属性设置为True来填充文件名

在SSIS包上,将脚本任务、Foreach循环容器和FTP任务放置在Foreach循环容器中,如屏幕截图2所示

将脚本任务中的Main方法替换为脚本任务代码部分下的代码。脚本任务将使用与给定模式匹配的文件集合填充变量ListOfFiles。本例将首先使用模式*.txt,它不会产生任何结果,然后使用模式*.xls,它将匹配FTP服务器上的几个文件

配置Foreach循环容器,如屏幕截图3和4所示。此任务将循环执行变量**ListOfFiles*。如果没有文件,循环容器内的FTP任务将不会执行。如果存在文件,循环容器中的FTP任务将针对FTP服务器上找到的文件数执行该任务

如屏幕截图5和6所示配置FTP任务

屏幕截图7显示了在找不到模式*.txt的匹配文件时执行包的示例

屏幕截图8显示了执行包之前文件夹C:\temp\的内容

屏幕截图9显示了在找到模式*.xls的匹配文件时执行包的示例

屏幕截图10显示FTP远程路径/Practice/Directory\u New的内容

屏幕截图11显示了执行包后文件夹C:\temp\的内容

屏幕截图12显示了当提供不正确的远程路径时的包故障

屏幕截图13显示了与包故障相关的错误消息

希望有帮助

脚本任务代码:

可在SSIS 2008及更高版本中使用的C代码

包括使用System.Text.RegularExpressions的using语句

截图1:

截图2:

截图3:

截图4:

截图5:

截图6:

截图7:

截图8:

截图9:

截图10:

截图11:

截图12:

截图13:

public void Main()
{
    Variables varCollection = null;
    ConnectionManager ftpManager = null;
    FtpClientConnection ftpConnection = null;
    string[] fileNames = null;
    string[] folderNames = null;
    System.Collections.ArrayList listOfFiles = null;
    string remotePath = string.Empty;
    string filePattern = string.Empty;
    Regex regexp;
    int counter;

    Dts.VariableDispenser.LockForWrite("User::RemotePath");
    Dts.VariableDispenser.LockForWrite("User::FilePattern");
    Dts.VariableDispenser.LockForWrite("User::ListOfFiles");
    Dts.VariableDispenser.GetVariables(ref varCollection);

    try
    {
        remotePath = varCollection["User::RemotePath"].Value.ToString();
        filePattern = varCollection["User::FilePattern"].Value.ToString();

        ftpManager = Dts.Connections["FTP"];
        ftpConnection = new FtpClientConnection(ftpManager.AcquireConnection(null));
        ftpConnection.Connect();
        ftpConnection.SetWorkingDirectory(remotePath);
        ftpConnection.GetListing(out folderNames, out fileNames);
        ftpConnection.Close();

        listOfFiles = new System.Collections.ArrayList();
        if (fileNames != null)
        {
            regexp = new Regex("^" + filePattern + "$");
            for (counter = 0; counter <= fileNames.GetUpperBound(0); counter++)
            {
                if (regexp.IsMatch(fileNames[counter]))
                {
                    listOfFiles.Add(remotePath + fileNames[counter]);
                }
            }
        }

        varCollection["User::ListOfFiles"].Value = listOfFiles;
    }
    catch (Exception ex)
    {
        Dts.Events.FireError(-1, string.Empty, ex.ToString(), string.Empty, 0);
        Dts.TaskResult = (int) ScriptResults.Failure;
    }
    finally
    {
        varCollection.Unlock();
        ftpConnection = null;
        ftpManager = null;
    }

    Dts.TaskResult = (int)ScriptResults.Success;
}
Public Sub Main()
    Dim varCollection As Variables = Nothing
    Dim ftpManager As ConnectionManager = Nothing
    Dim ftpConnection As FtpClientConnection = Nothing
    Dim fileNames() As String = Nothing
    Dim folderNames() As String = Nothing
    Dim listOfFiles As Collections.ArrayList
    Dim remotePath As String = String.Empty
    Dim filePattern As String = String.Empty
    Dim regexp As Regex
    Dim counter As Integer

    Dts.VariableDispenser.LockForRead("User::RemotePath")
    Dts.VariableDispenser.LockForRead("User::FilePattern")
    Dts.VariableDispenser.LockForWrite("User::ListOfFiles")
    Dts.VariableDispenser.GetVariables(varCollection)

    Try

        remotePath = varCollection("User::RemotePath").Value.ToString()
        filePattern = varCollection("User::FilePattern").Value.ToString()

        ftpManager = Dts.Connections("FTP")
        ftpConnection = New FtpClientConnection(ftpManager.AcquireConnection(Nothing))

        ftpConnection.Connect()
        ftpConnection.SetWorkingDirectory(remotePath)
        ftpConnection.GetListing(folderNames, fileNames)
        ftpConnection.Close()

        listOfFiles = New Collections.ArrayList()
        If fileNames IsNot Nothing Then
            regexp = New Regex("^" & filePattern & "$")
            For counter = 0 To fileNames.GetUpperBound(0)
                If regexp.IsMatch(fileNames(counter)) Then
                    listOfFiles.Add(remotePath & fileNames(counter))
                End If
            Next counter
        End If

        varCollection("User::ListOfFiles").Value = listOfFiles

        Dts.TaskResult = ScriptResults.Success

    Catch ex As Exception
        Dts.Events.FireError(-1, String.Empty, ex.ToString(), String.Empty, 0)
        Dts.TaskResult = ScriptResults.Failure
    Finally
        varCollection.Unlock()
        ftpConnection = Nothing
        ftpManager = Nothing
    End Try

    Dts.TaskResult = ScriptResults.Success
End Sub