SSIS 小脚本 - 时间参数验证
2013-01-14 11:16 BIWORK 阅读(1139) 评论(0) 编辑 收藏 举报项目中也经常使用到时间参数的验证,例如根据某一时间范围来从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;
}
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 单线程的Redis速度为什么快?
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 展开说说关于C#中ORM框架的用法!
· SQL Server 2025 AI相关能力初探
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库