Sample code for 30+ languages & platforms
SQL Server

Upload Text to an SFTP Server (StringBuilder)

See more SFTP Examples

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

Note: This example uses relative local paths, which are resolved against the application's current working directory. Absolute local paths may also be used. Supply the paths appropriate to your own environment.

Background: This is the text-oriented upload: you assemble a document — a CSV, a config file, JSON — in a StringBuilder and the method handles character encoding as it writes. The charset controls the bytes actually stored (for example utf-8 or iso-8859-1), and the byte-order-mark flag is useful when a consuming Windows application expects a BOM. An existing destination is truncated first.

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

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

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

    --  Connect, authenticate, and initialize the SFTP subsystem.
    DECLARE @port int
    SELECT @port = 22
    EXEC sp_OAMethod @sftp, 'Connect', @success OUT, 'sftp.example.com', @port
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @sftp, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @sftp
        RETURN
      END

    --  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.
    DECLARE @password nvarchar(4000)
    SELECT @password = 'mySshPassword'

    EXEC sp_OAMethod @sftp, 'AuthenticatePw', @success OUT, 'mySshLogin', @password
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @sftp, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @sftp
        RETURN
      END

    EXEC sp_OAMethod @sftp, 'InitializeSftp', @success OUT
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @sftp, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @sftp
        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)
    EXEC sp_OAMethod @sb, 'Append', @success OUT, 'gadgets,17' + CHAR(10)

    --  Upload as UTF-8 without a byte-order mark.  If the remote file exists, it is truncated first.
    DECLARE @includeBom int
    SELECT @includeBom = 0
    EXEC sp_OAMethod @sftp, 'UploadSb', @success OUT, @sb, 'subdir/inventory.csv', 'utf-8', @includeBom
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @sftp, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @sftp
        EXEC @hr = sp_OADestroy @sb
        RETURN
      END

    PRINT 'Uploaded text file.'

    EXEC sp_OAMethod @sftp, 'Disconnect', NULL

    EXEC @hr = sp_OADestroy @sftp
    EXEC @hr = sp_OADestroy @sb


END
GO