I am trying to create an SSIS package that will be used in a standardized file system archiving process. Basically, I can add information to the configuration table, and then use this table to archive specific files in the specified folders. My problem is that many files have a dynamic name, so I need to get a list of all the files and then ask which files I should touch.
Not being a C # / VB programmer, there are some problems when trying to script part of the package to capture all the files in the specified network directory and then return these file names to the SSIS object variable.
I have a string variable 'User :: SourceNetworkFolderName' which will contain the UNC folder into which I want to read all the files. Then I want to transfer all these file names (with extension) back to the SSIS object variable called "User :: SourceFilesInTheDirectory". After I have a list of file names in an object variable, I was going to loop them into an SQL table.
Does anyone have any specific suggestions on how I am going to get a list of all the file names from the variable directory into my SSIS object variable?
Thank you in advance!
EDIT:
Here is my updated code:
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()
{
SqlConnection SQLConnection = new SqlConnection(
"Trusted_Connection=true;" +
"server=SERVERNAME;" +
"database=DATABASENAME; " +
"connection timeout=30;" +
"Network Library=dbmssocn");
try
{
SQLConnection.Open();
}
catch (Exception OpenConnectionError)
{
Console.WriteLine(OpenConnectionError.ToString());
}
string[] ArrayFileName = Directory.GetFiles(Dts.Variables["SourceNetworkFolderName"].Value.ToString());
SqlParameter SQLFileNameParam = new SqlParameter("@FileName", SqlDbType.VarChar, 100);
foreach (string strFileName in ArrayFileName)
{
SQLFileNameParam.Value = strFileName;
SqlCommand SQLInsertToTable = new SqlCommand("INSERT INTO Archive_Extract_Network_Folder_File_List (FileName) VALUES (@FileName)", SQLConnection);
SQLInsertToTable.Parameters.Add(SQLFileNameParam);
SQLInsertToTable.ExecuteNonQuery();
SQLInsertToTable.Parameters.Clear();
SQLInsertToTable = null;
}
try
{
SQLConnection.Close();
}
catch (Exception CloseConnectionError)
{
Console.WriteLine(CloseConnectionError.ToString());
}
ArrayFileName = null;
Dts.TaskResult = (int)ScriptResults.Success;
}
}
}
source
share