通过SSIS

时间:2016-06-03 13:18:25

标签: .net sql-server vb.net windows ssis

我编写了一个链接到数据库的vb.net控制台程序,它具有通过将各种配置中的单独PDF页面连接在一起来生成报告包的主要功能。

要做到这一点,我必须使用一些解决方法,但已经在本地测试了构建和部署的应用程序。手动哪个工作正常。这些列在下面:

我需要通过SSIS调用应用程序,以便可以将其作为更广泛的代理作业的一部分进行调度。

我无法远程访问作业运行的SSIS服务器,但可以通过SSMS / Trigger连接到该工作等等。可以看到应用程序.exe和关联文件所在的文件夹的共享。

因为我无权在服务器上安装应用程序,所以我采用了在本地安装Visual Studio的一次输出并从本地apps文件夹中提取.exe和相关文件的方法。然后将这些文件复制到文件共享中。我再次测试了.exe文件可以在那里使用我的凭据登录时手动运行,这没有问题,并产生预期的输出文件和日志文件。

当SSIS作业运行时,它使用服务帐户 - 我无法确定该帐户是否可以访问共享驱动器,但是因为它能够将驱动器上的文件移动到其他作业,我认为这是真正。此外,我创建了一个更基本的控制台应用程序,它只读取XML文件中的值,但不写任何输出,并且它运行成功。

我查看了SSMS,可以看到共享文件位于代理服务器的G:上。我假设.exe也将在代理服务器上执行,因此尝试将文件路径映射到G:和文件共享。我认为证券将根据您如何接近文件夹而有所不同。

创建PDF所需的插件不接受UNC文件路径,因此我需要为其提供相对或映射的文件夹路径。我无法映射驱动器,因为我不知道代理帐户密码 - 但如果绝对必要,可以解决此问题。目前我从XML配置文件加载相对路径。

当应用程序从我自己的帐户从共享文件夹位置手动运行时,它会按预期创建日志文件。

当通过SSIS运行应用程序时,作业失败并显示错误代码:-2146232576并且未创建任何日志文件。

由于日志文件是应用程序的第一件事,我认为它失败了,因为它无法写入输出。

有人知道确认的方法: A.).exe运行的地方(包从G:文件路径调用.exe) b。)代理帐户对其运行目录的权限 C.)错误代码的含义 D.)如果有一些完全离开的领域,我在这里缺席了吗?

Imports PdfSharp
Imports PdfSharp.Drawing
Imports PdfSharp.Pdf
Imports PdfSharp.Pdf.IO
Imports System.Data.SqlClient

