SSIS - 移动Excel文件OLEDB连接到存档 [英] SSIS - Move Excel File with OLEDB Connection to Archive

查看:762
本文介绍了SSIS - 移动Excel文件OLEDB连接到存档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建使用连接的Microsoft Office 12.0 Access数据库引擎OLE DB提供程序为Excel模式,通过以Excel文件中的所有表环路证明了这个问题如何通过Excel文件回路,并使用SSIS包加载到数据库?



和使用 ADO.NET Foreach源架构行集枚举器通过Excel文件循环。



一切工作正常,但是从Excel导入数据后,我就想来移动文件存档文件夹。并尝试用文件系统任务,但我得到的误差




[文件系统任务]错误:与以下错误消息时出现错误:因为它正由另一个进程使用该进程无法访问文件




和我也试图与脚本任务,从这个的链接。但我收到的一些错误,不能作为解决我已经有了C#零知识的错误。



下面是当我尝试,我得到了错误移动使用脚本任务的文件。




在System.RuntimeMethodHandle.InvokeMethod(对象的目标,对象[]参数,签名的签名,布尔构造函数)在System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(obj对象
,对象[]参数,对象[]参数)
在System.Reflection.RuntimeMethodInfo.Invoke(obj对象,的BindingFlags invokeAttr,粘结剂粘结,对象[]参数,CultureInfo的文化)
在System.RuntimeType.InvokeMember(字符串名称,的BindingFlags的BindingFlags,活页夹粘结剂,目标对象,对象[] providedArgs,ParameterModifier []修饰符,CultureInfo的文化的String [] namedParams)
在Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTATaskScriptingEngine.ExecuteScript()。







更新



下面是我完整的代码与我试图移动文件



如果我在枚举ScriptResults 添加断点,我没有得到弹出,并得到圆满地完成了任务,该文件也被移到存档,但如果我没有在C#代码添加任何断点,我得到了流行音乐和文件不会移动到存档。

 #地区的命名空间使用系统
;
使用System.Data这;
使用Microsoft.SqlServer.Dts.Runtime;使用System.Windows.Forms的
;
:使用System.IO;
#endregion

命名空间ST_9fc6ad7db45c4a7bb49f303680f789ef
{
[Microsoft.SqlServer.Dts.Tasks.ScriptTask.SSISScriptTaskEntryPointAttribute]
公共部分类ScriptMain:Microsoft.SqlServer 。.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase
{
公共无效的主要()
{
DirectoryInfo的DI =新DirectoryInfo的(Dts.Variables [SplitSkill_FolderPath] Value.ToString( ));

的FileInfo [] F1 = di.GetFiles(* XLSX。);
字符串文件名=网络连接[0] .Name点;

串sourceFileName =文件名;
串destinationFile = @D:\Flipkart\Data\Split Skill\Archive\+ sourceFileName;
串的SourceFile = @D:\Flipkart\Data\Split Skill\+ sourceFileName;

如果(File.Exists(destinationFile))
File.Delete(destinationFile);
//要移动一个文件或文件夹到一个新位置:
System.IO.File.Move(的资源文件,destinationFile);

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

#地区ScriptResults声明
枚举ScriptResults
{
成功= Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success,
失败= Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure
};
#endregion

}
}


解决方案

据我了解,我认为你必须通过板材的循环,通过循环文件任务和另一回事。所以,你可能有一个foreach循环内的2个任务。尝试使foreach循环里面的文件的副本系统任务



可执行

  C:\windows\system32\cmd.exe 

和为参数soemthing像

  C复制C:\xxx\abc\\Destination_template.accdbC :\xxx\abc\\Destination_template.accdbDestination_template  -  Kopie.accdb

然后创建一个移动的副本存档文件系统的任务。
这应该做的伎俩(也许不是最好的方法,但应该工作)


I've created a connection using Microsoft Office 12.0 Access Database Engine OLE DB Provider as Excel Schema to loop through all the sheets in the Excel file as demonstrated in this question How to loop through Excel files and load them into a database using SSIS package?

And using Foreach ADO.NET Schema Rowset Enumerator to loop through the excel files.

Everything is working fine now, but after importing the data from Excel, I wanted to move that file to Archive folder. And tried using a File System Task, but I get the error as

[File System Task] Error: An error occurred with the following error message: "The process cannot access the file because it is being used by another process.".

And I also tried with script task from this link. But I was getting some error and couldn't solve the error as I've got zero knowledge on C#.

Below is the error I've got when I tried to move the files using a script task.

at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor) at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments) at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) at System.RuntimeType.InvokeMember(String name, BindingFlags bindingFlags, Binder binder, Object target, Object[] providedArgs, ParameterModifier[] modifiers, CultureInfo culture, String[] namedParams) at Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTATaskScriptingEngine.ExecuteScript().


Update:

Here's my complete code with which I'm trying to move the files.

If I add a breakpoint at enum ScriptResults, I don't get that popup and the task gets completed successfully and the file is also been moved to archive, but if I don't add any breakpoint in the C# code, I get that pop and the file is not moved to archive.

#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

    }
}

解决方案

As far as I understand, I think you have a task for looping through sheets and another for looping through files. So you probably have 2 tasks inside a foreach loop. Try making a copy of the file inside the foreach loop with a system task

for the executable

c:\windows\system32\cmd.exe

and for the arguments soemthing like

C COPY "C:\xxx\abc\\Destination_template.accdb" "C:\xxx\abc\\Destination_template.accdb"Destination_template - Kopie.accdb"

Then create a file system task which moves that copy to your archive. This should do the trick (Maybe not the best approach but should work)

这篇关于SSIS - 移动Excel文件OLEDB连接到存档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