ssis 角本组件更新数据

介绍 (Introduction)

SSIS Script component is one data transformation tasks in SQL Server Integration Services (SSIS). SSIS is an integration tool in the Microsoft BI family to extract data from heterogeneous data sources and transform it to your need. Apart from the standard data sources such as databases, text files, excel files, and web services, there can be instances where you need to retrieve non-traditional data sources. For example, let us say you want to extract the details of text files such as file sizes, created date, etc. In these types of scenarios, traditional data sources cannot be used.

SSIS脚本组件是SQL Server集成服务(SSIS)中的一项数据转换任务。 SSIS是Microsoft BI家族中的集成工具,可以从异构数据源中提取数据并将其转换为您的需求。 除了标准数据源(例如数据库,文本文件,excel文件和Web服务)外,在某些情况下您还需要检索非传统数据源。 例如,假设您要提取文本文件的详细信息,例如文件大小,创建日期等。在这些类型的方案中,不能使用传统的数据源。

This article explains how the SSIS Script component can be used as a data source in such instances. In simple terms, this component will use the functions and commands of the C# or VB.Net languages.

本文介绍了在这种情况下如何将SSIS脚本组件用作数据源。 简单来说,该组件将使用C#或VB.Net语言的功能和命令。

Before we start the discussion, it is important to note that there are two script related components in SSIS. One is the Script Task in the control flow, whereas the other is the script component in the Data flow task. We will be discussing the script component data flow task in this article.

在开始讨论之前,重要的是要注意SSIS中有两个与脚本相关的组件。 一个是控制流中的脚本任务 ,而另一个是数据流任务中的脚本组件。 我们将在本文中讨论脚本组件数据流任务。

Let us look at how the SSIS Script component can be used with a real-world example. Let us say we have a set of different files in multiple folders. Now we need to capture data such as file name, size, created date, and other details, as shown in the below screenshot.

让我们看看如何将SSIS脚本组件与实际示例结合使用。 假设我们在多个文件夹中有一组不同的文件。 现在,我们需要捕获数据,例如文件名,大小,创建日期和其他详细信息,如下面的屏幕快照所示。

Let us start the SSIS solution and drag and drop a data flow task to the control flow, and your screen should look like below.

让我们启动SSIS解决方案并将数据流任务拖放到控制流中,您的屏幕应如下图所示。

Next, double click the data flow task and drag and drop a script component flow to the data flow task. As soon as you drag and drop the script component, the following screen will appear for you to choose.

接下来,双击数据流任务并将脚本组件流拖放到数据流任务。 拖放脚本组件后,将出现以下屏幕供您选择。

This will decide what the type of the script component is. In this example, we will be using a Source Script Component Type. It is important to note that this configuration cannot be changed later. If you need to change the script component type, you need to drag and drop another component and reconfigure again. Therefore, before configuring the script component type, make sure you are selecting the correct configuration as per your requirement.

这将决定脚本组件的类型。 在此示例中,我们将使用源脚本组件类型。 重要的是要注意此配置以后不能更改。 如果需要更改脚本组件类型,则需要拖放另一个组件,然后重新配置。 因此,在配置脚本组件类型之前,请确保根据需要选择正确的配置。

Let us see the configuration of the SSIS Script Component as a data source. First, we will configure the Script tab.

让我们将SSIS脚本组件的配置视为数据源。 首先,我们将配置“ 脚本”选项卡。

We will be using Microsoft Visual C# 2017 as the scripting language, whereas you have the option of writing the script in Visual VB Script as well. However, once you start to write the script, you are not allowed to change the script language.

我们将使用Microsoft Visual C#2017作为脚本语言,而您也可以选择使用Visual VB脚本编写脚本。 但是,一旦开始编写脚本,就不允许更改脚本语言。

You can pass the read-only variable into the script so that there is higher maintainability. For example, we can configure the file path into an SSIS variable, and that can be modified from the package. If not, this has to be hardcoded into the script component that will become difficult to manage. This SSIS variable is configured in ReadOnlyVariables, as shown in the above screenshot. Further, you can use the ReadWriteVariables to write a value that was generated inside the script component. We will not be using the ReadWriteVariables option as it is not relevant to the example that we are discussing.

您可以将只读变量传递到脚本中,以便具有更高的可维护性。 例如,我们可以将文件路径配置为SSIS变量,并且可以从包中对其进行修改。 如果不是,则必须将其硬编码到脚本组件中,这将变得难以管理。 如上面的屏幕快照所示,在ReadOnlyVariables中配置了此SSIS变量。 此外,您可以使用ReadWriteVariables写入在脚本组件内部生成的值。 我们将不使用ReadWriteVariables选项,因为它与我们正在讨论的示例无关。

