0

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#.

4

1 回答 1

0

我在这个链接中找到了我的查询的解决方案,SSIS Script Task Get File Names and Store to an SSIS Object Variable

但我仍然在发布我对我提出的问题的回答

            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;

                    }
                }
于 2015-02-11T13:27:38.760 回答