posted on Saturday, July 16, 2005 5:57 PM
by
Knight_Reign
Loading variable values from a file...
This is another question that comes up a lot. How do I load a file's contents into a variable. This is one case where the script task comes in very handy.
Drop a script task on the designer and add the following code:
Public Sub Main()
Dim errorInfo As String = ""
Dim Contents As String = ""
Contents = GetFileContents(Dts.Variables("FileName").Value, errorInfo)
If errorInfo.Length > 0 Then
MsgBox(errorInfo, MsgBoxStyle.Critical, "Error")
Dts.TaskResult = Dts.Results.Failure
Else
MsgBox(Contents, MsgBoxStyle.OKOnly, "File contents")
Dts.TaskResult = Dts.Results.Success
End If
End Sub
Public Function GetFileContents(ByVal filePath As String, Optional ByVal ErrorInfo As String = "") As String
Dim strContents As String
Dim objReader As StreamReader
Try
objReader = New StreamReader(filePath)
strContents = objReader.ReadToEnd()
objReader.Close()
Return strContents
Catch Ex As Exception
ErrorInfo = Ex.Message
End Try
End Function
Now, create a couple of variables. I've named them FileContents and FileName. Make sure they are of type string.
Set the value of the FileName variable to point to the fully qualified file name of the file with the text you wish to load into the variable.
Run the package and a message box will show up with the contents of the file.
Hope this helps,
Universe.Earth.Software.Microsoft.SQLServer.IS.KirkHaselden