【发布时间】:2014-11-10 16:57:17
【问题描述】:
我正在尝试使用 SSIS 脚本任务将 excel 文件复制到 Sharepoint 库中,但那里已经存在同名文件。它是先删除旧文件然后复制新文件还是只复制并替换新文件都没有关系。我不知道如何删除旧文件,并且在旧文件消失之前它不会复制新文件。到目前为止,我有:
/*
Microsoft SQL Server Integration Services Script Task
Write scripts using Microsoft Visual C# 2008.
The ScriptMain is the entry point class of the script.
*/
using System;
using System.Data;
using Microsoft.SqlServer.Dts.Runtime;
using System.Windows.Forms;
using System.IO;
namespace ST_be5f0817a6b54483a96a8c9e79402175.csproj
{
[System.AddIn.AddIn("ScriptMain", Version = "1.0", Publisher = "", Description = "")]
public partial class ScriptMain : Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase
{
#region VSTA generated code
enum ScriptResults
{
Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success,
Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure
};
#endregion
/*
The execution engine calls this method when the task executes.
To access the object model, use the Dts property. Connections, variables, events,
and logging features are available as members of the Dts property as shown in the following examples.
To reference a variable, call Dts.Variables["MyCaseSensitiveVariableName"].Value;
To post a log entry, call Dts.Log("This is my log text", 999, null);
To fire an event, call Dts.Events.FireInformation(99, "test", "hit the help message", "", 0, true);
To use the connections collection use something like the following:
ConnectionManager cm = Dts.Connections.Add("OLEDB");
cm.ConnectionString = "Data Source=localhost;Initial Catalog=AdventureWorks;Provider=SQLNCLI10;Integrated Security=SSPI;Auto Translate=False;";
Before returning from this method, set the value of Dts.TaskResult to indicate success or failure.
To open Help, press F1.
*/
public void Main()
{
string fileDir = (string)Dts.Variables["fileDir"].Value;
string SPDir = (string)Dts.Variables["SPDir"].Value;
if (File.Exists(Path.Combine(SPDir, "filename.CSV")))
{
File.Delete(Path.Combine(SPDir, "filename.CSV"));
}
File.Copy(Path.Combine(fileDir, "filename.CSV"), Path.Combine(SPDir, "filename.CSV"));
Dts.TaskResult = (int)ScriptResults.Success;
}
}
}
我想我可以通过在脚本之前使用共享点列表连接器运行数据流组件来删除文件,并让脚本将文件复制过来,但我试图避免那么多组件和连接以及该方法只是通常听起来更复杂。
欢迎任何帮助或建议。
【问题讨论】:
-
你有什么问题?它会产生错误吗?
-
它不会删除当前在 Sharepoint 上的文件。如果我让它按原样运行,那么我会收到一个文件已经存在的错误。如果我把 if 语句和 copy 语句注释掉,只留下 delete 语句,它不会报错,也不会删除文件。
-
当你在调试器中运行它时,它是否命中了 File.Delete 行,或者 if() 测试是否为假?
-
据我所知,它正在删除行。我注释掉了 if 部分,使其显示为 //if (File.Exists(Path.Combine(SPDir, "filename.CSV"))) { File.Delete(Path.Combine(SPDir, "filename.CSV")) ; } //File.Copy(Path.Combine(fileDir, "filename.CSV"), Path.Combine(SPDir, "filename.CSV"));这不会给出错误。抱歉,我不知道如何在此处添加换行符。我以为它应该是双空格,但似乎没有这样做。
-
这篇文章中似乎有很多有用的信息:stackoverflow.com/questions/6391711/… 如果您尝试不使用 Path.Combine 的测试并输入文件的完整路径会怎样?
标签: sharepoint ssis sharepoint-2013