SSIS script task component to write Input0Buffer to a text file - vb.net

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?

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.

Related

SSIS Script Task Destination Component Not Completing

I have an SSIS Data Flow that uses a script task to create a row, process the row, and then write the result of one of the columns to a variable in a destination script task.
Destination Script is simply:
Public Overrides Sub Input0_ProcessInputRow(ByVal Row As Input0Buffer)
'
Variables.CampaignId = Row.CampaignId
'
End Sub
DataFlow
Is there something I am missing here? Do I need to be calling the end of the component? or testing for the end of the rowset?
ReadWriteVariables need to be written to in PostExecute and cannot be written to in the RowBuffer.
Imports System
Imports System.Data
Imports System.Math
Imports Microsoft.SqlServer.Dts.Pipeline.Wrapper
Imports Microsoft.SqlServer.Dts.Runtime.Wrapper
<Microsoft.SqlServer.Dts.Pipeline.SSISScriptComponentEntryPointAttribute> _
<CLSCompliant(False)> _
Public Class ScriptMain
Inherits UserComponent
Dim CampID As String
' This method is called after all the rows have passed through this component.
Public Overrides Sub PostExecute()
MyBase.PostExecute()
Variables.CampaignId = CampID
End Sub
Public Overrides Sub Input0_ProcessInputRow(ByVal Row As Input0Buffer)
CampID = Row.CampaignId
End Sub
End Class
Answered my own question, but hopefully this helps someone else whom the error code does not generate for them. After multiple runs I finally got an error code about readwrite variable outside of post execute which helped tremendously with troubleshooting. Gotta love Microsoft...

£ symbol not beind Read from TXT file in Visual Basic 2010

When i run my program to open a txt file, it doesnt display the £ symbol. Is there a way to display it? Heres my code
Imports System
Imports System.IO
Imports System.Text
Public Class Test
Public Shared Sub Main()
Dim path As String = ("H:\16.1\Stock.txt")
' Open the file to read from.
Dim readText() As String = File.ReadAllLines(path, Encoding.UTF8)
Dim Z As String
For Each Z In readText
Console.WriteLine(Z)
Next
Console.ReadLine()
End Sub
End Class

'Sub Main' was not found. Service management console application error?

There is possibly a few things wrong with the below. Haven't had a chance to test/debug the code yet as can't run it. It stating that no main method has been found. But there is? i've even changed it to shared etc. It's probably something obvious?
It's flagging - 'Sub Main' was not found in 'ConsoleApplication1.Module1' error.
Also the main method wasn't always a separate class, I was just trying stuff. I'm importing a reference - system.processes. Was initially created as a vb.form but realised i didn't want the form part and recreated as a console app (which is very possibly where the problem lies as it's one of the first console apps i've done).
Code is basically planned to act on a service dying. Report and try and manage the restart (not finished, ideas welcome).
Imports System
Imports System.Management
Imports System.ServiceProcess
Imports System.Diagnostics
Imports System.Threading
Imports System.IO
Module Module1
Public Class Control
Public Sub Main() 'Public Sub Main(ByVal sArgs() As String)
Dim restart As New Rest
restart.startTime = DateTime.Now()
restart.cr20Services()
restart.Report()
End Sub
End Class
Public Class Rest
public startTime As String
Dim logPath As String = "C:\cr20\restart.txt"
'Dim fileExists As Boolean = File.Exists(strFile)
Dim arrcr20ServicesInitialStatus As New ArrayList
Dim failedServices As New ArrayList
Dim arrcr20Services As New ArrayList
Public Sub cr20Services()
'cr20 Services
arrcr20Services.Add("cr20 service")
arrcr20Services.Add("cr20 router")
For Each cr20Service In arrcr20Services
arrcr20ServicesInitialStatus.Add(cr20Service & " - " & cr20Status(cr20Service))
cr20Restore(cr20Service)
Next
End Sub
Private Function cr20Status(ByVal cr20Service As String)
Dim service As ServiceController = New ServiceController(cr20Service)
Return service.Status.ToString
End Function
Private Sub cr20Restore(ByVal cr20Service As String)
Dim service As ServiceController = New ServiceController(cr20Service)
'Dim p() As System.Diagnostics.Process = System.Diagnostics.Process.GetProcessesByName("calc")
If (service.Status.Equals(ServiceControllerStatus.Stopped)) Or (service.Status.Equals(ServiceControllerStatus.StopPending)) Then
failedServices.Add(service)
service.Stop()
Thread.Sleep(10000) 'give service 10 seconds to stop
service.Start()
End If
End Sub

Cannot load temporary file in VBCodeProvider

Here's my script code:
Imports System.Diagnostics
Public Class Script
Implements IScript
Public Sub DoWork(w As WebBrowser, f As Form1) Implements IScript.DoWork
w.Navigate("http://www.google.com")
wait("5000")
w.Document.All("input").InvokeMember("click")
w.Document.All("input").SetAttribute("value", "Torrenter is the best!")
wait("2000")
w.Document.All("421").InvokeMember("click")
wait("1000")
End Sub
Public Sub wait(ByVal interval As Integer)
Dim sw As New Stopwatch
sw.Start()
Do While sw.ElapsedMilliseconds < interval
' Allows UI to remain responsive
Application.DoEvents()
Loop
sw.Stop()
End Sub
End Class
In-code:
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
If int1.Text = "1" Then
int1.Text = "0"
Dim script As IScript = GenerateScript(File.ReadAllText(ListBox2.Items.Item(int2).ToString()))
script.DoWork(WebBrowser1, Me) 'Object reference not set to an instance of an object.
int2 = int2 + 1
int1.Text = "1"
End If
End Sub
Why? :(
It's supposed to start the next script after the first was done. I tried 4 methods but I can't understand why.
The problem is that your script code is failing to compile, but then you are trying to instantiate an object from the compiled assembly anyway. Since it failed to compile, the assembly doesn't actually exist, hence the error. If you modify the Return line in the GenerateScript method, so that it shows the compile errors, the actual problem will be more clear:
Dim results As CompilerResults = provider.CompileAssemblyFromSource(parameters, codes)
If results.Errors.HasErrors Then
Dim builder As New StringBuilder()
builder.AppendLine("Script failed to compile due to the following errors:")
For Each i As CompilerError In results.Errors
builder.AppendFormat("Line {0}: {1}", i.Line, i.ErrorText)
builder.AppendLine()
Next
Throw New Exception(builder.ToString())
Else
Return CType(results.CompiledAssembly.CreateInstance("Script"), IScript)
End If
I suspect that one of the reasons it's failing to compile is because the script uses IScript which is undefined. The reason it would complain that it's undefined is for two reasons. First, you declared the IScript interface as nested inside the Form1 class. You should move that outside of the form class so that it's not nested inside of any other type. Second, you are not specifying the full namespace nor importing the namespace in your script. You can automatically add the Imports line to the beginning of the script code before compiling it, like this:
Dim interfaceNamespace As String = GetType(IScript).Namespace
Dim codes As String = "Imports " & interfaceNamespace & Environment.NewLine & code
As I mentioned in the comments above, you really ought to be passing a string array into the CompileAssemblyFromSource method, not a string. I'm not sure how that even compiles, unless that's something having Option Strict Off somehow allows? In any case, it expects an array, so you should really be giving it one, like this:
Dim interfaceNamespace As String = GetType(IScript).Namespace
Dim codeArray() As String = New String() {"Imports " & interfaceNamespace & Environment.NewLine & code}
Dim results As CompilerResults = provider.CompileAssemblyFromSource(parameters, codeArray)
Another obvious reason why the script would fail to compile is because you have it using methods and properties of your Form1 class, as if it were a member of that class. Remember, the Script class defined by the script file source code is a completely separate class in a separate assembly. It will have no reference to the form unless you give it a reference, for instance, you could define the interface like this:
Public Interface IScript
Sub DoWork(f As Form1)
End Interface
Then, in your script, you could do this:
Public Class Script
Implements IScript
Public Sub DoWork(f As Form1) Implements IScript.DoWork
f.WebBrowser1.Navigate("http://www.google.com")
f.wait("5000")
f.wait("4000")
f.WebBrowser1.Document.All("input").InvokeMember("click")
f.WebBrowser1.Document.All("input").SetAttribute("value", "User")
f.wait("2000")
f.WebBrowser1.Document.All("421").InvokeMember("click")
End Sub
End Class
UPDATE
Ok, since you can't get it working, and I don't want this whole conversation to be a total loss, I put together a working project and tested it. Here's what you need to do to get it to work.
Contents of IScript.vb
Public Interface IScript
Sub DoWork(w As WebBrowser)
End Interface
Contents of Form1.vb
Imports Microsoft.VisualBasic
Imports System.CodeDom.Compiler
Imports System.Reflection
Imports System.IO
Imports System.Text
Public Class Form1
Dim int1 As Integer = 0
Dim int2 As Integer = 0
Dim p As Point
Public Function GenerateScript(ByVal code As String) As IScript
Using provider As New VBCodeProvider()
Dim parameters As New CompilerParameters()
parameters.GenerateInMemory = True
parameters.ReferencedAssemblies.Add(GetType(WebBrowser).Assembly.Location)
parameters.ReferencedAssemblies.Add(Assembly.GetExecutingAssembly().Location)
Dim interfaceNamespace As String = GetType(IScript).Namespace
code = "Imports System.Windows.Forms" & Environment.NewLine & "Imports " & interfaceNamespace & Environment.NewLine & code
Dim results As CompilerResults = provider.CompileAssemblyFromSource(parameters, code)
If results.Errors.HasErrors Then
Dim builder As New StringBuilder()
builder.AppendLine("Script failed to compile due to the following errors:")
For Each i As CompilerError In results.Errors
builder.AppendFormat("Line {0}: {1}", i.Line, i.ErrorText)
builder.AppendLine()
Next
Throw New Exception(builder.ToString())
Else
Return CType(results.CompiledAssembly.CreateInstance("Script"), IScript)
End If
End Using
End Function
Public Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
For Each File As FileInfo In New System.IO.DirectoryInfo(Application.StartupPath & "/scripts").GetFiles
If CheckedListBox1.GetItemCheckState(int2) = CheckState.Checked Then
ListBox1.Items.Add(File.FullName)
End If
int2 = int2 + 1
Next
int2 = 0
Dim script As IScript = GenerateScript(File.ReadAllText(ListBox1.Items.Item(int2).ToString()))
script.DoWork(WebBrowser1)
End Sub
End Class
Contents of script file
Imports System.Diagnostics
Public Class Script
Implements IScript
Public Sub DoWork(w As WebBrowser) Implements IScript.DoWork
w.Navigate("http://www.google.com")
wait("5000")
wait("4000")
w.Document.All("input").InvokeMember("click")
w.Document.All("input").SetAttribute("value", "User")
wait("2000")
w.Document.All("421").InvokeMember("click")
End Sub
Public Sub wait(ByVal interval As Integer)
Dim sw As New Stopwatch
sw.Start()
Do While sw.ElapsedMilliseconds < interval
' Allows UI to remain responsive
Application.DoEvents()
Loop
sw.Stop()
End Sub
End Class

Workaround for VB.NET partial method using CodeDom?

I know CodeDom doesn't support partial methods, but is there a workaround? I found a workaround for C#, but I need one for VB.NET. Thanks.
It is a horrible hack, as wicked as the C# one, but it does work:
Imports System.CodeDom
Imports System.CodeDom.Compiler
Imports System.IO
Module Module1
Sub Main()
Dim unit As New CodeCompileUnit
Dim nspace As New CodeNamespace("SomeNamespace")
Dim vbclass As New CodeTypeDeclaration("SomeClass")
vbclass.IsClass = True
Dim snippet As New CodeSnippetTypeMember("Partial _")
vbclass.Members.Add(snippet)
Dim method As New CodeMemberMethod()
method.Name = "SomeMethod"
method.Attributes = MemberAttributes.Private
vbclass.Members.Add(method)
nspace.Types.Add(vbclass)
unit.Namespaces.Add(nspace)
dim provider As CodeDomProvider = CodeDomProvider.CreateProvider("VB")
Dim options As New CodeGeneratorOptions()
options.BlankLinesBetweenMembers = False
dim writer As new StringWriter()
provider.GenerateCodeFromCompileUnit(unit, writer, options)
Console.WriteLine(writer.ToString())
Console.ReadLine()
End Sub
End Module
Note that the BlankLinesBetweenMembers option is crucial to make the hack work. Output:
Option Strict Off
Option Explicit On
Namespace SomeNamespace
Public Class SomeClass
Partial _
Private Sub SomeMethod()
End Sub
End Class
End Namespace
I ended up detecting the language and using CodeSnippetTypeMember()
Dim onDeleting = "Partial Private Sub OnDeleting()" & Environment.NewLine & "End Sub"
type.Members.Add(New CodeSnippetTypeMember(onDeleting))