SSIS脚本任务无法写入Object变量

时间:2018-01-16 17:51:57

标签: c# sql-server csv ssis script-task

我正在使用脚本任务从Share Point库下载CSV文件,我可以使用File.ReadAllLines(),代码会将我的数据表中的数据写入System.Object变量就好了dev机器但在部署到服务器时失败。

我已经检查了从库中读取的帐户的访问权限,这似乎不是问题,因此我决定尝试使用可以利用默认.net凭据缓存的方法。我已经尝试了WebClient和HttpWebRequest,他们将下载CSV,但是当我需要将数据从本地内存中的数据表写入包范围的Object变量时,会在最后一步导致调用错误。

有关为什么下载方法之间的奇怪交互干扰写入变量的任何想法?

这是错误: here's a screen shot of the error

以下是代码:

public void Main()
{
    //read the value of the index from the package variable
    int index = (int)Dts.Variables["User::SQLIndex"].Value;

    Array reports;

    string filePath = "https://servername/filepath/MobiusRepts.csv";
    HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(filePath);
    request.Method = "GET";
    request.Accept = "text/html";
    request.Credentials = System.Net.CredentialCache.DefaultCredentials;
    WebResponse GetResponse = request.GetResponse();
    //create a stream reader for the response 
    StreamReader reader = new StreamReader(GetResponse.GetResponseStream(), Encoding.UTF8);
    //response is the full response string converted to string
    string response = reader.ReadToEnd();
    //Dts.Events.FireInformation(0, "downLoadCSV", response.ToString(), String.Empty, 0, ref fireAgain);

    reports = response.Split(new String[] {"\r\n"}, StringSplitOptions.None);
    Dts.Events.FireInformation(0, "reportsLength", reports.Length.ToString(), String.Empty, 0, ref fireAgain);


    //declare a data table
    DataTable tempReportsTable = new DataTable();
    tempReportsTable.Columns.Add("ReportEnvironment");
    tempReportsTable.Columns.Add("ReportName");
    tempReportsTable.Columns.Add("ReportKey");
    tempReportsTable.Columns.Add("ReportID");
    //set type to int32 to pass it into sql more easily
    tempReportsTable.Columns.Add("SQLID", typeof(Int32));

    // int index = 0; //an index for the sql rows
    string[] row = { "", "", "" }; // a string array to hold the split lines
    //use a for each loop to iterate over the array
    foreach(string line in reports) {
    row = line.Split(';');
    // create a new data row
    DataRow dataRow = tempReportsTable.NewRow();
    // clean up the environment and name values
    dataRow["ReportEnvironment"] = row[2].Substring(0, 4);
    dataRow["ReportName"] = row[1].Trim();
    // create the report key using the id and env.
    dataRow["ReportKey"] = row[0].Trim() + ":" + row[2].Substring(0, 4);
    dataRow["ReportID"] = row[0].Trim();
    // set the index for the id in the sql table
    dataRow["SQLID"] = index;
    //add the row to the table
    tempReportsTable.Rows.Add(dataRow);
    Dts.Events.FireInformation(0, "Loop", index.ToString(), String.Empty, 0, ref fireAgain);
    Dts.Events.FireInformation(0, "loop test", row[1].Trim() + ", " + row[0].Trim() + ":" + row[2].Substring(0, 4), String.Empty, 0, ref fireAgain);
    //increment the index 
    index++;
    row.Initialize();
    }

    //push the data table into the object
    //Dts.Variables["User::ReportsObject"].Value = new DataTable();
    Dts.Events.FireInformation(0, "table test", "success", String.Empty, 0, ref fireAgain);
    Dts.Variables["User::ReportsObject"].Value = tempReportsTable.DataSet;

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

0 个答案:

没有答案