简体   繁体   中英

C# SSIS How to iterate through columns and set to null

I need to set any columns that are empty strings to an actual null value in SSIS

Normally I can just use a Derived Column and set it to NULL(DSTR, 18, 1252)

Since I have loads of fields and want to do it in one go, I have decided to use a script component to do this. This is the code so far

foreach (PropertyInfo inputColumn in Row.GetType().GetProperties())
{
    if (!inputColumn.Name.EndsWith("IsNull")
    { 
        if (inputColumn.GetValue(Row, null).ToString().Equals(""))
        {                       
            inputColumn.SetValue(Row, null, null);
        }
    }
}

But it throws this error:

[Script Component [11692]] Error: System.NullReferenceException: Object reference not set to an instance of an object. at
Microsoft.SqlServer.Dts.Pipeline.ScriptComponentHost.HandleUserException(Exception e) at
Microsoft.SqlServer.Dts.Pipeline.ScriptComponentHost.ProcessInput(Int32 inputID, PipelineBuffer buffer) at Microsoft.SqlServer.Dts.Pipeline.ManagedComponentHost.HostProcessInput(IDTSManagedComponentWrapper100 wrapper, Int32 inputID, IDTSBuffer100 pDTSBuffer, IntPtr bufferWirePacket)

How can I set it to the equivalent of NULL(type) in a derived column?

根据我的评论,只需将等效的_IsNull属性设置为true。

Row.GetType().GetProperty(inputColumn.Name + "_IsNull").SetValue(Row, true, null);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM