JYousef
JYousef

Reputation: 181

SSIS script task component to write Input0Buffer to a text file

I'm trying to dump all input0Buffer to a txt file, to end my struggle with making the flat destination re-usable as i've over 100 package and each structure and columns are different.

I'm redirecting the error rows to a flat file, so it's a nightmare to set that manually in every package, so I wanna write the whole input without specifieng Row.Name, all of them into text file.

I'm up to the point that i'm getting only one column!! it's driving me crazy!!

Imports System
Imports System.Data
Imports System.Math
Imports Microsoft.SqlServer.Dts.Pipeline.Wrapper
Imports Microsoft.SqlServer.Dts.Runtime.Wrapper
Imports System.IO
Imports System.Reflection
Imports System.Xml
Imports Microsoft.SqlServer.Dts.Pipeline


<Microsoft.SqlServer.Dts.Pipeline.SSISScriptComponentEntryPointAttribute()> _
<CLSCompliant(False)> _
Public Class ScriptMain
  Inherits UserComponent

  Dim textWriter As StreamWriter
  Private inputBuffer As PipelineBuffer

  Public Overrides Sub ProcessInput(ByVal InputID As Integer, ByVal Buffer As Microsoft.SqlServer.Dts.Pipeline.PipelineBuffer)
    inputBuffer = Buffer
    MyBase.ProcessInput(InputID, Buffer)
  End Sub

  Public Overrides Sub PreExecute()
    MyBase.PreExecute()
    textWriter = New StreamWriter( "c:\Test4.txt", True)
  End Sub

  Public Overrides Sub PostExecute()
    MyBase.PostExecute()
    textWriter.Close()
    ''
  End Sub

  Public Overrides Sub Input0_ProcessInputRow(ByVal Row As Input0Buffer)

    Dim delim As String = ", "

    Dim RowCount As Integer = 0

    For RowCount = 0 To inputBuffer.ColumnCount = -1
      If inputBuffer.Item(RowCount).ToString() = "" Then
        inputBuffer.Item(RowCount) = String.Empty
      End If
    Next

    textWriter.WriteLine(inputBuffer.Item(RowCount).ToString() & delim)

  End Sub

End Class

can anyone help me please?

Upvotes: 0

Views: 7231

Answers (1)

IAmTimCorey
IAmTimCorey

Reputation: 16765

The issue is where your write is at. You are writing outside of your For loop. You loop through each row and set the value of the entry to String.Empty but you aren't doing any writing to the textWriter here. Adjust the code as follows:

Dim myBuilder As New StringBuilder

For RowCount = 0 To inputBuffer.ColumnCount = -1
  If inputBuffer.Item(RowCount).ToString() = "" Then
    inputBuffer.Item(RowCount) = String.Empty
  End If
  myBuilder.Append(inputBuffer.Item(RowCount).ToString() & delim)
Next

textWriter.WriteLine(myBuilder.ToString)

That will ensure that each column gets written. The only issue will be that your last column will have a delimiter after it. You might want to trim that off before you write it. Also, you will need to add an Imports System.Text to your code as well.

One note I wanted to add since it can cause some confusion: your loop counter is called RowCount when it is performing the act of counting columns (ColumnCount-1 shows us that). That might cause some confusion down the road and assumptions could be made that might cause coding mistakes.

Upvotes: 1

Related Questions