在 SSIS 中使用 C# 语言的脚本任务,如何转换创建文件然后附加完整文件名的 DTS ActiveX 脚本

发布于 2024-12-08 15:31:10 字数 3521 浏览 1 评论 0原文

我正在尝试将 DTS 进程转换为 SSIS,并且我想转换创建 3 个文件的 DTS 脚本任务(仅在有数据要写入时才创建文件),然后将完整文件名附加到变量,即然后复制到全局变量。由于我是新手,如果可能的话,我想要一步一步的演示,拜托,拜托,漂亮请帮忙......谢谢。下面是创建两个而不是三个文件的 DTS 脚本示例:

Function Main()
DIM dbConnection, rsGetFileDetails, rsGetInfo
DIM fsoFileSystem, fExportFile
DIM sGroupCode, sSequenceNumber, sFileIdentifier, FileName
DIM sLineItem, sAttachmentPaths
DIM  FileLocation
sAttachmentPaths = ""
FileLocation = DTSGlobalVariables("FileDestPath").Value

'****************************************************
'*********Connect to Database******************
'****************************************************
SET dbConnection = CreateObject( "ADODB.Connection" )
dbConnection.Provider = "sqloledb"
dbConnection.Properties("Data Source").Value = "Server"
dbConnection.Properties("Initial Catalog").Value = "Database"
dbConnection.Properties("Integrated Security").Value = "1234"
dbConnection.Open 

'*******************************************************************
'***********GET TRACE ALERT DELIST DATA************
'*******************************************************************


SET fsoFileSystem = CreateObject( "Scripting.FileSystemObject" )
SET rsGetInfo = CreateObject( "ADODB.recordset" )


sql =  "SELECT     tblExample1.IDNumber, tblExample2.First_Name, tblExample3.Main_Name FROM         tblExample1 INNER JOIN tblExample2 ON tblExample1.IDNumber = tblExample2.Entity_ID_Number WHERE     (tblExample1.IDNumber = '2') AND (Process_Date IS NULL)"

rsGetInfo.Open sql, dbConnection
IF rsGetInfo.EOF THEN
ELSE

FileName = "MyFileName_" & Replace( Date() , "/" , "")
'//Create the file
SET fExportFile = fsoFileSystem.CreateTextFile( FileLocation & FileName & ".txt", true )
DTSGlobalVariables("FileLocation").Value =  FileLocation & FileName & ".txt"    

    rsGetInfo.MoveFirst
    DO WHILE NOT  rsGetInfo.EOF OR rsGetInfo.BOF
        sLineItem = ""
        sLineItem = rsGetInfo("IDNumber") & vbtab & rsGetDelistInfo("First_Name") & vbtab & rsGetInfo("Main_Name")

        fExportFile.Write(sLineItem & vbcrlf)

        rsGetInfo.MoveNext
    LOOP
    '// Set Attachment Path     
    sAttachmentPaths = FileLocation & FileName & ".txt" 
END IF


'*******************************************************************
'***********GET DEFAULT DELIST DATA************
'*******************************************************************


SET fsoFileSystem = CreateObject( "Scripting.FileSystemObject" )
SET rsGetInfo = CreateObject( "ADODB.recordset" )


sql =  "SELECT     Contract_No FROM      tblfunny WHERE     (funnyNumb = 1) and (Process_Date IS NULL)"

rsGetInfo.Open sql, dbConnection

IF rsGetInfo.EOF THEN
ELSE

    FileName = "MyFileName_" & Replace( Date() , "/" , "")
    '//Create the file
    SET fExportFile = fsoFileSystem.CreateTextFile(  FileLocation & FileName & ".txt", true )
    DTSGlobalVariables("FileLocation").Value =  FileLocation & FileName & ".txt"    
    rsGetInfo.MoveFirst

    DO WHILE NOT  rsGetInfo.EOF OR rsGetInfo.BOF
        sLineItem = ""
        sLineItem = rsGetInfo("Contract_No")

        fExportFile.Write(sLineItem & vbcrlf)

        rsGetInfo.MoveNext
    LOOP
    '// Set Attachment Path
    IF sAttachmentPaths = "" THEN
        sAttachmentPaths = FileLocation & FileName & ".txt"
    ELSE
        sAttachmentPaths = sAttachmentPaths & "; "& FileLocation & FileName & ".txt"
    END IF
