Solved

Reading a file in SSIS

Posted on 2014-01-24
4
329 Views
Last Modified: 2016-02-10
Hi,

We have an issue where we need to monitor a log file for an error code.  I thought there may be a way to do this in SSIS where it can read a line in a file?  I have seen an article about reading the SQL Error Log file but not specifically any file.

Hope you can point me in the right direction.

Many Thanks
Anita
0
Comment
Question by:ADhal37
  • 2
  • 2
4 Comments
 
LVL 17

Accepted Solution

by:
Barry Cunney earned 500 total points
ID: 39807037
Hi Anita,
In SSIS add a Script Task component.
The Script Task component facilitates writing VB.net or C# code

In the Script Task, for your situation, you could write C# code that uses the .Net StreamReader object to read the required file - Partial sample code below:

static void Main()
    {
	//
	// If you have an SSIS variable that has the file name, read in the value of this variable
	Logfile = Dts.Variables["LogFile"].Value


	string line;
	using (StreamReader reader = new StreamReader(LogFile))
	{
	    line = reader.ReadLine();
	}

Open in new window

0
 

Author Comment

by:ADhal37
ID: 39821429
Hi

Many Thanks for that.  I have written some code in VB that reads the file:

Public Sub Main()
            '
        ' Add your code here

        'initialize StreamReader class for text file'
        Dim streamReader As New IO.StreamReader("C:\reader.txt")
        ' Read the StreamReader To End and assign to local variable
        Dim StreamText As String = streamReader.ReadToEnd()
        ' assign SSIS variable with value of StreamText local variable.
        Me.Dts.Variables("vText").Value = StreamText
            '
            Dts.TaskResult = ScriptResults.Success
      End Sub

Could you please advise on how I can return an error or send an alert if there is a particular error code in the log file?
0
 
LVL 17

Assisted Solution

by:Barry Cunney
Barry Cunney earned 500 total points
ID: 39823657
Hi Anita,
An option is to use the Dts.Events.FireError event which can log an error
Dts.Events.FireError(0, "Log File Error Check", "Error Code found in Log file", String.Empty, 0)


So your code could be something like the following

Public Sub Main()
            '
        
	Dim LogFile As String
	Dim ErrorCode As String
	
        LogFile = Me.Dts.Variables("LogFile").Value
	ErrorCode = Me.Dts.Variables("ErrorCode ").Value


        'initialize StreamReader class for text file'
        Dim streamReader As New IO.StreamReader(LogFile)
        
	' Read the StreamReader To End and assign to local variable
        Dim StreamText As String = streamReader.ReadToEnd()
        
	If StreamText.Contains(ErrorCode) Then
		
		Dts.Events.FireError(0, "Log File Error Check", "Error Code found in Log file", String.Empty, 0)
		
	End If
	
          
          Dts.TaskResult = ScriptResults.Success


End Sub

Open in new window



What exact type of alert is required in your case? Is an e-mail alert required?
0
 

Author Comment

by:ADhal37
ID: 39823736
That's great, I will give that a whirl.

Ideally, it could just log an error in the SQL error log and I could then build an alert to fire when it shows.

Thanks
0

Featured Post

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Join & Write a Comment

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
ADCs have gained traction within the last decade, largely due to increased demand for legacy load balancing appliances to handle more advanced application delivery requirements and improve application performance.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now