1

我是 .Net 和 SQL 的狂热爱好者,我正在开发一个 SSIS 包,该包从平面文件中提取数据并将其输入到 SQL 表中。我需要帮助的部分是获取文件的修改日期并用它填充我在该表中创建的派生列。我创建了以下变量:DateTime 类型的 FileDate、String 的 FilePath 和 String 的 SourceFolder 用于文件的路径。我在想 DateModified 可以使用脚本组件填充到带有 DataFlow 的派生列中吗?有人可以告诉我我是否走在正确的轨道上吗?我很感激任何帮助。谢谢。

4

1 回答 1

2

A Derived Column Transformation can only work with Integration Services Expressions. A script task would allow you to access the .net libraries and you would want to use the method that @wil kindly posted or go with the static methods in System.IO.File

However, I don't believe you would want to do this in a Data Flow Task. SSIS would have to evaluate that code for every row that flows through from the file. On a semi-related note, you cannot write to a variable until the ... event is fired to signal the data flow has completed (I think it's OnPostExecute but don't quote me) so you wouldn't be able to use said variable in a downstream derived column at any rate. You would of course, just modify the data pipeline to inject the file modified date at that point.

What would be preferable and perhaps your intent is to use a Script Task prior to the Data Flow task to assign the value to your FileDate variable. Inside your Data Flow, then use a Derived Column to add the @FileDate variable into the pipeline.

// This code is approximate. It should work but it's only been parsed by my brain
// 
// Assumption: 
// SourceFolder looks like a path   x:\foo\bar 
// FilePath looks like a file name  blee.txt
// SourceFolder [\] FilePath is a file that the account running the package can access
// 
// Assign the last mod date to FileDate variable based on file system datetime
// Original code, minor flaws
// Dts.Variables["FileDate"].Value = File.GetLastWriteTime(System.IO.Path.Combine(Dts.Variables["SourceFolder"].Value,Dts.Variables["FilePath"].Value));
Dts.Variables["FileDate"].Value = System.IO.File.GetLastWriteTime(System.IO.Path.Combine(Dts.Variables["SourceFolder"].Value.ToString(), Dts.Variables["FilePath"].Value.ToString()));

Edit

I believe something is amiss with either your code or your variables. Do your values approximately line up with mine for FilePath and SourceFolder? Variables are case sensitive but I don't believe that to be your issue given the error you report.

This is the full script task and you can see by the screenshot below, the design-time value for FileDate is 2011-10-05 09:06 The run-time value (locals) is 2011-09-23 09:26:59 which is the last mod date for the c:\tmp\witadmin.txt file

using System;
using System.Data;
using Microsoft.SqlServer.Dts.Runtime;
using System.Windows.Forms;

namespace ST_f74347eb0ac14a048e9ba69c1b1e7513.csproj
{
    [System.AddIn.AddIn("ScriptMain", Version = "1.0", Publisher = "", Description = "")]
    public partial class ScriptMain : Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase
    {
        enum ScriptResults
        {
            Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success,
            Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure
        };

        public void Main()
        {
            Dts.Variables["FileDate"].Value = System.IO.File.GetLastWriteTime(System.IO.Path.Combine(Dts.Variables["SourceFolder"].Value.ToString(), Dts.Variables["FilePath"].Value.ToString()));
            Dts.TaskResult = (int)ScriptResults.Success;
        }
    }
}

enter image description here

C:\tmp>dir \tmp\witadmin.txt
 Volume in drive C is Local Disk
 Volume Serial Number is 3F21-8G22

 Directory of C:\tmp

09/23/2011  09:26 AM           670,303 witadmin.txt
于 2011-10-05T01:54:53.617 回答