问题
I'm currently working on a database that have '\0' characters in fields.
For instance the field
Category CHAR(4)
sometimes has value '\0\0\0\0' (4 zero characters) and sometimes ' ' (4 blank characters)
I want to use a script component to individuate all the fields with this problem. I've written the following script, but it doesn't work since the C# converts the '\0\0\0\0' to an empty string.
public override void Input0_ProcessInputRow(Input0Buffer Row)
{
Type rowType = Row.GetType();
foreach (IDTSInputColumn100 column in ComponentMetaData.InputCollection[0].InputColumnCollection)
{
PropertyInfo columnValue = rowType.GetProperty(column.Name.Replace("_", ""));
Object obj = columnValue.GetValue(Row, null);
if (obj is string)
{
string s = (string)obj;
StringBuilder sb = new StringBuilder();
foreach (char c in s)
{
if (c < ' ')
{
sb.Append(' ');
}
else
sb.Append(c);
}
columnValue.SetValue(Row, sb.ToString(), null);
}
}
}
Is it possible to convert the field to a byte array instead of a string, in order to be able to individuate '\0' characters?
回答1:
Do you need to do this in a script component? Could you not use a Derived Column task and use Substring to pull out each of the column values? You could use Trim as well to remove any spaces.
回答2:
Consider converting the char(4) to a binary representation (using Data Conversion component), then manipulating the individual elements from there. If possible, cast it in your source query, so that it's already binary before it enters the pipeline.
来源:https://stackoverflow.com/questions/4214510/ssis-script-component-to-remove-0-charachers-in-charn-fields