你很幸运。 SSIS 不支持混合记录类型,但您可以摆脱它,因为您只有 1 个标题行。
我的实现看起来像是读取文件第一行的脚本任务和读取其余数据的数据流任务。
阅读第一行
这个很简单。创建一个 SSIS 变量,将其命名为字符串类型的 FirstLine。将该值作为读/写值传递给脚本任务。
使用此答案中的代码
Read only the first few lines of text from a file
现在您只需将line1 的值推送到我们的 SSIS 级别变量中。看起来像
Dts.Variables["User::FirstLine"].Value = line1;
这假设您希望将整行存储到 FirstLine 中。如果您需要将其分成单独的字段,那么您需要实现该逻辑。您没有提供有关如何将“Hamilton Beach 20150410 Sunny”划分为各个部分的指导,但上述逻辑是正确的。解析并分配到不同的 SSIS 级别变量。
我的具体实现创建了 3 个 SSIS 变量,都是字符串类型
- 用户::HeaderIHaveNoIdeaWhatThisIs
- 用户::HeaderObservationDate
- 用户::HeaderWeather
以下代码表示已经链接的内容
using System;
using System.Data;
using System.IO;
using Microsoft.SqlServer.Dts.Runtime;
using System.Windows.Forms;
namespace ST_7edd5e6df63a4837afac15b86c21d639.csproj
{
[System.AddIn.AddIn("ScriptMain", Version = "1.0", Publisher = "", Description = "")]
public partial class ScriptMain : Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase
{
#region VSTA generated code
enum ScriptResults
{
Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success,
Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure
};
#endregion
public void Main()
{
// User::HeaderIHaveNoIdeaWhatThisIs,User::HeaderObservationDate,User::HeaderWeather
// https://stackoverflow.com/questions/9439733/read-only-the-first-few-lines-of-text-from-a-file
string line1 = string.Empty;
using (StreamReader reader = new StreamReader(@"C:\ssisdata\so_29811494.txt"))
{
line1 = reader.ReadLine();
}
// Magic here to understand how to split this out. Assuming this is also fixed width
// Horrible, hard coded brittle approach taken
//Hamilton Beach 20150410 Sunny
string h1, h2, h3;
h1 = line1.Substring(0, 20).TrimEnd();
h2 = line1.Substring(20, 12).TrimEnd();
h3 = line1.Substring(32, line1.Length - 32);
Dts.Variables["User::HeaderIHaveNoIdeaWhatThisIs"].Value = h1;
Dts.Variables["User::HeaderObservationDate"].Value = h2;
Dts.Variables["User::HeaderWeather"].Value = h3;
Dts.TaskResult = (int)ScriptResults.Success;
}
}
}
读取其余数据
在您的平面文件连接管理器中,您希望将 Skip header rows 的值从 0 更改为 1。这表示在我们读取前 N 行之前不应开始验证数据和解析。像往常一样定义你的连接管理器。
将数据流任务连接到上述脚本任务。在数据流任务中,使用平面文件源并连接派生列组件。派生列组件是我们如何将 SSIS 变量中的值获取到数据流中的方式。添加一个名为HeaderColumn 的新列并使用类似@[User::FirstLine] 的表达式。
如果您注意到右侧的列指示DT_NTEXT 的数据类型,则可能与目标列定义不匹配。您可能需要对变量SUBSTRING(@[User::FirstLine], 1, 20) 进行子串化。这导致数据类型为 DT_WSTR,长度为 20。您的目标是使其与目标定义匹配。
您可能还需要将其设为 DT_STR 数据类型,而不是 DT_WSTR。在这种情况下,将显式转换添加到您的子字符串操作(DT_STR, 20, 1252)SUBSTRING(@[User::FirstLine], 1, 20)
源数据
我根据提供的数据定义了我的文件(单击问题上的编辑以获取定义而不去除空白)
Hamilton Beach 20150410 Sunny
Bob Male Blue Black
Bill Male BrownBrown
GeorgeMale GreenBlonde
JackieFemaleGreenBlack
Jill FemaleBlue Black