【问题标题】:Help with SSIS PackageSSIS 包的帮助
【发布时间】:2011-11-11 05:04:41
【问题描述】:

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

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

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);
}

【问题讨论】:

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


    【解决方案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
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-10-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-26
      • 2011-07-12
      • 2010-12-20
      • 1970-01-01
      相关资源
      最近更新 更多