ssis - 将最新的csv文件导入到SSIS中的SQL Server

标签 ssis

我有一个文件夹,其中每半小时都会收到带有时间戳的.csv文件。现在,我需要从可用文件中获取最新文件,并将其导入sql server。

例如

在我的源文件夹中,我有

test_01112012_120122.csv
test_01112012_123022.csv
test_01112012_123555.csv

现在,我需要获取最新文件,并在SSIS的帮助下将该文件导入sql服务器。

谢谢
讽刺的

最佳答案

即使您使用SSIS作为导入工具,也将需要@garry Vass或类似的代码。

在SSIS中,您需要将连接字符串更新为平面文件连接管理器,以指向新文件。如此,您需要确定什么是最新文件。

查找最新文件

是否通过文件属性(加里的代码)或文件名的切片和切分将取决于您的业务规则。它始终是最新修改的文​​件(属性),还是需要基于被解释为序列的文件名?如果test_01112012_120122.csv出错并更新了内容,则这很重要。修改日期将更改,但文件名不会更改,并且这些更改不会移植回数据库。

我建议您创建2个类型为String的变量,范围为名为RootFolderCurrentFile的包。 (可选)如果要限制为*.csv之类的特定类型,则可以创建一个称为FileMask的文件。 RootFolder将是您希望在C:\ssisdata\MyProject中查找文件的基本文件夹。从完全限定路径的脚本到最新修改的文​​件的脚本中,将为CurrentFile分配一个值。在这一点上,我发现将设计时值分配给CurrentFile(通常分配给集合中最旧的文件)会很有帮助。

将一个脚本任务拖到控制流上,并设置为您的ReadOnlyVariable User::RootFolder(可选的是User::FileMask)。您的ReadWriteVariable将为User::CurrentFile。

该脚本将放在public partial class ScriptMain: ...大括号内

    /// <summary>
    /// This verbose script identifies the most recently modified file of type fileMask
    /// living in RootFolder and assigns that to a DTS level variable.
    /// </summary>
    public void Main()
    {
        string fileMask = "*.csv";
        string mostRecentFile = string.Empty;
        string rootFolder = string.Empty;

        // Assign values from the DTS variables collection.
        // This is case sensitive. User:: is not required
        // but you must convert it from the Object type to a strong type
        rootFolder = Dts.Variables["User::RootFolder"].Value.ToString();

        // Repeat the above pattern to assign a value to fileMask if you wish
        // to make it a more flexible approach

        // Determine the most recent file, this could be null
        System.IO.FileInfo candidate = ScriptMain.GetLatestFile(rootFolder, fileMask);

        if (candidate != null)
        {
            mostRecentFile = candidate.FullName;
        }

        // Push the results back onto the variable
        Dts.Variables["CurrentFile"].Value = mostRecentFile;

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

    /// <summary>
    /// Find the most recent file matching a pattern
    /// </summary>
    /// <param name="directoryName">Folder to begin searching in</param>
    /// <param name="fileExtension">Extension to search, e.g. *.csv</param>
    /// <returns></returns>
    private static System.IO.FileInfo GetLatestFile(string directoryName, string fileExtension)
    {
        System.IO.DirectoryInfo directoryInfo = new System.IO.DirectoryInfo(directoryName);

        System.IO.FileInfo mostRecent = null;

        // Change the SearchOption to AllDirectories if you need to search subfolders
        System.IO.FileInfo[] legacyArray = directoryInfo.GetFiles(fileExtension, System.IO.SearchOption.TopDirectoryOnly);
        foreach (System.IO.FileInfo current in legacyArray)
        {
            if (mostRecent == null)
            {
                mostRecent = current;
            }

            if (current.LastWriteTimeUtc >= mostRecent.LastWriteTimeUtc)
            {
                mostRecent = current;
            }
        }

        return mostRecent;

        // To make the below code work, you'd need to edit the properties of the project
        // change the TargetFramework to probably 3.5 or 4. Not sure
        // Current error is the OrderByDescending doesn't exist for 2.0 framework
        //return directoryInfo.GetFiles(fileExtension)
        //     .OrderByDescending(q => q.LastWriteTimeUtc)
        //     .FirstOrDefault();
    }

    #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

}

更新连接管理器

至此,我们的脚本已为CurrentFile变量分配了一个值。下一步是告诉SSIS我们需要使用该文件。在CSV的连接管理器中,您需要为ConnectionString设置一个Expression(F4或右键单击并选择Properties)。您要分配的值是CurrentFile变量,表达方式是@[User::CurrentFile]
最后,这些屏幕截图基于即将发布的SQL Server 2012,因此图标可能看起来有所不同,但功能保持不变。

关于ssis - 将最新的csv文件导入到SSIS中的SQL Server,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8831060/

相关文章:

sql-server - VS 2017 无法部署 SSIS SQL Server 2012

SQL-SSIS。在表中查找缺失的数字

c# - 使用 SSIS 使用合并列取消透视数据 excel

excel - 在 SSIS 中运行脚本以修改 excel 文件时出现错误 :The server threw an exception.(来自 HRESULT 的异常:0x80010105 (RPC_E_SERVERFAULT))

ssis - 在 SSIS 中导入带有排序和连接的大型数据文件

sql-server - 如何获取解决方案中所有 SSIS 包的所有错误

ssis - 如何在SSIS中动态获取带时间戳的文件名

SSIS 从平面文件中获取数据并分配给变量

sql-server - 包部分控制流程中的动态连接

ssis - 我不在 SSIS 中使用临时表