2012-08-01 68 views
5

我正在尝试构建将用于标准化文件系统归档过程的SSIS包。基本上,我将能够将信息添加到配置表中,然后使用此表将特定文件夹中的某些文件存档。我的问题是很多文件都具有动态命名,所以我需要获取所有文件的列表,然后查询以确定我应该触摸哪些文件。SSIS脚本任务获取文件名并存储到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; 
     } 
    } 
} 

回答

5

里面你的脚本,只是生成的文件名的阵列和阵列设置为您的变量(确保变量被设置为可写在脚本任务中)。如果变量是类型对象,则可以在后续任务中使用for循环对其进行迭代,然后对文件执行任何你想要的操作。你不应该只需要在一个脚本中创造任何奇迹。

把所有文件的源代码目录下的一个数组:

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

把所有文件“BIN”的延伸中的数组:

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

您可能需要包括System.IO在您的脚本代码的顶部。

编辑

要由循环任务数组转换成列表进行处理。调用上面的代码后,调用此:

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

您将需要包括System.Collections.Generic在脚本文件的开头。

+0

谢谢你的帮助!我取得了一些进展,但我再次陷入困境。我认为我的新问题与我从脚本任务中填充对象变量的方式有关。在调试器中,我正在使用watch和我的变量'User :: SourceFilesInTheDirectory'显示: Value = {Dimensions:[2]} Type = String [] 我相信要将此变量用作ADO它需要阅读: Value = {System.Data。DataSet} Type = DataSet – Joe 2012-08-01 23:04:06

+0

我感觉超级接近,但是当我试图将这个变量作为一个Foreach ADO枚举器运行时,我得到了调试错误。 关于如何让这个变量具有正确的类型以便我可以将它用作Foreach枚举器的任何想法?我已经粘贴下面我的代码: – Joe 2012-08-01 23:14:10

+0

'代码 公共无效的Main(){ // 抓取从 'SourceNetworkFolderName' SSIS变量的文件的列表,以阵列 串[] ARRAY1 = Directory.GetFiles(Dts.Variables [ “SourceNetworkFolderName”] Value.ToString())。 //使用数组中的所有文件名填充SSIS对象变量'SourceFilesInTheDirectory' Dts.Variables [“SourceFilesInTheDirectory”]。Value = array1; Dts.TaskResult =(int)ScriptResults.Success; } ' – Joe 2012-08-01 23:15:43