c# - 如何查看特定字符串的文本文件并将该字符串粘贴到 SSIS/C#/VB 中的变量中

标签 c# vb.net visual-studio ssis

我正在将一个旧软件重写成一个 SSIS 包。我有一个类似于此的文本文件:

junk
other junk
other junk2
Sent: Friday, July 14, 2017 1:56 PM
more junk
more junk2

我需要打开文件(路径和文件名是常量),查找“Sent:” 并复制所有内容直到新行(在本例中为“Friday, July 14, 2017 1:56 PM") 到一个变量中,稍后可以由 SSIS 包使用。我的理解是 SSIS 没有所需的组件,我需要一个脚本任务来执行此操作。请问有人可以帮我解决这个问题吗?

此外,由于我没有使用脚本任务/C#/VB 的经验,因此非常感谢详细的指南。

最佳答案

您需要一个Script Component 而不是Script Task 因为您要在 中使用Flat File Connection 获取数据数据流级别。在您的 Flat File Source 中只有一列并将其称为 Column0

然后添加一个Transformation类型的Script Component。首先转到 Input Columns 选项卡并选择 Column0。然后,您需要返回到 Script tap 并添加输出变量 OutputLine,作为读写变量,如下所示:

enter image description here

点击Edit Script按钮,有3种方法:PreExecutePostExecuteInput0_ProcessInputRow。将以下代码行添加到 PostExecuteInput0_ProcessInputRow:

public override void PostExecute()
{
    base.PostExecute();
    this.Variables.OutputLine = LineValue;
}

// Add this variable that will hold the desired line value.
string LineValue = "";

public override void Input0_ProcessInputRow(Input0Buffer Row)
{
    if (Row.Column0.StartsWith("Sent:"))
    {
        LineValue = Row.Column0.Substring(5);
    }

}

构建它,关闭它,点击OK,你就完成了。

关于c# - 如何查看特定字符串的文本文件并将该字符串粘贴到 SSIS/C#/VB 中的变量中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45168805/

相关文章:

c# - 带验证的 MVC 嵌套 View 模型

c# - 从 C# 休眠计算机

c# - 连接池工作正常吗?

visual-studio - Visual Studio : Run all tests in a single project?

Github for Windows 忽略 .gitignore Visual Studio

windows - MS Visual Studio 2012 可以与 Windows Vista 一起使用吗?

c# - 将 DataTable 中的列值从 byte[] 转换为 String

c# - Nancy 模型绑定(bind)在 Chrome、IE 中不起作用

vb.net - 使用 Linq VB.NET 选择组中的最大日期项目

vb.net - Visual Basic 中的泛型类型限制