SSIS脚本任务获取文件名和文件存储到SSIS对象变量 [英] SSIS Script Task Get File Names and Store to an SSIS Object Variable

查看:1027
本文介绍了SSIS脚本任务获取文件名和文件存储到SSIS对象变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想建立将在一个标准化的文件系统归档过程中使用的SSIS包。基本上,我就能将信息添加到一个配置表,然后使用此表中指定的文件夹归档某些文件。我的问题是,很多文件都有动态命名的,所以我需要得到所有文件的列表,然后查询以决定哪些文件,我应该是感人。



不作为一个C#/ VB程序员试图脚本包的争夺在指定的网络目录中的所有文件,然后喂这些文件名回的SSIS对象变量。


的部分时,引起了我的一些问题

我有一个字符串变量用户:: SourceNetworkFolderName,这将包含我想从读取所有文件的文件夹的UNC位置。我想,然后回传给所有这些文件名(扩展),以所谓的用户:: SourceFilesInTheDirectory的SSIS对象变量。一旦我有文件名列表到对象变量,我要的foreach他们环路成一个SQL表。



有没有人对我怎么去任何具体建议有关获取所有文件名的列表,从我的变量目录到我的SSIS对象变量?



感谢你在前进!



编辑:
这是我更新的代码:

 使用系统; 
使用System.Data这;
使用Microsoft.SqlServer.Dts.Runtime;使用System.Windows.Forms的
;
:使用System.IO;
使用System.Collections.Generic;
使用System.Data.SqlClient的;

命名空间ST_f5e4ae71f14d40d8811af21fa2a9a622.csproj
{
[System.AddIn.AddIn(ScriptMain版本=1.0,出版商=,说明=)]
酒店的公共部分类ScriptMain:Microsoft.SqlServer.Dts.Tasks.ScriptTask.VS​​TARTScriptObjectModelBase
{

#地区VSTA生成的代码
枚举ScriptResults
{
成功= Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success,
失败= Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure
};
#endregion

公共无效的主要()
{
//设置连接字符串到SQL
的SqlConnection的SQLConnection =新的SqlConnection(
/ /用户ID =用户名;+ //用户名
//密码=密码;+ //密码
Trusted_Connection = TRUE;+ // Windows验证
服务器= SERVERNAME;+ // SQL Server的
数据库= DATABASENAME;+ // SQL数据库
连接超时= 30;+ //连接超时
网络库= DBMSSOCN) ; // TCP / IP连接(DBNMPNTW=命名管道)


//打开SQL连接并消除错误代码

{
SQLConnection.Open();
}
赶上(例外OpenConnectionError)
{
Console.WriteLine(OpenConnectionError.ToString());
}


//抓取从'SourceNetworkFolderName'SSIS变量文件的列表叫做ARRAY1阵列。
的String [] = ArrayFileName Directory.GetFiles(Dts.Variables [SourceNetworkFolderName] Value.ToString());


//设置为表人口
的SqlParameter SQLFileNameParam =新的SqlParameter SQL变量(@文件名,SqlDbType.VarChar,100);


//遍历数组,并插入到SQL表
的foreach(在ArrayFileName串strFileName)
{与
//更新SQL变量从数组$ b $文件名b SQLFileNameParam.Value = strFileName;
//使表插入
的SqlCommand SQLInsertToTable =新的SqlCommand(INSERT INTO Archive_Extract_Network_Folder_File_List(文件名)VALUES(@FileName)的SQLConnection);
//这SNIPPIT允许SQL脚本中使用的变量。
SQLInsertToTable.Parameters.Add(SQLFileNameParam);
//执行的SqlCommand
SQLInsertToTable.ExecuteNonQuery();
//清除参数和设置对象为null
SQLInsertToTable.Parameters.Clear();
SQLInsertToTable = NULL;
}


//关闭SQL连接并消除错误代码

{
SQLConnection.Close();
}
赶上(例外CloseConnectionError)
{
Console.WriteLine(CloseConnectionError.ToString());
}


//设置阵列为null,因为它不再需要。
ArrayFileName = NULL;


成功
Dts.TaskResult =(int)的ScriptResults.Success //退出;
}
}
}


解决方案

在你的脚本,只需建立文件名称的数组,并设置数组您的变量(确保变量被设置在脚本任务写入)。如果变量类型的对象可以在其上使用了在后续任务循环然后循环,做任何你想用的文件。你不应该需要任何奇迹只在一个脚本中工作。



把所有文件的源代码目录下的一个数组:

 的String []数组1 = Directory.GetFiles(Dts.Variables(SourceNetworkFolderName)Value.ToString()); 



把所有文件BIN的延伸,在数组:

 的String []数组2 = Directory.GetFiles(Dts.Variables(SourceNetworkFolderName)Value.ToString(),* .BIN); 

您可能需要包括 System.IO 。在您的脚本代码的顶部



修改



要转换的数组由循环任务的处理的列表。调用上面的代码后,调用这样的:

 列表<串GT;的fileList =新的List<串GT;(astrTest); 
Dts.Variables [SourceFilesInTheDirectory]值=的fileList。

您将需要包括 System.Collections.Generic 在脚本文件的开头。


I am trying to build an SSIS package that will be used in a standardized file system archive process. Basically, I will be able to add information to a configuration table and then use this table to archive certain files in specified folders. My issue is that a lot of the files have dynamic naming so I need to get a list of all files and then query to decide which files I should be touching.

Not being a C#/VB programmer is causing me some problems when trying to script a portion of the package the grabs all files in a specified network directory and then feed these file names back into an SSIS object variable.

I have a string variable 'User::SourceNetworkFolderName' which will contain the UNC location of the folder I want to read all files from. I want to then pass all these files names (with extension) back to an SSIS object variable called 'User::SourceFilesInTheDirectory'. Once I have the list of file names into the object variable, I was going to foreach loop them into an SQL table.

Does anyone have any specific suggestions on how I go about getting a list of all files names from my variable directory to 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()
        {
        //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;
        }
    }
}

解决方案

Inside your script, just build an array of the file names and set that array to your variable (make sure the variable is set to writable in the scripting task). If the variable is of type object you can then iterate on it using a for loop in a subsequent task and do whatever you want to with the files. You should not need to work on any miracles solely in one script.

Put all files under your source dir in an Array:

string[] array1 = Directory.GetFiles(Dts.Variables("SourceNetworkFolderName").Value.ToString());

Put all files with an extension of "BIN" in an array:

string[] array2 = Directory.GetFiles(Dts.Variables("SourceNetworkFolderName").Value.ToString(), "*.BIN");

You may need to include System.IO at the top of your scripting code.

EDIT:

To convert array to a List for processing by the Loop task. After calling the code above, call this:

List<string> fileList = new List<string>(astrTest);
Dts.Variables["SourceFilesInTheDirectory"].Value = fileList;

You will need to include System.Collections.Generic at the top of your script file.

这篇关于SSIS脚本任务获取文件名和文件存储到SSIS对象变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