Sample code for 30+ languages & platforms
SQL Server

QuickGetBd Example

The QuickGetBd method is called to send a GET request to download a binary target, such as PDF, zip, image file, etc. into a Chilkat BinData object.

Chilkat SQL Server Downloads

SQL Server
-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls.
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int
    -- Important: Do not use nvarchar(max).  See the warning about using nvarchar(max).
    DECLARE @sTmp0 nvarchar(4000)
    DECLARE @success int
    SELECT @success = 0

    -- This example assumes the Chilkat API to have been previously unlocked.
    -- See Global Unlock Sample for sample code.

    DECLARE @http int
    EXEC @hr = sp_OACreate 'Chilkat.Http', @http OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    EXEC sp_OASetProperty @http, 'KeepResponseBody', 1

    DECLARE @bd int
    EXEC @hr = sp_OACreate 'Chilkat.BinData', @bd OUT

    -- Download the contents of a PDF file into the bd object.
    EXEC sp_OAMethod @http, 'QuickGetBd', @success OUT, 'https://www.chilkatsoft.com/hello.pdf', @bd
    IF @success = 0
      BEGIN
        -- This will happen if the response status code was 400 or greater,
        -- or if the request could not be sent, or if no response was received.
        -- 
        -- In other words, success will only be 1 if bd contains the desired content at the URL (not an error response).
        DECLARE @statusCode int
        EXEC sp_OAGetProperty @http, 'LastStatus', @statusCode OUT

        PRINT 'Response status: ' + @statusCode

        IF @statusCode = 0
          BEGIN
            -- There was an error in communications.  
            EXEC sp_OAGetProperty @http, 'LastErrorText', @sTmp0 OUT
            PRINT @sTmp0
          END
        ELSE
          BEGIN
            -- We received a response status code indicating failure.
            -- Examine the response body.
            EXEC sp_OAGetProperty @http, 'LastResponseBody', @sTmp0 OUT
            PRINT @sTmp0
          END

        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @bd
        RETURN
      END

    -- Load the downloaded PDF into a Chilkat PDF object.
    DECLARE @pdf int
    EXEC @hr = sp_OACreate 'Chilkat.Pdf', @pdf OUT

    EXEC sp_OAMethod @pdf, 'LoadBd', @success OUT, @bd
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @pdf, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @bd
        EXEC @hr = sp_OADestroy @pdf
        RETURN
      END

    -- ...


    PRINT 'Success.'

    EXEC @hr = sp_OADestroy @http
    EXEC @hr = sp_OADestroy @bd
    EXEC @hr = sp_OADestroy @pdf


END
GO