【发布时间】:2015-07-14 10:24:18
【问题描述】:
我使用Microsoft Office 12.0 Access Database Engine OLE DB Provider 作为 Excel 架构创建了一个连接,以循环遍历 Excel 文件中的所有工作表,如本问题 How to loop through Excel files and load them into a database using SSIS package? 中所示
并使用Foreach ADO.NET Schema Rowset Enumerator 循环遍历 excel 文件。
现在一切正常,但是从 Excel 导入数据后,我想将该文件移动到存档文件夹。并尝试使用File System Task,但我得到的错误是
[文件系统任务] 错误:出现错误并显示以下错误消息:“该进程无法访问该文件,因为它正被另一个进程使用。”。
我还尝试了来自 link 的脚本任务。但是我遇到了一些错误并且无法解决错误,因为我对 C# 的了解为零。
以下是我尝试使用脚本任务移动文件时遇到的错误。
在 System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor) 在 System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(对象 obj,对象 [] 参数,对象 [] 参数) 在 System.Reflection.RuntimeMethodInfo.Invoke(Object obj,BindingFlags invokeAttr,Binder binder,Object[] 参数,CultureInfo 文化) 在 System.RuntimeType.InvokeMember(字符串名称、BindingFlags bindingFlags、Binder binder、Object 目标、Object[] providedArgs、ParameterModifier[] 修饰符、CultureInfo 文化、String[] namedParams) 在 Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTATaskScriptingEngine.ExecuteScript()。
更新:
这是我尝试移动文件的完整代码。
如果我在enum ScriptResults 添加断点,我不会弹出该弹出窗口并且任务成功完成并且文件也被移动到存档,但是如果我不在 C# 代码中添加任何断点,我得到了那个pop,文件没有被移到存档中。
#region Namespaces
using System;
using System.Data;
using Microsoft.SqlServer.Dts.Runtime;
using System.Windows.Forms;
using System.IO;
#endregion
namespace ST_9fc6ad7db45c4a7bb49f303680f789ef
{
[Microsoft.SqlServer.Dts.Tasks.ScriptTask.SSISScriptTaskEntryPointAttribute]
public partial class ScriptMain : Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase
{
public void Main()
{
DirectoryInfo di = new DirectoryInfo(Dts.Variables["SplitSkill_FolderPath"].Value.ToString());
FileInfo[] fi = di.GetFiles("*.xlsx");
String filename = fi[0].Name;
string sourceFileName = filename;
string destinationFile = @"D:\Flipkart\Data\Split Skill\Archive\" + sourceFileName;
string sourceFile = @"D:\Flipkart\Data\Split Skill\" + sourceFileName;
if (File.Exists(destinationFile))
File.Delete(destinationFile);
// To move a file or folder to a new location:
System.IO.File.Move(sourceFile, destinationFile);
Dts.TaskResult = (int)ScriptResults.Success;
}
#region ScriptResults declaration
enum ScriptResults
{
Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success,
Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure
};
#endregion
}
}
【问题讨论】:
-
显然文件最常被其他进程打开或访问。确保在导入后释放文件,然后再尝试存档。
-
如何发布?我在谷歌上试过,我找不到任何东西。
-
请出示您的 C# 代码。
-
来自这个链接 - bicortex.com/… 我正在使用 C# 代码。
-
您从脚本任务中得到什么错误?
标签: c# sql-server excel ssis