Sample code for 30+ languages & platforms
SQL Server

FTPS with Client Cert from Windows Certificate Store

See more FTP Examples

Demonstrates how to do mutual TLS authentication using a client certificate installed in the Windows certificate store.

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 requires the Chilkat API to have been previously unlocked.
    -- See Global Unlock Sample for sample code.

    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'

    -- If using implicit TLS, you probably want port 990..
    EXEC sp_OASetProperty @ftp, 'Port', 990

    -- Set this to 0 for implicit TLS, otherwise set to 1 for explicit TLS (where port is typically 21).
    EXEC sp_OASetProperty @ftp, 'AuthTls', 0

    -- Set this to 1 for implicit TLS, otherwise set to 0.
    EXEC sp_OASetProperty @ftp, 'Ssl', 1

    DECLARE @cert int
    EXEC @hr = sp_OACreate 'Chilkat.Cert', @cert OUT

    EXEC sp_OAMethod @cert, 'LoadByCommonName', @success OUT, 'The common name of your certificate'
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @cert, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @ftp
        EXEC @hr = sp_OADestroy @cert
        RETURN
      END

    -- Use this certificate for our TLS mutually authenticated connection:
    EXEC sp_OAMethod @ftp, 'SetSslClientCert', @success OUT, @cert
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @cert, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @ftp
        EXEC @hr = sp_OADestroy @cert
        RETURN
      END

    -- Establish the TLS connection with the FTP server.
    EXEC sp_OAMethod @ftp, 'ConnectOnly', @success OUT
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @ftp, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @ftp
        EXEC @hr = sp_OADestroy @cert
        RETURN
      END

    -- If a login is required, then login with the FTP account login/password.
    EXEC sp_OASetProperty @ftp, 'Username', 'myLogin'
    EXEC sp_OASetProperty @ftp, 'Password', 'myPassword'
    EXEC sp_OAMethod @ftp, 'LoginAfterConnectOnly', @success OUT
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @ftp, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @ftp
        EXEC @hr = sp_OADestroy @cert
        RETURN
      END

    -- Do whatever you're doing to do ...
    -- upload files, download files, etc...

    -- .....
    -- .....

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

    EXEC @hr = sp_OADestroy @ftp
    EXEC @hr = sp_OADestroy @cert


END
GO