Chilkat Examples

ChilkatHOME.NET Core C#Android™AutoItCC#C++Chilkat2-PythonCkPythonClassic ASPDataFlexDelphi ActiveXDelphi DLLGoJavaLianjaMono C#Node.jsObjective-CPHP ActiveXPHP ExtensionPerlPowerBuilderPowerShellPureBasicRubySQL ServerSwift 2Swift 3,4,5...TclUnicode CUnicode C++VB.NETVBScriptVisual Basic 6.0Visual FoxProXojo Plugin

SQL Server Examples

Web API Categories

ASN.1
AWS KMS
AWS Misc
Amazon EC2
Amazon Glacier
Amazon S3
Amazon S3 (new)
Amazon SES
Amazon SNS
Amazon SQS
Async
Azure Cloud Storage
Azure Key Vault
Azure Service Bus
Azure Table Service
Base64
Bounced Email
Box
CAdES
CSR
CSV
Certificates
Code Signing
Compression
DKIM / DomainKey
DNS
DSA
Diffie-Hellman
Digital Signatures
Dropbox
Dynamics CRM
EBICS
ECC
Ed25519
Email Object
Encryption
FTP
FileAccess
Firebase
GMail REST API
GMail SMTP/IMAP/POP
Geolocation
Google APIs
Google Calendar
Google Cloud SQL
Google Cloud Storage
Google Drive
Google Photos
Google Sheets
Google Tasks
Gzip
HTML-to-XML/Text
HTTP

HTTP Misc
IMAP
JSON
JSON Web Encryption (JWE)
JSON Web Signatures (JWS)
JSON Web Token (JWT)
Java KeyStore (JKS)
MHT / HTML Email
MIME
MS Storage Providers
Microsoft Graph
Misc
NTLM
OAuth1
OAuth2
OIDC
Office365
OneDrive
OpenSSL
Outlook
Outlook Calendar
Outlook Contact
PDF Signatures
PEM
PFX/P12
PKCS11
POP3
PRNG
REST
REST Misc
RSA
SCP
SCard
SFTP
SMTP
SSH
SSH Key
SSH Tunnel
ScMinidriver
SharePoint
SharePoint Online
Signing in the Cloud
Socket/SSL/TLS
Spider
Stream
Tar Archive
ULID/UUID
Upload
WebSocket
XAdES
XML
XML Digital Signatures
XMP
Zip
curl
uncategorized

 

 

 

(SQL Server) Async Task Chain (another example)

Demonstrates using a task chain to run a sequence of FTP tasks asynchronously.

Chilkat ActiveX Downloads

ActiveX for 32-bit and 64-bit Windows

