簡體   English   中英

SSIS腳本任務獲取文件名並將其存儲到SSIS對象變量

[英]SSIS Script Task Get File Names and Store to an SSIS Object Variable

我正在嘗試構建一個SSIS包,該包將在標准化文件系統存檔過程中使用。 基本上,我將能夠向配置表中添加信息,然后使用該表將某些文件存檔在指定的文件夾中。 我的問題是,很多文件都具有動態命名功能,因此我需要獲取所有文件的列表,然后查詢以決定應該觸摸哪些文件。

如果不是C#/ VB程序員,則在嘗試編寫程序包的一部分腳本時會引起一些問題,這些程序將抓取指定網絡目錄中的所有文件,然后將這些文件名反饋給SSIS對象變量。

我有一個字符串變量'User :: SourceNetworkFolderName',它將包含我要從中讀取所有文件的文件夾的UNC位置。 然后,我想將所有這些文件名(帶有擴展名)傳遞回名為'User :: SourceFilesInTheDirectory'的SSIS對象變量。 將文件名列表放入對象變量后,我將要進行foreach將它們循環到SQL表中。

有人對我如何從變量目錄到SSIS對象變量的所有文件名列表有任何具體建議嗎?

先感謝您!

編輯:這是我更新的代碼:

using System;
using System.Data;
using Microsoft.SqlServer.Dts.Runtime;
using System.Windows.Forms;
using System.IO;
using System.Collections.Generic;
using System.Data.SqlClient;

namespace ST_f5e4ae71f14d40d8811af21fa2a9a622.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

        public void Main()
        {
        //Setup Connection String to SQL
            SqlConnection SQLConnection = new SqlConnection(
                                       //"user id=username;" +                  //UserName
                                       //"password=password;" +                 //Password
                                       "Trusted_Connection=true;" +             //Windows Auth
                                       "server=SERVERNAME;" +                   //SQL Server
                                       "database=DATABASENAME; " +              //SQL Database
                                       "connection timeout=30;" +               //connection timeout
                                       "Network Library=dbmssocn");             //TCP/IP Connection ("dbnmpntw" = Name Pipes)


        //Open the SQL Connection and remove the error code
            try
            {
                SQLConnection.Open();
            }
            catch (Exception OpenConnectionError)
            {
                Console.WriteLine(OpenConnectionError.ToString());
            }


        //Fetch a list of files from 'SourceNetworkFolderName' SSIS variable to an array called array1.
            string[] ArrayFileName = Directory.GetFiles(Dts.Variables["SourceNetworkFolderName"].Value.ToString());


        //Set up sql variable for table population
            SqlParameter SQLFileNameParam = new SqlParameter("@FileName", SqlDbType.VarChar, 100);


        //Loop through the array and insert into an SQL table
            foreach (string strFileName in ArrayFileName)
            {
            //Update sql variable with file names from array
                SQLFileNameParam.Value = strFileName;
            //Make the table insert
                SqlCommand SQLInsertToTable = new SqlCommand("INSERT INTO Archive_Extract_Network_Folder_File_List (FileName) VALUES (@FileName)", SQLConnection);
            //This snippit allows the use of the variable in the sql script.
                SQLInsertToTable.Parameters.Add(SQLFileNameParam);
            //Execute SqlCommand
                SQLInsertToTable.ExecuteNonQuery();
            //Clear the parameters and set the object to null    
                SQLInsertToTable.Parameters.Clear();
                SQLInsertToTable = null;
            }


        //Close the SQL Connection and remove the error code
            try
            {
                SQLConnection.Close();
            }
            catch (Exception CloseConnectionError)
            {
                Console.WriteLine(CloseConnectionError.ToString());
            }


        //Set array to null since it is no longer required.
            ArrayFileName = null;


        //Exit on success
            Dts.TaskResult = (int)ScriptResults.Success;
        }
    }
}

在腳本內部,只需構建一個文件名數組並將該數組設置為您的變量(確保在腳本任務中將該變量設置為可寫)。 如果變量的類型為object,則可以在后續任務中使用for循環對其進行迭代,並對文件執行任何操作。 您不必只用一個腳本就可以處理任何奇跡。

將所有文件放在源目錄下的數組中:

string[] array1 = Directory.GetFiles(Dts.Variables("SourceNetworkFolderName").Value.ToString());

將所有擴展名為“ BIN”的文件放入數組中:

string[] array2 = Directory.GetFiles(Dts.Variables("SourceNetworkFolderName").Value.ToString(), "*.BIN");

您可能需要在腳本代碼的頂部包括System.IO

編輯

將數組轉換為列表以供Loop任務處理。 調用上面的代碼后,請調用以下代碼:

List<string> fileList = new List<string>(astrTest);
Dts.Variables["SourceFilesInTheDirectory"].Value = fileList;

您將需要在腳本文件頂部包含System.Collections.Generic

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM