[英]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.