SSIS 小脚本 - 时间参数验证
2013-01-14 11:16
302 查看
项目中也经常使用到时间参数的验证,例如根据某一时间范围来从Source中过滤一些数据. 在运行Package之前需要配置这些时间格式的参数,因此需要验证这些参数是否是正确的时间格式. 并且通常还有时间大小的验证,例如起始时间要小于结束时间.
/* 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.Globalization; namespace ST_TEST.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() { String sPackageName = Dts.Variables["System::PackageName"].Value.ToString(); string sSelectFromDate = Dts.Variables["User::SelectFromDate"].Value.ToString(); string sSelectThruDate = Dts.Variables["User::SelectThruDate"].Value.ToString(); DateTime dtSelectDateFrom = new DateTime(); DateTime dtSelectDateThru = new DateTime(); CultureInfo provider = CultureInfo.InvariantCulture; bool bParametersValid = true; bParametersValid &= ValidateRuntimeParameterFilled(sPackageName, sSelectFromDate, "SelectFromDate"); bParametersValid &= ValidateRuntimeParameterFilled(sPackageName, sSelectThruDate, "SelectThruDate"); bParametersValid &= FormatDateTimeParameter(sPackageName, sSelectFromDate, "SelectFromDate", ref dtSelectDateFrom, provider); bParametersValid &= FormatDateTimeParameter(sPackageName, sSelectThruDate, "SelectThruDate", ref dtSelectDateThru, provider); if (!bParametersValid) { return; } if (dtSelectDateFrom > dtSelectDateThru) { Dts.Events.FireError(0, sPackageName, "SelectFromDate: " + sSelectFromDate + " is greater than SelectThruDate: " + sSelectThruDate, "", 0); return; } Dts.TaskResult = (int)ScriptResults.Success; } // Validate current run date was specified private bool ValidateRuntimeParameterFilled(string sPackageName, string sParameterValue, string sParameterName) { if (sParameterValue.Length == 0) { Dts.Events.FireError(0, sPackageName, string.Format("{0} was not specified", sParameterName), "", 0); return false; } return true; } // Formatting datetime input parameter private bool FormatDateTimeParameter(string sPackageName, string sParameterValue, string sParameterName, ref DateTime dtFormattedDateTime, CultureInfo Culture) { if (sParameterValue.Length != 0) { try { dtFormattedDateTime = DateTime.ParseExact(sParameterValue, "yyyy-MM-dd HH:mm:ss", Culture); } catch (System.Exception e) { Dts.Events.FireError(0, sPackageName, string.Format("Exception occurred validating {0}: {1}, error: {2}", sParameterName, sParameterValue, e.Message.ToString()), "", 0); return false; } } return true; } } }
相关文章推荐
- 报表设计中参数、脚本的使用以及生成系统时间
- 基于时间延迟的Python验证脚本
- 报表设计中参数、脚本的使用以及生成系统时间
- 验证用户输入的参数合法性的shell脚本
- 通过sharepoint 客户脚本,验证列表添加页面上的时间
- SSIS 小脚本 - 文件路径验证
- 验证只输入时间的脚本和文本选中js脚本(兼容firefox)
- 验证用户输入的参数合法性的shell脚本
- shell脚本变量与参数
- Python 时间操作例子和时间格式化参数小结
- 关于ajax提交表单参数序列化和时间戳转换
- 使用linux的shell脚本实现在当前行重复动态显示时间等字符串信息(不另起新行)
- Shell脚本的一些参数
- selenium运行自动化脚本(验证通过)
- linux中各个shell配置脚本文件的作用域和启动时间
- 1.9-expect脚本传递参数
- 批量获取及验证HTTP代理的Python脚本
- Python+Hadoop Streaming实现MapReduce(如何给map和reduce的脚本传递参数)
- 验证时间的正则表达式
- shell脚本 接受用户参数技巧一