SSIS脚本任务抛出的错误

3

我正在升级2005年的SSIS包到2016年。我已经将这个包升级了,但当我尝试运行它时,控制流中的脚本任务出现错误。

#region Namespaces
using System;
using System.Data;
using Microsoft.SqlServer.Dts.Runtime;
using System.Windows.Forms;
using System.IO;
#endregion

namespace ST_9752d9eb585d4a4d97a334ef01ccf313
{

[Microsoft.SqlServer.Dts.Tasks.ScriptTask.SSISScriptTaskEntryPointAttribute]
public partial class ScriptMain : Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase
{
    public void Main()
    {

        string fileName;

        fileName = Dts.Variables["User::Source_File_And_Path"].Value.ToString();

        using (StreamWriter w = File.AppendText(fileName))
        {
            w.Write("\r\n");
            w.Close();
        }

            Dts.TaskResult = (int)ScriptResults.Success;
    }

    #region ScriptResults declaration
    /// <summary>
    /// This enum provides a convenient shorthand within the scope of this class for setting the
    /// result of the script.
    /// 
    /// This code was generated automatically.
    /// </summary>
    enum ScriptResults
    {
        Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success,
        Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure
    };
    #endregion

}
}

这个脚本任务被用于向文件数据的末尾添加CRLF。我已经在代码中添加了断点,发现它在using (StreamWriter w = file.AppendText(fileName))这一行处出现问题。我收到了以下错误。

enter image description here

这里是异常详情:

用户代码处理 System.ArgumentException 未被处理 HResult=-2147024809 Message=路径中存在非法字符。 Source=mscorlib StackTrace: 在 System.Security.Permissions.FileIOPermission.EmulateFileIOPermissionChecks(String fullPath) 在 System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost) 在 System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost) 在 System.IO.StreamWriter.CreateFile(String path, Boolean append, Boolean checkHost) 在 System.IO.StreamWriter..ctor(String path, Boolean append, Encoding encoding, Int32 bufferSize, Boolean checkHost) 在 System.IO.StreamWriter..ctor(String path, Boolean append) 在 System.IO.File.AppendText(String path) 在 ST_9752d9eb585d4a4d97a334ef01ccf313.ScriptMain.Main() 位置:c:\Users\aluhman\AppData\Local\Temp\2\Vsta\5c1672d48682401d852b1b44649f951b\ScriptMain.cs 第 31 行 InnerException:

这些都是2005年就已经工作的,而这是我现在在2016年看到的一个新错误。


3
尝试将异常详细信息复制到剪贴板,然后粘贴到记事本中。如果出现错误,则可能是由于文件名包含文件名不允许的字符导致的。 - jdweng
请检查您的 fileName 文件名。 - Subbu
这是文件名"F:\Data_Imports\Company\Inbound\Customers_*.txt"。 - A. Luhman
1个回答

1
你不能一次性打开所有文件,而是必须逐个迭代它们:

类似这样:

string fileName = Dts.Variables["User::Source_File_And_Path"].Value.ToString();
string [] fileEntries = Directory.GetFiles(Path.GetFullPath(fileName));
foreach (string f in fileEntries)
{
     if (Path.GetExtension(f).ToUpper()==".TXT".ToUpper() && f.StartsWith("Customers_")==true)
     {
        using (StreamWriter w = File.AppendText(f))
            {
                w.Write("\r\n");
                w.Close();
            }
     }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接