Sample code for 30+ languages & platforms
SQL Server

Download an FTP Text File into a StringBuilder

See more FTP Examples

Demonstrates the Chilkat Ftp2.GetFileSb method, which downloads a remote text file, decodes it with a charset, and appends the text to a StringBuilder. The arguments are the remote file path, the charset, and the StringBuilder.

Background: When the remote file is text and you want its content as characters, this decodes it in one step so you can parse or display it directly. The charset must match the file's actual encoding or the text will be garbled. Like GetFileBd it appends, so clear the StringBuilder first if you want only this file's text.

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
    DECLARE @iTmp0 int
    DECLARE @sTmp0 nvarchar(4000)
    DECLARE @success int
    SELECT @success = 0

    --  Demonstrates the Ftp2.GetFileSb method, which downloads a remote text file, decodes it, and
    --  appends the text to a StringBuilder.  The 1st argument is the remote file path, the 2nd is the
    --  charset, and the 3rd is the StringBuilder.

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

    EXEC sp_OASetProperty @ftp, 'Hostname', 'ftp.example.com'
    EXEC sp_OASetProperty @ftp, 'Username', 'myFtpLogin'

    --  Normally you would not hard-code the password in source.  You should instead obtain it
    --  from an interactive prompt, environment variable, or a secrets vault.
    EXEC sp_OASetProperty @ftp, 'Password', 'myPassword'

    EXEC sp_OAMethod @ftp, 'Connect', @success OUT
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @ftp, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @ftp
        RETURN
      END

    --  The StringBuilder is not cleared automatically, so clear it first for replacement behavior.
    DECLARE @sb int
    EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @sb OUT

    EXEC sp_OAMethod @ftp, 'GetFileSb', @success OUT, 'public_html/data.csv', 'utf-8', @sb
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @ftp, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @ftp
        EXEC @hr = sp_OADestroy @sb
        RETURN
      END


    EXEC sp_OAGetProperty @sb, 'Length', @iTmp0 OUT
    PRINT 'Characters downloaded: ' + @iTmp0
    DECLARE @fileText nvarchar(4000)
    EXEC sp_OAMethod @sb, 'GetAsString', @fileText OUT

    PRINT @fileText

    EXEC sp_OAMethod @ftp, 'Disconnect', @success OUT
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @ftp, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @ftp
        EXEC @hr = sp_OADestroy @sb
        RETURN
      END

    EXEC @hr = sp_OADestroy @ftp
    EXEC @hr = sp_OADestroy @sb


END
GO