Module Module1

    Sub Main(ByVal args() As String)

        Dim eventlog As System.IO.StreamWriter
        eventlog = My.Computer.FileSystem.OpenTextFileWriter("\\csimccs01\FTP\Out\PDFCREATOR\log.txt", True)

        eventlog.WriteLine(Now() & " Process Started at")

        Dim countargs = 0
        Dim FY As String = ""
        Dim FP As String = ""
        Dim con1 As New SqlConnection
        Dim con2 As New SqlConnection
        Dim con3 As New SqlConnection
        Dim cmd1 As New SqlCommand
        Dim cmd2 As New SqlCommand
        Dim cmd3 As New SqlCommand

        Dim sqldataset2 As SqlDataReader
        Dim packid As Integer
        Dim LV7Symbol As String
        Dim PackDescription As String
        Dim SPPATH As String
        Dim RptVer As Integer
        Dim outputdoc As PdfDocument
        Dim PDFCount As Integer
        Dim progress As Integer = 0
        Dim importdoc As PdfDocument
        Dim count As Integer


        Dim constr As String
        Dim inputfilefolder As String
        Dim outputfilefolder As String
        Dim fullinputfilepath As String
        Dim outputfilename As String

        eventlog.WriteLine(Now() & " Looking for config file")
        Try
            If My.Computer.FileSystem.FileExists("\\csimccs01\FTP\Out\PDFCREATOR\config.xml") Then
                Dim xmlconfig = XDocument.Load("\\csimccs01\FTP\Out\PDFCREATOR\config.xml")
                constr = xmlconfig.<config>.<DBCONNECTIONSTRING>.Value
                inputfilefolder = xmlconfig.<config>.<FilePath>.<Input>.Value
                outputfilefolder = xmlconfig.<config>.<FilePath>.<Output>.Value
                eventlog.WriteLine(Now() & " Config file loaded and variables set")
                eventlog.WriteLine(Now() & " inputfolder: " & inputfilefolder)
                eventlog.WriteLine(Now() & " outputfolder: " & outputfilefolder)
            Else
                eventlog.WriteLine(Now() & " Config File Not Found")
                Exit Sub
            End If


        Catch ex As Exception
            Console.WriteLine(1)
            eventlog.WriteLine(Now() & " Config File Not Found, routine exited by exception")
            eventlog.Close()
            Exit Sub
        End Try

        countargs = 0

        eventlog.WriteLine(Now() & " Setting Fiscal Year & Period Variables")

        If FY = "" Then

            FY = "FY2016"
        End If

        If FP = "" Then

            FP = "FP01"
        End If
        eventlog.WriteLine(Now() & " Fiscal Year & Period Variables set")

        Try
            eventlog.WriteLine(Now() & " Attempting to open database con1")
            con1.ConnectionString = constr
            con1.Open()
            eventlog.WriteLine(Now() & " Con1 open")
            cmd1.Connection = con1
            cmd1.CommandText = "SELECT [PackID],[LV7Symbol],[LV7Description],[SharePoint_Output_Path],[Report_Version] FROM [D_EXP_CPM].[dbo].[FACT_REPORT_CONTROL_TABLE]"
            Dim sqldataset1 As SqlDataReader = cmd1.ExecuteReader()
            eventlog.WriteLine(Now() & " Report List loaded into Dataset1")


            eventlog.WriteLine(Now() & " Starting to loop through Dataset1")

            While sqldataset1.Read()


                packid = sqldataset1.Item("PackID")
                LV7Symbol = sqldataset1.Item("LV7Symbol").ToString
                PackDescription = sqldataset1.Item("LV7Description").ToString
                SPPATH = sqldataset1.Item("SharePoint_Output_Path").ToString
                RptVer = sqldataset1.Item("Report_Version") + 1
                eventlog.WriteLine(Now() & " Pack level variables set for pack id " & packid)
                eventlog.WriteLine(Now() & " Opening connection3 to update version number")
                con3.ConnectionString = constr
                con3.Open()
                cmd3.Connection = con3
                cmd3.CommandText = "UPDATE [D_EXP_CPM].[dbo].[FACT_REPORT_CONTROL_TABLE] SET [Report_Version]=" & RptVer & " WHERE [PackID] =" & packid
                cmd3.ExecuteNonQuery()
                con3.Close()
                eventlog.WriteLine(Now() & " Version Updated")
                eventlog.WriteLine(Now() & " Connection 3 closed")


                Try
                    eventlog.WriteLine(Now() & " Opening connection2")

                    con2.ConnectionString = constr
                    con2.Open()
                    cmd2.Connection = con2
                    cmd2.CommandText = "SELECT [EntryID],[PackID], [TemplateID], [SortOrder] FROM [D_EXP_CPM].[dbo].[FACT_REPORTPACK_PAGE_CONFIG] WHERE [PACKID] = " & packid & "  Order By [PackID], [SortOrder]"
                    sqldataset2 = cmd2.ExecuteReader()
                    eventlog.WriteLine(Now() & " Results or pack page configuration loaded into sqldatset2")

                    eventlog.WriteLine(Now() & " Opening temporary PDF")
                    outputdoc = New PdfDocument
                    eventlog.WriteLine(Now() & " Starting to loop through Pack pages for pack " & packid)
                    While sqldataset2.Read()

                        If Not sqldataset2.HasRows Then
                            eventlog.WriteLine(Now() & " No template configuration data found for packid " & packid)
                            GoTo nextrpt
                        End If

                        fullinputfilepath = inputfilefolder & "\CC " & LV7Symbol & "_TID" & (sqldataset2.Item("TemplateID") - 1) & "_" & FP & "_" & FY & ".pdf"
                        eventlog.WriteLine(Now() & " input file path set to: " & fullinputfilepath)

                        Try
                            eventlog.WriteLine(Now() & " attempting to read input file")
                            importdoc = PdfReader.Open(fullinputfilepath, PdfSharp.Pdf.IO.PdfDocumentOpenMode.Import)
                            eventlog.WriteLine(Now() & " inputfile read successfully")
                        Catch ex As Exception
                            eventlog.WriteLine(Now() & " Unable to read intput file, move to next report")
                            GoTo nextrpt 
                        End Try

                        count = importdoc.PageCount

                        progress = 0

                        Do Until progress = count
                            Dim pdfpage As PdfPage = importdoc.Pages(progress)
                            outputdoc.AddPage(pdfpage)
                            progress = progress + 1
                        Loop
                        eventlog.WriteLine(Now() & " incrementing pdf page count")
                        PDFCount = PDFCount + 1
                    End While
                    eventlog.WriteLine(Now() & " all pages of input file consolidated")

                    PackDescription.Replace("&", "and")
                    eventlog.WriteLine(Now() & " '&' charcter removed from file names")

                    outputfilename = outputfilefolder & "\" & LV7Symbol & "_" & FY & "_" & FP & "_" & PackDescription & "_VER" & RptVer & ".pdf"

                    eventlog.WriteLine(Now() & " attempting to save output PDF as: " & outputfilename)

                    If outputdoc.PageCount > 0 Then
                        outputdoc.Save(outputfilename)
                    End If

                    eventlog.WriteLine(Now() & " output file saved")
nextrpt:

                Catch ex As Exception
                    eventlog.WriteLine(Now() & " Exception encountered in page level try catch")
                Finally
                    con2.Close()
                    eventlog.WriteLine(Now() & " Connection 2 closed")

                End Try


            End While



        Catch ex As Exception
            Console.WriteLine(1)
            eventlog.WriteLine(Now() & " Exception encountered in report level try catch")
            eventlog.Close()
            Exit Sub
        Finally
            con1.Close()
            eventlog.WriteLine(Now() & " Connection 1 closed")
        End Try

        Console.WriteLine(0)
        eventlog.WriteLine(Now() & " Process completed successfully")
        eventlog.Close()

        Exit Sub



    End Sub


End Module

1 个答案:

答案 0 :(得分:2)

原来它不是代码,而是安装在服务器上的.net版本。无法升级.net,因此将visual studio中的编译设置更改为.net 4,并将包文件修改为目标net40而不是net45,现在可以正常工作。