Solved

Reading a file in SSIS

Posted on 2014-01-24
4
334 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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
set default date format in mssql to mm/dd/yyyy 22 66
grouping logic 6 46
T-SQL: Nested CASE Statements 4 24
T-SQL:  Sigh---Boy, this is fun.... 12 25
Meet the world's only “Transparent Cloud™” from Superb Internet Corporation. Now, you can experience firsthand a cloud platform that consistently outperforms Amazon Web Services (AWS), IBM’s Softlayer, and Microsoft’s Azure when it comes to CPU and …
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

919 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

17 Experts available now in Live!

Get 1:1 Help Now