帮助SSIS包

时间:2011-09-06 10:20:46

标签: sql-server sql-server-2008 ssis

大家好,我有一个复杂的SSIS包,但是我正撞在一个有特定部分的砖墙上。我有一个维护部分,将删除超过3个月(从今天的日期)的文件。这些文件都有文件名中的日期,例如AA-CDR- 20110606030000 -2-001A648E6F74-026874.xml

所以我编写了一个任务,它将使用Foreach循环遍历特定文件夹中的所有文件,然后我有一个脚本将使用foreach循环使用变量集加载文件名。然后使用下面的脚本删除该文件。这在调试模式下运行时工作正常。但是,当尝试在服务器上执行此操作时,我遇到了“System.FormatException:String未被识别为有效的DateTime”的失败。我真的不明白为什么,任何想法?

DateTime deleteDate = DateTime.Today.AddMonths(-3);

String file = Dts.Variables["FileName"].Value.ToString();
String fileDateString = file.Substring(42, 8);
String fileDateYear = fileDateString.Substring(0, 4);
String fileDateMonth = fileDateString.Substring(4, 2);
String fileDateDay = fileDateString.Substring(6, 2);

DateTime fileDateTime = Convert.ToDateTime(fileDateDay + "-" + fileDateMonth + "-" + fileDateYear);

if (fileDateTime < deleteDate)
{
  System.IO.File.Delete(file);
}

1 个答案:

答案 0 :(得分:1)

生产服务器上似乎有一个文件没有遵循该模式,并且无法从其名称中提取日期。

尝试使用以下内容:

try
{
    DateTime fileDateTime = Convert.ToDateTime(fileDateDay + "-" + fileDateMonth + "-" + fileDateYear);

    if (fileDateTime < deleteDate)
    {
        System.IO.File.Delete(file);
    }
}
catch (System.FormatException)
{
    // log the Dts.Variables["FileName"] value here to see why it could not be parsed
}