Scenario
A client is wanting to use the web to retrieve coordinate information from Google Maps to store the information for a Marketing campaign.
Solution
The SSIS Script task can connect to the web page and return the results. The following script provides sample code on how to get the Script Task to perform the action. The script task was developed in SQL SSIS 2008 using VB, the 2005 version is similar but the guts of the HTTP code remains the same (thanks to my colleague Andrew Crowder for putting together the main syntax):
First, a package is created with two variables of type String. The variables are passed into the script task in the following manner:
- ReadOnly: User::URL
- ReadWrite: User::HTML
Now, the script itself:
Imports System
Imports Microsoft.SqlServer.Dts.Runtime
Imports System.IO
Imports System.Net
<System.AddIn.AddIn("ScriptMain", Version:="1.0", Publisher:="", Description:="")> _
<System.CLSCompliantAttribute(False)> _
Partial Public Class ScriptMain
Inherits Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase
Enum ScriptResults
Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success
Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure
End Enum
Public Sub Main()
Try
'setup the variables, URL is set in the package as a var and read
Dim URL As String = Dts.Variables("URL").Value
Dim request As HttpWebRequest = WebRequest.Create(URL)
Dim response As HttpWebResponse = request.GetResponse()
Dim reader As StreamReader = New StreamReader(response.GetResponseStream())
'read the http data into a string to be used later in the package
Dim str As String = reader.ReadToEnd()
Dts.Variables("HTML").Value = str.ToString()
Dts.TaskResult = ScriptResults.Success
Catch ex As Exception
'fire the info event and return as error from the task
Dts.Events.FireInformation(0, "Web Script Task", ex.Message.ToString(), "", 0, 0)
Dts.TaskResult = ScriptResults.Failure
End Try
End Sub
Execution
The following URL value is set and returns the coordinate information into the HTML variable:
HTML: 200,8,37.4219720,-122.0841430
The script task can also return the HTML from a page, for example http://www.rdacorp.com, and allow for other SSIS tasks to manipulate the HTML variable as per the requirements and design of the package as it would like any other variable within the package.






5 comments:
Very helpful solution :) I will have to try to rewrite it to C# as I prefer it over VB.
PowR_TocH
If you are using BIDS 2008 you can use C#. However if you are stuck with BIDS 2005 than you are stuck with VB.
One more thing is this script to be placed in a Scropt component task?
yes, the script is placed in the Script Task located in the Control Flow
Can You please help me with such problem: I would like to use the script above to retrieve information returned from a search from this site: http://www.genecards.org//index.php?path=/Search/Advanced/[{by%3A%22keyword%22%2C+f%3A%22text%22%2C+txt%3A%221007_s_at%22}]/[{cat%3A%22all%22%2C+gifts%3A%22all%22%2C+giftsRangeA%3A%221%22%2C+giftsRangeB%3A%22100%22%2C+hgnc%3Atrue%2C+ent%3Atrue%2C+ens%3Atrue}]#searchResults yet I'm a newbie in SSIS and I don't know how to achieve this. I will be very grateful for any help.
Post a Comment