我需要將許多附件從SharePoint中的多個列表移到SQL中的BLOB表中。我發現允許我將附件下載到本地驅動器的代碼,但是我無法修改它以將它作爲二進制數據直接加載到SharePoint中。將附件從SP遷移到SQL BLOB
using Microsoft.SharePoint.Client;
using System;
using System.IO;
using System.Net;
using System.Windows.Forms;
using System.Data;
using Microsoft.SqlServer.Dts.Pipeline.Wrapper;
using Microsoft.SqlServer.Dts.Runtime.Wrapper;
[Microsoft.SqlServer.Dts.Pipeline.SSISScriptComponentEntryPointAttribute]
public class ScriptMain : UserComponent
{
public override void CreateNewOutputRows()
{
try
{
int startListID = 1;
String siteUrl = "https://mysharepointsite.com/sites/mysite";
String listName = "AttachmentTesting";
NetworkCredential credentials =
new NetworkCredential("username", "password", "domain");
using (ClientContext clientContext = new ClientContext(siteUrl))
{
clientContext.Credentials = credentials;
//Get the Site Collection
Site oSite = clientContext.Site;
clientContext.Load(oSite);
clientContext.ExecuteQuery();
// Get the Web
Web oWeb = clientContext.Web;
clientContext.Load(oWeb);
clientContext.ExecuteQuery();
CamlQuery query = new CamlQuery();
query.ViewXml = @"";
List oList = clientContext.Web.Lists.GetByTitle(listName);
clientContext.Load(oList);
clientContext.ExecuteQuery();
ListItemCollection items = oList.GetItems(query);
clientContext.Load(items);
clientContext.ExecuteQuery();
foreach (ListItem listItem in items)
{
if (Int32.Parse(listItem["ID"].ToString()) >= startListID)
{
Folder folder =
oWeb.GetFolderByServerRelativeUrl(oSite.Url +
"/Lists/" + listName + "/Attachments/" +
listItem["ID"]);
clientContext.Load(folder);
try
{
clientContext.ExecuteQuery();
}
catch (ServerException ex)
{
}
FileCollection attachments = folder.Files;
clientContext.Load(attachments);
clientContext.ExecuteQuery();
foreach (Microsoft.SharePoint.Client.File oFile in folder.Files)
{
FileInfo myFileinfo = new FileInfo(oFile.Name);
WebClient client1 = new WebClient();
client1.Credentials = credentials;
byte[] fileContents =
client1.DownloadData("https://mysharepointsite.com" +
oFile.ServerRelativeUrl);
}
}
}
}
}
catch (Exception e)
{
}
}
}
最後的文件流只是爲了測試前面的代碼,它成功地檢索了附件。現在我需要進行下一步,並將其加載到某種緩衝區中以推入SQL。我已經嘗試使用:
Output0Buffer.AddRow();
Output0Buffer.fileName = oFile.Name;
在循環中,並Output0Buffer.SetEndOfRowset();
末但這給出了一個錯誤:
[Import Column [2]] Error: SSIS Error Code DTS_E_INDUCEDTRANSFORMFAILUREONERROR. The "Import Column" failed because error code 0xC02090BB occurred, and the error row disposition on "Import Column.Inputs[Import Column Input].Columns[fileName]" specifies failure on error. An error occurred on the specified object of the specified component. There may be error messages posted before this with more information about the failure.
我該怎麼做推着我的字節數組fileContents到我的SQL表的最後一步blob列? 非常感謝。
編輯:我應該省略fstream。這只是爲了測試目的。我試圖做的是將附件直接從SharePoint推送到SQL,而不需要將其存儲在本地的中間步驟。