SSIS - C#:脚本仅在我将断点(调试模式)放入其中时才有效

时间:2017-06-12 03:14:38

标签: c# ssis etl

如果我在其上插入断点,我的脚本只能正常工作(文件被移动,没有错误)。 这是在ETL作业完成后将文件移动到另一个文件夹的脚本。

源文件是.xls(excel file 2003),它遍历每个文件的文件和工作表。

文件仍以某种方式使用,在ETL作业完成后,我仍无法手动移动文件。 如果我“复制”文件而不是“移动”,我就不会发现任何问题。

但是对于这项任务,我需要在ETL作业完成后“移动”文件。

我也尝试过使用“文件系统任务”。它仍然不起作用,错误显示文件仍被另一个进程使用。

谁有同样的问题?或者有人想在ETL工作完成后“释放”excel文件?

感谢您的帮助

#region Help:  Introduction to the script task
/* The Script Task allows you to perform virtually any operation that can be accomplished in
 * a .Net application within the context of an Integration Services control flow. 
 * 
 * Expand the other regions which have "Help" prefixes for examples of specific ways to use
 * Integration Services features within this script task. */
#endregion


#region Namespaces
using System;
using System.Data;
using Microsoft.SqlServer.Dts.Runtime;
using System.Windows.Forms;
using System.IO;
using System.Collections.Generic;
using System.Diagnostics;
using System;
using System.Diagnostics;
using System.Threading;

#endregion

namespace ST_b6c4a58519344f53b083ac59c40a2b40
{
    /// <summary>
    /// ScriptMain is the entry point class of the script.  Do not change the name, attributes,
    /// or parent of this class.
    /// </summary>
    [Microsoft.SqlServer.Dts.Tasks.ScriptTask.SSISScriptTaskEntryPointAttribute]
    public partial class ScriptMain : Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase
    {
        #region Help:  Using Integration Services variables and parameters in a script
        /* To use a variable in this script, first ensure that the variable has been added to 
         * either the list contained in the ReadOnlyVariables property or the list contained in 
         * the ReadWriteVariables property of this script task, according to whether or not your
         * code needs to write to the variable.  To add the variable, save this script, close this instance of
         * Visual Studio, and update the ReadOnlyVariables and 
         * ReadWriteVariables properties in the Script Transformation Editor window.
         * To use a parameter in this script, follow the same steps. Parameters are always read-only.
         * 
         * Example of reading from a variable:
         *  DateTime startTime = (DateTime) Dts.Variables["System::StartTime"].Value;
         * 
         * Example of writing to a variable:
         *  Dts.Variables["User::myStringVariable"].Value = "new value";
         * 
         * Example of reading from a package parameter:
         *  int batchId = (int) Dts.Variables["$Package::batchId"].Value;
         *  
         * Example of reading from a project parameter:
         *  int batchId = (int) Dts.Variables["$Project::batchId"].Value;
         * 
         * Example of reading from a sensitive project parameter:
         *  int batchId = (int) Dts.Variables["$Project::batchId"].GetSensitiveValue();
         * */

        #endregion

        #region Help:  Firing Integration Services events from a script
        /* This script task can fire events for logging purposes.
         * 
         * Example of firing an error event:
         *  Dts.Events.FireError(18, "Process Values", "Bad value", "", 0);
         * 
         * Example of firing an information event:
         *  Dts.Events.FireInformation(3, "Process Values", "Processing has started", "", 0, ref fireAgain)
         * 
         * Example of firing a warning event:
         *  Dts.Events.FireWarning(14, "Process Values", "No values received for input", "", 0);
         * */
        #endregion

        #region Help:  Using Integration Services connection managers in a script
        /* Some types of connection managers can be used in this script task.  See the topic 
         * "Working with Connection Managers Programatically" for details.
         * 
         * Example of using an ADO.Net connection manager:
         *  object rawConnection = Dts.Connections["Sales DB"].AcquireConnection(Dts.Transaction);
         *  SqlConnection myADONETConnection = (SqlConnection)rawConnection;
         *  //Use the connection in some code here, then release the connection
         *  Dts.Connections["Sales DB"].ReleaseConnection(rawConnection);
         *
         * Example of using a File connection manager
         *  object rawConnection = Dts.Connections["Prices.zip"].AcquireConnection(Dts.Transaction);
         *  string filePath = (string)rawConnection;
         *  //Use the connection in some code here, then release the connection
         *  Dts.Connections["Prices.zip"].ReleaseConnection(rawConnection);
         * */
        #endregion


        /// <summary>
        /// This method is called when this script task executes in the control flow.
        /// Before returning from this method, set the value of Dts.TaskResult to indicate success or failure.
        /// To open Help, press F1.
        /// </summary>
        /// 


        public void Main()
        {
            // TODO: Add your code here

            //load the data from dataset


            var dsVariable = Dts.Variables["User::XlsGoodFileList"].Value;
            DataSet ds = (DataSet)dsVariable;
            DataTable statusTable = ds.Tables[0];

            //to contain the value from dataset
            string filePath = "";
            string fileStatus = "";
            List<string> fileListPath = new List<string>();
            List<string> fileName = new List<string>();

            List<string> tempSource = new List<string>();
            List<string> tempDestination = new List<string>();

            //filling the array to be used later
            foreach (DataRow row in statusTable.Rows)
            {

                filePath = row["FileName"].ToString();
                fileStatus = row["isSuccess"].ToString();

                string[] name = filePath.Split('\\');

                //filter the only correct file(s)
                if (fileStatus == "True")
                {
                    fileListPath.Add(filePath);
                    fileName.Add(name[name.Length - 1]);
                }

                //start moving the correct file(s)
                string path = @"";
                string path2 = @"";
                if (fileListPath.Count > 0)
                {
                    for (int i = 0; i < fileListPath.Count; i++)
                    { 
                             path = @"";
                             path2 = @"D:\ETL TOOL\GAMC\Archive\GAMC_REPORTS\";
                            path = fileListPath[i];
                            path2 += fileName[i];
                            tempSource.Add(path);
                            tempDestination.Add(path2);
                    }

                    for (int i = 0; i<tempSource.Count; i++)
                    {
                        File.Move(tempSource[i], tempDestination[i]);     
                    }
                }

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









        #region ScriptResults declaration
        /// <summary>
        /// This enum provides a convenient shorthand within the scope of this class for setting the
        /// result of the script.
        /// 
        /// This code was generated automatically.
        /// </summary>
        enum ScriptResults
        {
            Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success,
            Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure
        };
        #endregion

    }
}

0 个答案:

没有答案