Sample code for 30+ languages & platforms
SQL Server

Upload Text to an FTP Server (StringBuilder)

See more FTP Examples

Demonstrates the Chilkat Ftp2.PutFileSb method, which encodes the text in a StringBuilder and uploads it to a remote path. The arguments are the StringBuilder, the charset, whether to include a byte-order mark, and the remote file path.

Background: The text-oriented upload: assemble a document — CSV, config, JSON — in a StringBuilder and the method handles encoding as it writes. The charset governs the bytes actually stored, and the byte-order-mark flag is useful when a consuming Windows application expects a BOM.

Chilkat SQL Server Downloads

SQL Server
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int
    DECLARE @sTmp0 nvarchar(4000)
    DECLARE @success int
    SELECT @success = 0

    --  Demonstrates the Ftp2.PutFileSb method, which encodes the text in a StringBuilder and uploads
    --  it to a remote path.  The 1st argument is the StringBuilder, the 2nd is the charset, the 3rd
    --  selects whether a byte-order mark is included, and the 4th is the remote file path.

    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

    --  Build the text to upload.
    DECLARE @sb int
    EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @sb OUT

    EXEC sp_OAMethod @sb, 'Append', @success OUT, 'name,quantity' + CHAR(10)
    EXEC sp_OAMethod @sb, 'Append', @success OUT, 'widgets,42' + CHAR(10)

    --  Upload as UTF-8 without a byte-order mark.
    DECLARE @includeBom int
    SELECT @includeBom = 0
    EXEC sp_OAMethod @ftp, 'PutFileSb', @success OUT, @sb, 'utf-8', @includeBom, 'public_html/inventory.csv'
    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

    PRINT 'Uploaded text file.'

    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