I have an SSIS package that is inputting data from a Flat File into a SQL 2008 database table. The 3rd party generates the Flat File (.csv) every day. There are leading whitespaces in every field that I need to remove.
I thought a Script Component would do the trick?
I want to have it loop through all of the Input Columns and LTrim(RTrim) all of the values for every column.
I found this code here: http://microsoft-ssis.blogspot.com/2010/12/do-something-for-all-columns-in-your.html
But, I don't know how to change it to Trim the values?
I tried changing the "ValueOfProperty.ToUpper()" to "ValueOfProperty.Trim()" but then it causes an error on the component "Error 30203: Identifier expected..."
Help please??
Here's my SSIS Data Flow:
Flat File > Data Conversion > Script Component > OLE DB Destination
' This script adjusts the value of all string fields
Imports System
Imports System.Data
Imports System.Math
Imports System.Reflection ' Added
Imports Microsoft.SqlServer.Dts.Pipeline.Wrapper
Imports Microsoft.SqlServer.Dts.Runtime.Wrapper
<microsoft .sqlserver.dts.pipeline.ssisscriptcomponententrypointattribute=".sqlserver.dts.pipeline.ssisscriptcomponententrypointattribute"> _
<clscompliant false="false"> _
Public Class ScriptMain
Inherits UserComponent
' Method that will be started for each record in you dataflow
Public Overrides Sub Input0_ProcessInputRow(ByVal Row As Input0Buffer)
' Use Reflection to loop through all the properties of Row:
' Example:
' Row.Field1 (String)
' Row.Field1_IsNull (Boolean)
' Row.Field2 (String)
' Row.Field2_IsNull (Boolean)
Dim p As PropertyInfo
For Each p In Row.GetType().GetProperties()
' Do something for all string properties: Row.Field1, Row.Field2, etc.
If p.PropertyType Is GetType(String) Then
' Use a method to set the value of each String type property
' Make sure the length of the new value doesn't exceed the column size
p.SetValue(Row, DoSomething(p.GetValue(Row, Nothing).ToString()), Nothing)
End If
Next
End Sub
' New function that you can adjust to suit your needs
Public Function DoSomething(ByVal ValueOfProperty As String) As String
' Uppercase the value
ValueOfProperty = ValueOfProperty.ToUpper() 'Maybe change this to Trim()?
Return ValueOfProperty
End Function
End Class