// Important: See this note about string length limitations for strings returned by sp_OAMethod calls.
//
CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int
    DECLARE @iTmp0 int
    DECLARE @sTmp0 nvarchar(4000)
    -- All Chilkat classes can be unlocked at once at the beginning of a program
    -- by calling UnlockBundle.  It requires a Bundle unlock code.
    DECLARE @chilkatGlob int
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.Global', @chilkatGlob OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    DECLARE @success int
    EXEC sp_OAMethod @chilkatGlob, 'UnlockBundle', @success OUT, 'Anything for 30-day trial.'
    IF @success <> 1
      BEGIN
        EXEC sp_OAGetProperty @chilkatGlob, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @chilkatGlob
        RETURN
      END

    DECLARE @ftp int
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.Ftp2', @ftp OUT

    EXEC sp_OASetProperty @ftp, 'Hostname', 'ftp.example.com'
    EXEC sp_OASetProperty @ftp, 'Username', 'login'
    EXEC sp_OASetProperty @ftp, 'Password', 'password'

    -- Connect and login to the FTP server.
    EXEC sp_OAMethod @ftp, 'Connect', @success OUT
    IF @success <> 1
      BEGIN
        EXEC sp_OAGetProperty @ftp, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @chilkatGlob
        EXEC @hr = sp_OADestroy @ftp
        RETURN
      END

    DECLARE @taskChain int
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.TaskChain', @taskChain OUT

    -- Create a task to change to the remote directory where the file will be uploaded.
    DECLARE @task int
    EXEC sp_OAMethod @ftp, 'ChangeRemoteDirAsync', @task OUT, 'junk'
    EXEC sp_OAGetProperty @ftp, 'LastMethodSuccess', @iTmp0 OUT
    IF @iTmp0 = 0
      BEGIN
        EXEC sp_OAGetProperty @ftp, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @chilkatGlob
        EXEC @hr = sp_OADestroy @ftp
        EXEC @hr = sp_OADestroy @taskChain
        RETURN
      END
    -- Add this task to the task chain.
    EXEC sp_OAMethod @taskChain, 'Append', @success OUT, @task
    EXEC @hr = sp_OADestroy @task

    -- Create a task to upload a file.
    DECLARE @localFilename nvarchar(4000)
    SELECT @localFilename = 'c:/temp/hamlet.xml'
    DECLARE @remoteFilename nvarchar(4000)
    SELECT @remoteFilename = 'hamlet.xml'

    EXEC sp_OAMethod @ftp, 'PutFileAsync', @task OUT, @localFilename, @remoteFilename
    EXEC sp_OAGetProperty @ftp, 'LastMethodSuccess', @iTmp0 OUT
    IF @iTmp0 = 0
      BEGIN
        EXEC sp_OAGetProperty @ftp, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @chilkatGlob
        EXEC @hr = sp_OADestroy @ftp
        EXEC @hr = sp_OADestroy @taskChain
        RETURN
      END

    -- Add this task to the task chain.
    EXEC sp_OAMethod @taskChain, 'Append', @success OUT, @task
    EXEC @hr = sp_OADestroy @task

    -- Start the task chain running in a background thread.
    -- Each task is run one after the other (on the same background thread) until all tasks have completed.
    -- The task chain will stop at the first task that fails.
    EXEC sp_OASetProperty @taskChain, 'StopOnFailedTask', 1
    EXEC sp_OAMethod @taskChain, 'Run', @success OUT
    IF Not @success
      BEGIN
        EXEC sp_OAGetProperty @taskChain, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @chilkatGlob
        EXEC @hr = sp_OADestroy @ftp
        EXEC @hr = sp_OADestroy @taskChain
        RETURN
      END

    -- The application is now free to do anything else
    -- while the FTP commands are being run...

    -- For this example, we'll simply sleep and periodically
    -- check to see if the taskchain if finished. 
    EXEC sp_OAGetProperty @taskChain, 'Finished', @iTmp0 OUT
    WHILE @iTmp0 <> 1
      BEGIN

        -- Sleep 100 ms.
        EXEC sp_OAMethod @taskChain, 'SleepMs', NULL, 100

      END

    -- A finished task chain could be one that was canceled, aborted, or truly finished.  

    -- If the task chain "completed", then it ran to completion.  A "completed" task will
    -- have a StatusInt equal to 7.   If the task finished, but was not completed, then it must've
    -- been aborted or canceled:
    EXEC sp_OAGetProperty @taskChain, 'StatusInt', @iTmp0 OUT
    IF @iTmp0 <> 7
      BEGIN

        PRINT 'Task did not complete.'

        EXEC sp_OAGetProperty @taskChain, 'Status', @sTmp0 OUT
        PRINT 'task chain status: ' + @sTmp0
        EXEC @hr = sp_OADestroy @chilkatGlob
        EXEC @hr = sp_OADestroy @ftp
        EXEC @hr = sp_OADestroy @taskChain
        RETURN
      END

    -- If we got to this point, the ChangeRemoteDir and PutFile were successful.
    -- We can visually verify by examining the LastErrorText that was recorded for each
    -- of these method calls..
    DECLARE @numTasks int
    EXEC sp_OAGetProperty @taskChain, 'NumTasks', @numTasks OUT
    DECLARE @taskIdx int
    SELECT @taskIdx = 0

    WHILE (@taskIdx < @numTasks)
      BEGIN

        EXEC sp_OAMethod @taskChain, 'GetTask', @task OUT, @taskIdx

        -- Examine the status of this task, and the ResultErrorText
        -- (the ResultErrorText is the ftp.LastErrorText captured for FTP method called by the task).
        -- Everything should indicate success.

        EXEC sp_OAGetProperty @task, 'Status', @sTmp0 OUT
        PRINT 'task status: ' + @sTmp0

        EXEC sp_OAGetProperty @task, 'ResultErrorText', @sTmp0 OUT
        PRINT 'task log: ' + @sTmp0

        EXEC @hr = sp_OADestroy @task

        SELECT @taskIdx = @taskIdx + 1
      END

    EXEC sp_OAMethod @ftp, 'Disconnect', @success OUT

    EXEC @hr = sp_OADestroy @chilkatGlob
    EXEC @hr = sp_OADestroy @ftp
    EXEC @hr = sp_OADestroy @taskChain


END
GO

 

© 2000-2024 Chilkat Software, Inc. All Rights Reserved.