这是一个自己学习实现的包,具体的需求如下,如果你遇上以下的情况可以看看!!有疑问可来信交流:zxm1110@126.com
1,本地会持续的动态的生成一些重要文件,需要把生成的文件上传到FTP服务器。并创建相关的文件夹。
2,文件夹的创建方式,以天为单位,比如今天上传的文件就传到以日期命名的文件夹,如果存在文件夹就不创建,不存在则创建。
3,上传文件不能覆盖原上传的文件,上传完成后删除本地的生成文件。
4,然后发邮件通知相关的人。
SSIS包开发:
整体图:
开发讲解:
新建变量:
设置变量的初始值:添加脚本,在可重写和只读栏选择对应的变量。
设置变量脚本代码:
public void Main() { // TODO: Add your code here string filePath = Dts.Variables["InterfaceFilesConnection"].Value.ToString(); string[] fileEntries = System.IO.Directory.GetFiles(filePath); if (fileEntries.Length !=0) { Dts.Variables["FileCheck"].Value = "Y"; } else { Dts.Variables["FileCheck"].Value = "N"; } Dts.Variables["FTPDirectory"].Value = "/" + DateTime.Today.ToString("yyyy-MM-dd"); Dts.Variables["FTPFolder"].Value = Dts.Variables["FTPDirectory"].Value + "/"; Dts.Variables["IFLogFileConnection"].Value = Dts.Variables["IFLogFileConnection"].Value.ToString() + "IFLogFile_" + DateTime.Now.ToString("yyyy_MM_dd_HH_mm_ss")+".log"; Dts.TaskResult = (int)ScriptResults.Success; }
判断本地是否有文件,没有就不继续,有就走上传流程。
判断文件夹:通过c#代码实现,这将会有个很大的问题。值得注意,判断服务器上文件是否存在的方法是通过FtpWebRequest请求和FtpWebResponse响应。WebRequestMethods.Ftp.ListDirectory获取文件名。应为要根据返回来的文件名判断是否存在了文件夹,要有文件名相等或匹配的判断。这里有两种情况都要写上。因为该部分的代码在
VS开发工具上返回的是有文件夹名构成的XML文件,但是当你部署带SQL Agent 上时,返回的就单单只是文件夹名的数组。就会出现开发工具上成功部署跑起来就失败!很重要的问题,我被迷惑了两天,最后把两种环境下的返回值拿出来对比才知道。我不清楚为什么,但是真实就是这样的!信我,我测过,不信你自己测吧。
代码:
/* Microsoft SQL Server Integration Services Script Task Write scripts using Microsoft Visual C# 2008. The ScriptMain is the entry point class of the script. */ using System; using System.Data; using Microsoft.SqlServer.Dts.Runtime; using System.Windows.Forms; using System.Net; using System.Text.RegularExpressions; using System.IO; using System.Collections.Generic; namespace ST_757525aca913416fb93d9cc6de0f476d.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 /* The execution engine calls this method when the task executes. To access the object model, use the Dts property. Connections, variables, events, and logging features are available as members of the Dts property as shown in the following examples. To reference a variable, call Dts.Variables["MyCaseSensitiveVariableName"].Value; To post a log entry, call Dts.Log("This is my log text", 999, null); To fire an event, call Dts.Events.FireInformation(99, "test", "hit the help message", "", 0, true); To use the connections collection use something like the following: ConnectionManager cm = Dts.Connections.Add("OLEDB"); cm.ConnectionString = "Data Source=localhost;Initial Catalog=AdventureWorks;Provider=SQLNCLI10;Integrated Security=SSPI;Auto Translate=False;"; Before returning from this method, set the value of Dts.TaskResult to indicate success or failure. To open Help, press F1. */ public void Main() { // TODO: Add your code here string server = Dts.Variables["FTPServer"].Value.ToString().Trim(); string user =Dts.Variables["FTPUserName"].Value.ToString().Trim(); string password = Dts.Variables["FTPPassWord"].Value.ToString().Trim(); string folderName = Dts.Variables["FTPDirectory"].Value.ToString().Trim().Substring(1); string ftpDirectory = ">" + folderName + "</A>"; //Dts.Variables["CheckFTPDirectory"].Value = "N"; //MangerFtp(server, user, password); bool flag = CheckFTPFile(folderName,ftpDirectory, server, user, password); if (flag == true) { Dts.Variables["CheckFTPDirectory"].Value = "Y"; } else { Dts.Variables["CheckFTPDirectory"].Value = "N"; } Dts.TaskResult = (int)ScriptResults.Success; } public bool CheckFTPFile(string folderName,string ftpDirectory, string strFTPPath, string strftpUserID, string strftpPassword) { FtpWebRequest reqFTP; Regex str; Match mc; bool flag = false; // dirName = name of the directory to create. if (strFTPPath.Length != 0 && strftpUserID.Length != 0 && strftpPassword.Length != 0) { reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(strFTPPath)); reqFTP.Method = WebRequestMethods.Ftp.ListDirectory; reqFTP.UseBinary = true; reqFTP.UsePassive = true; reqFTP.Credentials = new NetworkCredential(strftpUserID, strftpPassword); FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse(); StreamReader ftpStream = new StreamReader(response.GetResponseStream()); List<string> files = new List<string>(); string line = ftpStream.ReadLine(); while (line != null) { files.Add(line); line = ftpStream.ReadLine(); } str = new Regex(ftpDirectory); foreach (string FileName in files) { //Dts.Variables["ReturnFolderString"].Value =Dts.Variables["ReturnFolderString"].Value.ToString()+ FileName; //string FileName = ">2013-01-26</A>"; mc = str.Match(FileName); //判断在vs中返回的结果[会返回xml格式的文件数组]
if (mc.Success) { flag = true; //Dts.Variables["CheckFTPDirectory"].Value = "Y"; } //判断在sql agent上返回的ftp文件夹名【name1,name2,name3】
if (folderName == FileName) { flag = true; } }; ftpStream.Close(); response.Close(); } return flag; } } }
接下来就简单了:
用SSIS中的FTP组件和Foreach就可实现新建文件夹上传文件夹。这很简单,拉来就用 用!
浙公网安备 33010602011771号