END IF
Main = DTSTaskExecResult_Success 

End Function

I'm trying to convert a DTS process to SSIS and I want to convert a DTS script task that creates 3 files (only creates the file if there’s data to write to it) and then appends the full file names to a variable, which is then copied to a global variable. Since I'm new to this I would like a step by step demonstation if possible, Please, Please, Pretty Please help...Thanks. Below is an example of the DTS script that creates two instead of three files:

Function Main()
DIM dbConnection, rsGetFileDetails, rsGetInfo
DIM fsoFileSystem, fExportFile
DIM sGroupCode, sSequenceNumber, sFileIdentifier, FileName
DIM sLineItem, sAttachmentPaths
DIM  FileLocation
sAttachmentPaths = ""
FileLocation = DTSGlobalVariables("FileDestPath").Value

'****************************************************
'*********Connect to Database******************
'****************************************************
SET dbConnection = CreateObject( "ADODB.Connection" )
dbConnection.Provider = "sqloledb"
dbConnection.Properties("Data Source").Value = "Server"
dbConnection.Properties("Initial Catalog").Value = "Database"
dbConnection.Properties("Integrated Security").Value = "1234"
dbConnection.Open 

'*******************************************************************
'***********GET TRACE ALERT DELIST DATA************
'*******************************************************************


SET fsoFileSystem = CreateObject( "Scripting.FileSystemObject" )
SET rsGetInfo = CreateObject( "ADODB.recordset" )


sql =  "SELECT     tblExample1.IDNumber, tblExample2.First_Name, tblExample3.Main_Name FROM         tblExample1 INNER JOIN tblExample2 ON tblExample1.IDNumber = tblExample2.Entity_ID_Number WHERE     (tblExample1.IDNumber = '2') AND (Process_Date IS NULL)"

rsGetInfo.Open sql, dbConnection
IF rsGetInfo.EOF THEN
ELSE

FileName = "MyFileName_" & Replace( Date() , "/" , "")
'//Create the file
SET fExportFile = fsoFileSystem.CreateTextFile( FileLocation & FileName & ".txt", true )
DTSGlobalVariables("FileLocation").Value =  FileLocation & FileName & ".txt"    

    rsGetInfo.MoveFirst
    DO WHILE NOT  rsGetInfo.EOF OR rsGetInfo.BOF
        sLineItem = ""
        sLineItem = rsGetInfo("IDNumber") & vbtab & rsGetDelistInfo("First_Name") & vbtab & rsGetInfo("Main_Name")

        fExportFile.Write(sLineItem & vbcrlf)

        rsGetInfo.MoveNext
    LOOP
    '// Set Attachment Path     
    sAttachmentPaths = FileLocation & FileName & ".txt" 
END IF


'*******************************************************************
'***********GET DEFAULT DELIST DATA************
'*******************************************************************


SET fsoFileSystem = CreateObject( "Scripting.FileSystemObject" )
SET rsGetInfo = CreateObject( "ADODB.recordset" )


sql =  "SELECT     Contract_No FROM      tblfunny WHERE     (funnyNumb = 1) and (Process_Date IS NULL)"

rsGetInfo.Open sql, dbConnection

IF rsGetInfo.EOF THEN
ELSE

    FileName = "MyFileName_" & Replace( Date() , "/" , "")
    '//Create the file
    SET fExportFile = fsoFileSystem.CreateTextFile(  FileLocation & FileName & ".txt", true )
    DTSGlobalVariables("FileLocation").Value =  FileLocation & FileName & ".txt"    
    rsGetInfo.MoveFirst

    DO WHILE NOT  rsGetInfo.EOF OR rsGetInfo.BOF
        sLineItem = ""
        sLineItem = rsGetInfo("Contract_No")

        fExportFile.Write(sLineItem & vbcrlf)

        rsGetInfo.MoveNext
    LOOP
    '// Set Attachment Path
    IF sAttachmentPaths = "" THEN
        sAttachmentPaths = FileLocation & FileName & ".txt"
    ELSE
        sAttachmentPaths = sAttachmentPaths & "; "& FileLocation & FileName & ".txt"
    END IF
END IF
Main = DTSTaskExecResult_Success 

End Function

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文