处理 SSIS 脚本组件中的空值方式

huangapple go评论78阅读模式
英文:

How to handle null values in SSIS script component

问题

I have used the following C# code to convert string value name column to Camel case.
It works fine as long as no null values in the input column. When there is a null value, the script component task fails.
Can you please modify this script so that it can handle null values.

以下是翻译好的部分:

我已经使用以下的C#代码将字符串值名称列转换为驼峰命名法。
只要输入列中没有空值,它就正常工作。但是,如果有空值,脚本组件任务会失败。
请你修改这个脚本,以便它可以处理空值。

public override void Input0_ProcessInputRow(Input0Buffer Row)
{
    CultureInfo cultureInfo = Thread.CurrentThread.CurrentCulture;
    TextInfo textInfo = cultureInfo.TextInfo;

    string PARTICIPANTNAME = Row.PARTICIPANTNAME;
    
    if (PARTICIPANTNAME != null)
    {
        PARTICIPANTNAME = PARTICIPANTNAME.ToLower();
        Row.PARTICIPANTNAME = textInfo.ToTitleCase(PARTICIPANTNAME);
    }
}

希望对你有所帮助。

英文:

I have used the following C# code to convert string value name column to Camel case.
It works fine as long as no null values in the input column. When there is a null value, the script component task fails.
Can you please modify this script so that it can handle null values.

    public override void Input0_ProcessInputRow(Input0Buffer Row)
{
    CultureInfo cultureInfo = Thread.CurrentThread.CurrentCulture;
    TextInfo textInfo = cultureInfo.TextInfo;

    

    string PARTICIPANTNAME = Row.PARTICIPANTNAME;
    PARTICIPANTNAME = PARTICIPANTNAME.ToLower();
    Row.PARTICIPANTNAME = textInfo.ToTitleCase(PARTICIPANTNAME);
}

Thanks

答案1

得分: 0

当您单击进入脚本组件时,在幕后,它会组装一些支持处理需要为 null 的基本类型的脚手架。其中一件事情是向列添加一个 _IsNull 属性。在您的情况下,我会测试列是否为空,如果不为空 !,那么我将执行操作。如果列为空,就不需要做任何操作,否则您只会将 NULL 赋回给缓冲区。

public override void Input0_ProcessInputRow(Input0Buffer Row)
{
    CultureInfo cultureInfo = Thread.CurrentThread.CurrentCulture;
    TextInfo textInfo = cultureInfo.TextInfo;

    // 检查列是否为空
    if (!Row.PARTICIPANTNAME_IsNull)
    {
        string PARTICIPANTNAME = Row.PARTICIPANTNAME;
        PARTICIPANTNAME = PARTICIPANTNAME.ToLower();
        Row.PARTICIPANTNAME = textInfo.ToTitleCase(PARTICIPANTNAME);
    }
}
英文:

When you click into a Script Component, behind the scenes it puts together some scaffolding to deal with primitive types needing to be null. One of the things it does, is add an _IsNull property to the column. In your case, I would test to see if the column is null and if it's not ! then I will perform the operations. No need to do anything if the column is null otherwise you'd just be assigning NULL back to the buffer

public override void Input0_ProcessInputRow(Input0Buffer Row)
{
    CultureInfo cultureInfo = Thread.CurrentThread.CurrentCulture;
    TextInfo textInfo = cultureInfo.TextInfo;

    // Check to see if the column is null
    if (!Row.PARTICIPANTNAME_IsNull)
    {
        string PARTICIPANTNAME = Row.PARTICIPANTNAME;
        PARTICIPANTNAME = PARTICIPANTNAME.ToLower();
        Row.PARTICIPANTNAME = textInfo.ToTitleCase(PARTICIPANTNAME);
    }
}

huangapple
  • 本文由 发表于 2023年6月27日 21:08:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/76565218.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定