標準化されたファイルシステムアーカイブプロセスで使用されるSSISパッケージを構築しようとしています。基本的に、構成テーブルに情報を追加し、このテーブルを使用して、指定したフォルダー内の特定のファイルをアーカイブすることができます。私の問題は、多くのファイルに動的な名前が付けられているため、すべてのファイルのリストを取得してから、どのファイルにアクセスするかをクエリする必要があることです。
C#/ VBプログラマーではないため、パッケージの一部をスクリプト化して、指定したネットワークディレクトリ内のすべてのファイルを取得し、これらのファイル名をSSISオブジェクト変数にフィードバックしようとすると問題が発生します。
文字列変数'User:: SourceNetworkFolderName'があります。これには、すべてのファイルを読み取るフォルダーのUNCの場所が含まれます。次に、これらすべてのファイル名(拡張子付き)を「User::SourceFilesInTheDirectory」という名前のSSISオブジェクト変数に戻します。ファイル名のリストをオブジェクト変数に入れたら、それらをSQLテーブルにforeachループします。
変数ディレクトリから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;
}
}
}