微软BI 之SSIS 系列 - SSIS 时间参数验证脚本

浏览: 3549

项目中也经常使用到时间参数的验证,例如根据某一时间范围来从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;
}
}
}
推荐 0
本文由 BIWORK 创作,采用 知识共享署名-相同方式共享 3.0 中国大陆许可协议 进行许可。
转载、引用前需联系作者,并署名作者且注明文章出处。
本站文章版权归原作者及原出处所有 。内容为作者个人观点, 并不代表本站赞同其观点和对其真实性负责。本站是一个个人学习交流的平台,并不用于任何商业目的,如果有任何问题,请及时联系我们,我们将根据著作权人的要求,立即更正或者删除有关内容。本站拥有对此声明的最终解释权。

1 个评论

要回复文章请先登录注册