I have a list of files from a local folder in a SSIS Script task and I need to log it to a SQL table. Is there any way that I can do it directly from the script task or may be put the results in a object variable and then populate that to a SQL table. I am not taking the option of For Each Loop container since I feel I can log more details from a Script task.
Code:
List<String> FileList = new List<string>();
List<String> FileLoc = new List<string>();
foreach (string dirpath in Directory.EnumerateDirectories("C:\\Program Files\"))
foreach (string path in Directory.EnumerateFiles(dirpath))
{
FileList.Add(Path.GetFileName(path));
FileLoc.Add(Path.GetDirectoryName(path));
}
I would like to lof each FileList and FileLoc as individual columns in a table.
PS: I am new to C#.
I found a solution for my query in this link, SSIS Script Task Get File Names and Store to an SSIS Object Variable
But still I am posting my answer to the question that I had raised
ConnectionManager cm;
System.Data.SqlClient.SqlConnection sqlConn;
cm = Dts.Connections["<connection name>"];//should be an ADO .NET connection
sqlConn = (System.Data.SqlClient.SqlConnection)cm.AcquireConnection(Dts.Transaction);
foreach (string dirpath in Directory.EnumerateDirectories("<Folder Location>"))
{
//Sql Parameters object creation
SqlParameter SQLFileNameName = new SqlParameter("@FileName", SqlDbType.VarChar,200);
SqlParameter SQLFileLocation = new SqlParameter("@FileLoc", SqlDbType.VarChar, 200);
//Assign value to SQL parameter
SQLFileLocation.Value = dirpath;
foreach (string path in Directory.EnumerateFiles(dirpath))
{
//Assign value to SQL parameter
SQLFileNameName.Value = Path.GetFileName(path);
//Populate to a SQL Table
SqlCommand sqlCmd = new SqlCommand("INSERT INTO [dbo].[<Table Name>] (FileName,FileLocation) VALUES (@FileName,@FileLoc)", sqlConn);
//Assign Parameters to script
sqlCmd.Parameters.Add(SQLFileNameName);
sqlCmd.Parameters.Add(SQLFileLocation);
//Execute SQL Command
sqlCmd.ExecuteNonQuery();
//Clear the parameters and the set the object to NULL
sqlCmd.Parameters.Clear();
sqlCmd = null;
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.