Next, we will configure the Inputs and Outputs tabs. Since the SSIS Script Component is configured as a source, we will have only output to be configured.

接下来,我们将配置“ 输入和输出”选项卡。 由于SSIS脚本组件被配置为源,因此我们只有要配置的输出。

In this configuration, output columns are defined along with the data type and data length. The following table shows the data type and length for each output column.

在此配置中,将定义输出列以及数据类型和数据长度。 下表显示了每个输出列的数据类型和长度。

Output Column Name

Data Type

Length

FileName

String

1,500

FileType

String

50

DateModifed

Database timestamp

IsReadOnly

Boolean

Size

Eight bytes signed integer

DateCreated

Database timestamp

输出列名称

数据类型

长度

文档名称

1,500

文件类型

50

修改日期

数据库时间戳

IsReadOnly

布尔型

尺寸

八字节有符号整数

创建日期

数据库时间戳

Next is to create the script of the SSIS script component, which is the important configuration. By clicking the Edit Script button, you will be taken to the script editor, which is the visual studio editor. The following code is included.

接下来是创建SSIS脚本组件的脚本,这是重要的配置。 通过单击“ 编辑脚本”按钮,您将进入脚本编辑器,即Visual Studio编辑器。 包含以下代码。

public override void CreateNewOutputRows(){// Lock the variable for writeVariableDispenser variableDispenser = (VariableDispenser)this.VariableDispenser;variableDispenser.LockForRead("User::FilePath");IDTSVariables100 vars;variableDispenser.GetVariables(out vars);string filePath = vars["User::FilePath"].Value.ToString();// Unlock the variablevars.Unlock();foreach (string file in Directory.EnumerateFiles(filePath,"*.*",SearchOption.AllDirectories)){FileInfo oFileInfo = new FileInfo(file);OutputBuffer.AddRow();OutputBuffer.FileName = oFileInfo.FullName;OutputBuffer.DateCreated = oFileInfo.CreationTime;OutputBuffer.DateModified = oFileInfo.LastWriteTime;OutputBuffer.FileType = oFileInfo.Extension;OutputBuffer.Size = oFileInfo.Length; OutputBuffer.IsReadOnly = oFileInfo.IsReadOnly;}}

In the above script, FilePath is the SSIS variable that is used to store the file path. To implement the recursive search of files, SearchOption.AllDirectories is selected. OutputBuffer is the buffer that will be used to add the records for the source. Since this C#, you have the luxury of using many C# language features.

在上面的脚本中,FilePath是SSIS变量,用于存储文件路径。 要实现文件的递归搜索, 选择SearchOption.AllDirectories 。 OutputBuffer是将用于添加源记录的缓冲区。 从此C#开始,您可以奢侈地使用许多C#语言功能。

Now let us create a table as shown in the below script, and data will be written to the database after the package is executed.

现在,让我们创建一个表,如下面的脚本所示,执行包后,数据将被写入数据库。

CREATE TABLE [FileList] ([FileName] varchar(1500),[FileType] varchar(50),[DateModified] datetime,[IsReadOnly] bit,[Size] bigint,[DateCreated] datetime
)

Following is the file SSIS data flow after SQL Server destination is included.

以下是包含SQL Server目标之后的文件SSIS数据流。

When the package is executed, data will be stored into the FileList table, as shown in the below screenshot.

执行该软件包时,数据将存储到FileList表中,如下面的屏幕快照所示。

SSIS脚本组件中的其他配置 (Additional Configurations in SSIS Script Components)

After the script component is configured, typical data flow tasks can be used. For example, let us say we want to split the data set, depending on the size of the file.

配置脚本组件之后,可以使用典型的数据流任务。 例如,假设我们要根据文件大小拆分数据集。

Let us use SSIS Conditional Split to split the above data stream.

让我们使用SSIS条件拆分来拆分上述数据流。

The Conditional Split data flow task can be configured, as shown in the following screenshot.

可以配置条件拆分数据流任务,如以下屏幕截图所示。

As shown in the above screen, files that have a size of more than 2KB will be sent to one output, and the rest of the data will be into another output name.

如上面的屏幕所示,文件大小超过2KB的文件将被发送到一个输出,其余数据将被保存到另一个输出名称中。

Following is the SSIS data flow task after the inclusion of Conditional Split.

以下是包含条件拆分后的SSIS数据流任务。

In this configuration, both Morethan2KB and LessThan2KB output have the same attributes.

在此配置中,Morethan2KB和LessThan2KB输出具有相同的属性。

Similarly, all the data flow tasks can be used to the Output of the script component task.

同样,所有数据流任务都可以用于脚本组件任务的输出。

SSIS脚本组件中的多个输出路径 (Multiple Output paths in the SSIS Script Component)

The above implementation can be done differently inside the script component by defining multiple outputs.

通过定义多个输出,可以在脚本组件内部以不同方式完成上述实现。

Following is the way to configure multiple outputs in the SSIS script component.

以下是在SSIS脚本组件中配置多个输出的方法。

In the above configuration, two outputs, FileSizeLess2KB and FileSizeMore2KB, were added with the same columns and the same data types.

在以上配置中,两个输出FileSizeLess2KBFileSizeMore2KB被添加了相同的列和相同的数据类型。

Next, we will be adding the script as shown below:

接下来,我们将添加脚本,如下所示:

foreach (string file in Directory.EnumerateFiles(filePath,"*.*",SearchOption.AllDirectories)){FileInfo oFileInfo = new FileInfo(file);if (oFileInfo.Length > 2048){FileSizeMore2KBBuffer.AddRow();FileSizeMore2KBBuffer.FileName = oFileInfo.FullName;FileSizeMore2KBBuffer.DateCreated = oFileInfo.CreationTime;FileSizeMore2KBBuffer.DateModified = oFileInfo.LastWriteTime;FileSizeMore2KBBuffer.FileType = oFileInfo.Extension;FileSizeMore2KBBuffer.Size = oFileInfo.Length;FileSizeMore2KBBuffer.IsReadOnly = oFileInfo.IsReadOnly; }else{FileSizeLess2KBBuffer.AddRow();FileSizeLess2KBBuffer.FileName = oFileInfo.FullName;FileSizeLess2KBBuffer.DateCreated = oFileInfo.CreationTime;FileSizeLess2KBBuffer.DateModified = oFileInfo.LastWriteTime;FileSizeLess2KBBuffer.FileType = oFileInfo.Extension;FileSizeLess2KBBuffer.Size = oFileInfo.Length;FileSizeLess2KBBuffer.IsReadOnly = oFileInfo.IsReadOnly;}}

In the above script, two buffers were added, depending on the size of the file. After both outputs were added, there will be two outputs from the SSIS Script component, as shown in the bellow SSIS package.

在上面的脚本中,根据文件的大小添加了两个缓冲区。 添加两个输出之后,SSIS脚本组件将有两个输出,如下面的SSIS包所示。

Similar to the SSIS conditional split, you will see the same numbers for the two outputs. Similar to the above example, there are similar instances where traditional data sources cannot be used.

与SSIS条件拆分类似,两个输出将看到相同的数字。 与以上示例相似,在某些相似的情况下,无法使用传统数据源。

In the script component, you have the ability to create two different outputs with different attributes, which was not possible with the SSIS Conditional Split data flow task. For example, for the image files, you might need width and height that are not required for the other files. In this example, we can add height and width attributes to the required output buffers.

在脚本组件中,您可以创建两个具有不同属性的不同输出,而这对于SSIS条件拆分数据流任务是不可能的。 例如,对于图像文件,您可能需要其他文件不需要的宽度和高度。 在此示例中,我们可以将height和width属性添加到所需的输出缓冲区。

However, during the development of the SSIS script component, it is important to make a copy of the script as a backup option.

但是,在开发SSIS脚本组件期间,重要的是制作一个脚本副本作为备份选项。

结论 (Conclusion)

In this article, we discussed the use of the SSIS Script component in order to generate non-traditional data sources apart from the traditional data sources. Since C# and vb.net has a rich set of commands, developers can use it for different purposes. This script component can be used to generate multiple outputs. In these multiple outputs, you can add different parameters to the different buffer outputs so that users have higher flexibility that the SSIS Conditional split.

在本文中,我们讨论了SSIS脚本组件的使用,以生成除传统数据源之外的非传统数据源。 由于C#和vb.net具有丰富的命令集,因此开发人员可以将其用于不同的目的。 该脚本组件可用于生成多个输出。 在这些多个输出中,可以将不同的参数添加到不同的缓冲区输出中,从而使用户比SSIS条件拆分具有更高的灵活性。

翻译自: https://www.sqlshack.com/using-the-ssis-script-component-as-a-data-source/

ssis 角本组件更新数据

ssis 角本组件更新数据_使用SSIS脚本组件作为数据源相关推荐

  1. 子组件向父组件传递数据_如何将元素引用向下传递到角度的组件树中

    子组件向父组件传递数据 I recently had a requirement to make sure a PrimeNG Dropdown panel inside a modal with s ...

  2. uniapp 子组件 props拿不到数据_总结下React组件间的通讯

    这是个老话题了. 随着组件化开发成为主流,每个组件都有完善的生命周期,大家可以在生命周期内做一些事情,每个组件有自己的状态管理机制.同时,每个组件都是独立的.这能提高大家的开发效率,规范化开发. 今天 ...

  3. datagridview实时更新数据_旭诺云盒|智能办公新趋势进出口数据自动提取,通关状态实时更新...

    春节期间,很多公司同事都被滞留在老家无法返回公司上班,为了保证公司业务正常运转,同事之间依靠邮件.微信.QQ等工具进行文件和数据的传递,增加了很多数据整理时间.且电子口岸.单一窗口这些进出口企业频繁使 ...

  4. asscess 一条记录更新数据_一条MySQL更新语句是怎么执行的?

    流程图 这是在网上找到的一张流程图,写的比较好,大家可以先看图,然后看详细阅读下面的各个步骤. 执行流程: 1.连接验证及解析 客户端与MySQL Server建立连接,发送语句给MySQL Serv ...

  5. ionic 修改组件默认样式_开源Magpie:组件库详解

    开源项目专题系列(八)1.开源项目名称:magpie_fly2.github地址: https://github.com/wuba/magpie_fly 3.简介:magpie_fly 是58集体出品 ...

  6. 02-React受控组件及非受控组件、数据渲染、事件处理、组件通信

    一.受控组件和非受控组件 React组件的数据渲染是否被调用 是通过 传递过来的props完全控制 控制则为受控组件,否则非受控组件. 二.数据渲染 1.条件渲染 { flag ? "开启& ...

  7. 一、vue中当数据在父组件需要给子组件传递数据的时候,子组件输出为undefined

    父组件: <Type :imgList="imgList"/> 子组件: <img :src="imgList[0].imgUrl"/> ...

  8. echarts实时更新数据_虎牙为S10拼了8.0年度更新!随时回放实时数据,还能养柴犬...

    S10总决赛马上就要来临了,对于广大玩家来说,这真的是每年一度的狂欢盛宴.世界各地二十多支顶尖强队汇聚中国上海,直至决出最强者方才罢休. 作为英雄联盟中的世界杯,各大平台也都十分重视S10.尤其是虎牙 ...

  9. oracle主从表分离怎么实时更新数据_高可用数据库主从复制延时的解决方案

    MySQL主从复制的延时一直是业界困扰已久的问题.延时的出现会降低主从读写分离的价值,不利于数据实时性较高的业务使用MySQL. UDB是UCloud推出的云数据库服务,上线已达六年,运营了数以万计的 ...

最新文章

  1. 数据中台应用实战50篇(一)-企业级数据中台的建设方法架构和技术栈
  2. 一文读懂 Spring Boot、微服务架构和大数据治理三者之间的故事
  3. IDEA中新建SpringBoot项目时提示:Artifact contains illegal characters
  4. Silverlight 解谜游戏 之十四 音效
  5. “智物 智造”亮相云栖大会 远程操控工厂震撼骨灰级程序员
  6. 计算机网络实验报告嗅探器,计算机网络实验(Wireshark)
  7. python文件移动到文件夹_python – 将文件夹中的文件移动到顶级目录
  8. python中__new__的用法_python中的__init__ 、__new__、__call__小结及使用
  9. python源码中的学习笔记_第10章_面向对象的特征、特殊方法与属性、以及深浅拷贝
  10. 道德如果一直在滑坡,终会酿成大祸,历朝历代,莫过于是……
  11. 浅谈MMORPG服务器架构
  12. 自动驾驶(三十)---------驾驶员监测
  13. BUUCTF misc 九连环隐写
  14. VIC Image 驱动程序
  15. #10049. 「一本通 2.3 例 1」Phone List(trie树应用)
  16. 教程-OBS刷课(岗位证)
  17. 直方图匹配原理 MALAB实现
  18. 【Android Assimp】免积分 一步编译assimp(自己整合的编译工具)(内含已编译so库下载)
  19. Julia 语言初学笔记
  20. CCV入门教程(三)

热门文章

  1. c++ 中文 base64加密_这次有点骚了,破解安卓某 APP 低层加密
  2. 避免踩坑:易盾安全老司机起底Android九大漏洞,附解决建议
  3. Java操作Hive
  4. 数据库高可用 MHA 搭建手册
  5. Numpy中的时间类型
  6. [js]DOM 篇
  7. php 抓取天气情况 www.weather.com.cn
  8. NSUserDefaults 、对象归档
  9. shell脚本执行时报bad interpreter: Text file busy的解决方法
  10. DB2 V9 默认帐户信息和服务启动信息