gpt4 book ai didi

c# - SSIS 脚本任务抛出错误

转载 作者:行者123 更新时间:2023-11-30 17:32:58 25 4
gpt4 key购买 nike

我正在努力将 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(文件名))I am receiving the following error.

enter image description here

以下是异常详细信息:

System.ArgumentException was unhandled by user code HResult=-2147024809 Message=Illegal characters in path. Source=mscorlib StackTrace:

at System.Security.Permissions.FileIOPermission.EmulateFileIOPermissionChecks(String fullPath) at 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) at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost) at System.IO.StreamWriter.CreateFile(String path, Boolean append, Boolean checkHost) at System.IO.StreamWriter..ctor(String path, Boolean append, Encoding encoding, Int32 bufferSize, Boolean checkHost) at System.IO.StreamWriter..ctor(String path, Boolean append) at System.IO.File.AppendText(String path) at ST_9752d9eb585d4a4d97a334ef01ccf313.ScriptMain.Main() in c:\Users\aluhman\AppData\Local\Temp\2\Vsta\5c1672d48682401d852b1b44649f951b\ScriptMain.cs:line 31 InnerException:

这在 2005 年一切正常,但这是我现在在 2016 年看到的一个新错误。

最佳答案

您不能一次打开每个文件,而是必须一个一个地遍历它们:

类似于:

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

关于c# - SSIS 脚本任务抛出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45219936/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com