我正在努力将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))
处断开。 I am receiving the following error.
以下是例外情况:
System.ArgumentException未被用户代码处理 的HResult = -2147024809 消息=路径中的非法字符。 来源= mscorlib程序 堆栈跟踪:
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) 在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(字符串路径) 位于c:\ Users \ aluhman \ AppData \ Local \ Temp \ 2 \ Vsta \ 5c1672d48682401d852b1b44649f951b \ ScriptMain.cs中的ST_9752d9eb585d4a4d97a334ef01ccf313.ScriptMain.Main():第31行 InnerException:
这一切都在2005年有效,这是我现在在2016年看到的一个新错误。
答案 0 :(得分: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();
}
}
}