Friday, March 9, 2012

Run DTS from ASP.NET (VB)

I have a DTS package which imports data from a legacy app to SQLServer 2000. Right now I have a page in classic ASP which executes a SP which in turn calls the DTS package. After the user hits the "Update" button on the ASP page they are redirected to a page that says "This process takes several minutes, check back later." The current setup is causing problems because Joe may hit the "Update" button to refresh the data and get an error because the DTS package is already running from when Mary just ran the same process.

I want to rewrite this in .NET (2.0) to accomplish the following:

    Eliminate the SPCheck the status of the DTS package and alert the user if it is already running.Show a "Please wait while we process your request" message to the person who started the update while the DTS is running.Automatically show the report when the DTS is done.

I think I can find code in the forum to execute the DTS, but how do check the current status first? I've read about SQLDMO but the Web server and SQLServer are on different machines. Would this still work?

Thanks in advance.

I'm having trouble getting the DTS execute to run asynchronously. All the examples I've found show executing queries asynchronously, but nothing on DTS (there all in C# as well). Here's what I have in my class.

Imports System.IO
Imports System.NetImports System.Net.Mail
Imports System.ThreadingImports System.Runtime.Remoting.Messaging
Imports DTS
Imports DTS.DTSSQLServerStorageFlags

PartialClass Reporting_AmSherReports_rptColStats
Inherits System.Web.UI.Page
'----------------------------
'Remember, if you change the package, these numbers will need to be modified too.
'----------------------------

Private sPkgIDAs String ="{4CFD14F0-12D8-445C-AD98-E0DD784B166A}"
Private sPkgVersionAs String ="{F0CAFA3B-9A52-4780-B0C6-5B86C38BCFC5}"
Protected sMessageAs String =""

Protected Sub Page_Load(ByVal senderAs Object,ByVal eAs System.EventArgs)Handles Me.Load
If Page.IsPostBackThen

'Show processing message...

Processing.Visible =True
Report.Visible =False
Refresh.Visible =False

Dim stateAs New Object()
Dim beginHandlerAs New BeginEventHandler(AddressOf ExecuteDTS)
Dim endHandlerAs New EndEventHandler(AddressOf DTSComplete)
Dim timeoutHandlerAs New EndEventHandler(AddressOf DTSTimeout)
Dim taskAs New PageAsyncTask(beginHandler, endHandler, timeoutHandler, state)
RegisterAsyncTask(task)
End If
End Sub

Private Function ExecuteDTS(ByVal senderAs Object,ByVal eAs EventArgs,ByVal cbAs AsyncCallback,ByVal stateAs Object)As IAsyncResult
Try
Dim sSourceAs String =""
Dim sDescAs String =""
Dim lErrAs Long
Dim oPKGAs DTS.Package =New DTS.Package
Dim oStepAs DTS.Step

'Load Package

oPKG.LoadFromSQLServer("server","sa","", DTSSQLStgFlag_UseTrustedConnection, , sPkgID, sPkgVersion,"Refresh Stats")

'Execute

oPKG.Execute()

'Get Status and Error Message

For Each oStepIn oPKG.Steps
If oStep.ExecutionResult = DTSStepExecResult.DTSStepExecResult_FailureThen
oStep.GetExecutionErrorInfo(lErr, sSource, sDesc)
sMessage = sMessage &"Step """ & oStep.Name &""" Failed" & vbCrLf & _
vbTab &"Error: " & lErr & vbCrLf & _
vbTab &"Source: " & sSource & vbCrLf & _
vbTab &"Description: " & sDesc & vbCrLf & vbCrLf
End If
Next
Catch exAs Exception
Finally
Processing.Visible =False
Report.Visible =True
Refresh.Visible =False
End Try
End Function

Private Sub DTSComplete(ByVal arAs IAsyncResult)
Processing.Visible =False
Report.Visible =True
Refresh.Visible =True
End Sub

Private Sub DTSTimeout(ByVal arAs IAsyncResult)
'No implementation -- needed for PageAsyncTask

End Sub
End Class

No comments:

Post a Comment