简体   繁体   English

如何从特定文件夹中选择动态文件,然后使用SSIS导出到SQL Server

[英]How to pick dynamic files from a specific folder and then export to SQL server using SSIS

I have been trying to create an SSIS task which picks the MS Access file from a specific folder and then export to SQL Server ( if that file/table found in server then skip else export). 我一直在尝试创建一个SSIS任务,该任务从特定文件夹中选择MS Access文件,然后导出到SQL Server(如果在服务器中找到该文件/表,则跳过其他导出)。

I am new to SSIS, i have used script task to select the file names dynamically and then trying to move, but I end up getting unsatisfied results . 我是SSIS的新手,我使用脚本任务动态选择文件名,然后尝试移动,但是最终结果不令人满意。 Even I have googled and got few ideas, but still not able to get it the way I wanted. 即使我已经搜索过并没有什么想法,但仍然无法以我想要的方式获得它。 Any detailed help would be very helpful. 任何详细的帮助将非常有帮助。

Note : Here, am not always sure about the filename from that folder(ie dynamic) 注意:这里,并非总是确定该文件夹中的文件名(即动态)

There are many options for dynamically selecting files. 动态选择文件有很多选项。 Since you're unsure about the filename, I'm assuming this is a parameter or variable. 由于您不确定文件名,因此我假设这是参数或变量。 The following is an example of checking a folder from a variable for the given file name and loading it to an SSIS object variable. 以下是从给定文件名的变量检查文件夹并将其加载到SSIS object变量的示例。 These files are then loaded into a SQL Server table using the Foreach Loop . 然后,使用Foreach Loop这些文件加载​​到SQL Server表中。 You mentioned files as opposed to a single file, so this example assumes that only part of the file name is passed in, such as would be the case if the date/UID was appended to the beginning or end of the file name. 您提到的是文件而不是单个文件,因此本示例假定仅传入文件名的一部分,例如将日期/ UID附加到文件名的开头或结尾的情况。

  • Add a Script Task, with the parameters/variables holding the file and folder name as ReadOnlyVariables and the object variable which will store the file names during execution as a ReadWriteVariable . 添加一个脚本任务,其参数/变量将文件和文件夹的名称保存为ReadOnlyVariables ,对象变量将在执行期间将文件名存储为ReadWriteVariable The code for this is at the end of this post. 此代码位于本文的结尾。 The string.IndexOf method is used to check for files containing the given text, with the StringComparison.CurrentCultureIgnoreCase parameter used to make this search case-insensitive. string.IndexOf方法用于检查包含给定文本的文件,并使用StringComparison.CurrentCultureIgnoreCase参数使该搜索不区分大小写。 This example uses a variable for the file path and a parameter for the file name (denoted by $Package in the parameter name). 本示例使用一个变量作为文件路径,并使用一个参数作为文件名(在参数名中由$Package表示)。
  • Add a Foreach Loop of the Foreach From Variable Enumerator Enumerator type. 添加一个Foreach Loop Foreach From Variable Enumerator Enumerator类型的Foreach Loop Add the object variable that was populated in the Script Task as the Variable on the Collection page. 将在脚本任务中填充的对象变量添加为“集合”页面上的变量。 On the Variable Mappings pane, add a string variable at index 0. This will need to be an empty string variable that will hold the name of each file. 在“变量映射”窗格上,在索引0处添加一个字符串变量。这将是一个空字符串变量,它将保存每个文件的名称。
  • Create a Flat File Connection Manager from an example data file. 从示例数据文件创建平面文件连接管理器。 Make sure that the column names and data types are appropriately configured. 确保正确配置了列名和数据类型。 To set the file name dynamically, choose the ConnectionString expression (click the ellipsis of the Expression property in the Properties window of the connection manager) and add the same string variable from the Mappings Pane of the Foreach Loop . 要动态设置文件名,请选择ConnectionString表达式(在连接管理器的Properties窗口中单击Expression属性的省略号),然后从Foreach Loop的Mappings窗格中添加相同的字符串变量。
  • Inside the Foreach Loop , add a Data Flow Task with a Flat File Source using the same connection manager. Foreach Loop内部,使用相同的连接管理器添加带有平面文件源的数据流任务。 Then add either an OLE DB or SQL Server Destination with your destination connection and connect the flat file source to this. 然后,使用目标连接添加OLE DB或SQL Server目标,并将平面文件源与此连接。 I've found SQL Server Destinations to perform better, but you'll want to verify this in your own environment before making the choice. 我发现SQL Server目标具有更好的性能,但是在选择之前,您需要在自己的环境中进行验证。 Choose the necessary table and map the columns from the flat file source accordingly. 选择必要的表,并相应地映射来自平面文件源的列。

     List<string> fileList = new List<string>(); //get files from input directory DirectoryInfo di = new DirectoryInfo(Dts.Variables["User::FilePathVariable"].Value.ToString()); foreach (FileInfo f in di.GetFiles()) { //check for files with name containing text if (f.Name.IndexOf(Dts.Variables["$Package::FileNameParameter"].Value.ToString(), 0, StringComparison.CurrentCultureIgnoreCase) >= 0) { fileList.Add(f.FullName); } } //populate object variable Dts.Variables["User::YourObjectVariable"].Value = fileList; 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用SSIS导出SQL Server中的多个文本文件 - Using SSIS to Export Multiple Text Files in SQL Server 使用SSIS将Sharepoint上的xml文件中的数据导出到sql服务器 - export data from a xml file on sharepoint to sql server using ssis 使用SSIS将数据从SQL Server导出到Excel - Export data from sql server to excel using SSIS 如何将数据从SQL表导出到SSIS中的多个excel文件? - How to export data from SQL table to multiple excel files in SSIS? 使用SSIS将图像从SQL Server表导出到文件夹不起作用 - Exporting images from a SQL Server Table to a Folder using SSIS not working SSIS 2012-使用执行SQL任务和ForEachLoop容器将xml文件从文件夹加载到SQL Server表COLUMN中 - SSIS 2012 - Load xml files from folder into a SQL Server table COLUMN, using Execute SQL Task and ForEachLoop Container SSIS:如何使用SSIS从文件夹加载名称不同的平面文件 - SSIS: How to Load differently named flat files from a folder using SSIS SQL Server / SSIS-从目录中删除文件 - SQL Server/SSIS - Deleting files from Directory 使用SSIS将数据从SQL Server导出到Oracle CRM按需 - Using SSIS to Export Data from SQL Server to Oracle CRM On-Demand 将数据从平面文件加载到 Sql 服务器表,并使用 SSIS 导出到 excel - Load data from flat file to Sql Server table and also export to excel using SSIS
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM