本文将向您展示如何使用 SSIS 脚本组件作为目标,并提供一个实际示例。您还可以参考以下链接
在开始创建 SSIS 脚本组件作为目标包之前,让我们先看看我们要使用的数据表和数据。

配置 SSIS 脚本组件作为目标
步骤 1:将“数据流任务”从工具箱拖放到“控制流”区域。接下来,将其重命名为“SSIS 脚本组件作为目标”。

双击“数据流任务”以打开“数据流”选项卡。
步骤 2:将 OLE DB 源 ( OLE DB Source ) 从工具箱拖放到数据流区域。双击数据流区域中的 OLE DB 源将打开 OLE DB 连接管理器 ( OLE DB Connection Manager ) 设置。有关更多转换,请单击“此处” ( Click Here )。

从下面的屏幕截图中,您可以看到我们选择数据库中存在的 [MyEmployees] 表作为源表。

步骤 3:单击列选项卡以验证列。在此选项卡中,我们还可以取消勾选不需要的列。

步骤 4:将“脚本组件”从 SSIS ( SSIS ) 工具箱拖放到数据流区域。放置脚本组件后,将打开一个名为“选择脚本内容类型”的新弹出窗口,如下图所示。在这里,我们想演示 SSIS 脚本组件作为目标,因此我们选择“目标”选项。

接下来,将 OLE DB 源的输出箭头拖放到此脚本组件。

双击脚本组件将打开以下编辑器以配置属性。请参阅“ SSIS 脚本组件作为转换 ”以了解这些属性。

步骤 6:在“输入列”选项卡中,您可以仔细检查输入列。

步骤 7:在“连接管理器”选项卡中,您可以添加 OLE DB、ADO 或平面文件连接管理器,以便从脚本文件调用连接。在此示例中,我们将在 C# 脚本中直接提供文件路径。

步骤 8:在“脚本”选项卡中,请单击“编辑脚本...”按钮以编写实际的 C# 脚本。

单击“编辑脚本”后,它将打开 main.cs 类文件以编写 C# 代码。请在 Input0_ProcessInputRow(Input0Buffer Row) 函数中编写自定义代码。

步骤 9:在此处添加自定义代码。在此示例中,我们将源数据中的每一行和每一列读取到文本文件中。

我们在上面的 SSIS 脚本组件作为目标屏幕截图中使用的代码是
C# 代码
// Creating two private Variables
private StreamWriter textWriter;
private string columnDelimiter = ",";
/// This method is called once, before rows begin to be processed in the data flow.
///
/// You can remove this method if you don't need to do anything here.
public override void PreExecute()
{
base.PreExecute();
/* Establish the Connection before the data flow begins.
If you place this code inside the Input0_ProcessInputRow() then it will
an error
*/
textWriter = new StreamWriter(@"D:\FILE EXAMPLES\Write.txt", false);
}
/// This method is called after all the rows have passed through this component.
///
/// You can delete this method if you don't need to do anything here.
public override void PostExecute()
{
base.PostExecute();
// Closing the Connection.
textWriter.Close(); // It is always advisable to close the Connection
}
/// This method is called once for every row that passes through the component from Input0.
///
/// Example of reading a value from a column in the the row:
/// string zipCode = Row.ZipCode
///
/// Example of writing a value to a column in the row:
/// Row.ZipCode = zipCode
public override void Input0_ProcessInputRow(Input0Buffer Row)
{
textWriter.Write(Row.FirstName);
textWriter.Write(columnDelimiter);
textWriter.Write(Row.LastName);
textWriter.Write(columnDelimiter);
textWriter.Write(Row.Education);
textWriter.Write(columnDelimiter);
textWriter.Write(Row.Occupation);
textWriter.Write(columnDelimiter);
textWriter.Write(Row.YearlyIncome);
textWriter.Write(columnDelimiter);
textWriter.Write(Row.Sales);
textWriter.WriteLine();
}
我建议您也阅读注释。完成脚本编辑后,请关闭 main.cs 文件,然后单击“确定”以完成包的创建。
步骤 10:右键单击“解决方案资源管理器”中的“SSIS 脚本组件作为目标”包,然后选择“执行包”。

从上面的屏幕截图中,您可以看到我们的“SSIS 脚本组件作为目标”包已成功执行。让我们打开文本文件,看看数据是否已写入。

评论已关闭。