![](/img/trans.png)
[英]Exception has been thrown by the target of an invocation - SSIS Script Task Error
[英]Error being thrown by SSIS script task
我正在努力將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))
。 我收到以下錯誤。
以下是異常詳細信息:
用戶代碼未處理System.ArgumentException HResult = -2147024809消息=路徑中的非法字符。 Source = mscorlib StackTrace:
在system.Security.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, System.IO.FileStream..ctor中的String msgPath,Boolean bFromProxy,Boolean useLongPath,Boolean checkHost)(字符串路徑,FileMode模式,FileAccess訪問,FileShare共享,Int32 bufferSize,FileOptions選項,String msgPath,Boolean bFromProxy,Boolean useLongPath,Boolean checkHost)在System.IO上的System.IO.StreamWriter.CreateFile(String path,Boolean append,Boolean checkHost)處於System.IO的System.IO.StreamWriter..ctor(String path,Boolean append,Encoding encoding,Int32 bufferSize,Boolean checkHost)。 System.IO.File.AppendText(String path)的StreamWriter..ctor(String path,Boolean append)位於c:\\ Users \\ aluhman \\ AppData \\ Local \\ Temp \\ 2 \\ Vsta \\ 5c1672d48682401d8中的ST_9752d9eb585d4a4d97a334ef01ccf313.ScriptMain.Main() 52b1b44649f951b \\ ScriptMain.cs:第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();
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.