Wed, 06 June 2018
Connecting F# to Salesforce.com
The Salesforce.com ODBC Driver enables you to work with Salesforce.com data from F#. Note that the procedure outlined in this blog is the same for any ODBC driver, so it's still relevant even if you are using F# with another ODBC driver.
The Salesforce.com ODBC Driver is available to download from the Easysoft web site:
- Download the Windows Salesforce.com ODBC Driver. (Registration required.)
- Install and license the Salesforce.com ODBC Driver on the machine where is installed.
For installation instructions, see the Salesforce.com ODBC Driver documentation.
Before you can use the Salesforce.com ODBC Driver to connect F# to Salesforce.com, you need to configure an ODBC data source. An ODBC data source stores the connection details for the target database (e.g. Salesforce.com) and the ODBC driver that is required to connect to it (e.g. the Salesforce.com ODBC driver).
To create a Salesforce.com ODBC Driver data source:
- In the 32-bit version of ODBC Administrator, choose the System DSN tab, and then choose Add.
To run the 32-bit version of ODBC Administrator, in the Windows Run dialog box, enter:
- In the Create New Data Source dialog box, choose Easysoft ODBC-Salesforce Driver, and then choose Finish.
- Complete the Easysoft ODBC-Salesforce Driver DSN Setup dialog box:
Setting Value DSN Salesforce.com User Name The name of your Salesforce.com user. For example, firstname.lastname@example.org. Password The password for your Salesforce.com user. Token The security token for your Salesforce.com user, if required.
To find out whether you need to supply a security token, choose the Test button. If the connection attempt fails with an error which contains
LOGIN_MUST_USE_SECURITY_TOKEN, you need to supply one.
Salesforce.com emails the security token to the email address associated with your Salesforce.com user account. If you have not received a security token, you can regenerate it. Salesforce.com will then email the new security token to you. To regenerate your security token, log in to Salesforce.com and then choose Setup from the user menu. Search for "security token" in the Quick Find box. Click Reset Security Token in the Reset Security Token page. When you receive the token in your email client, copy it and then paste it into the Token field.
- Use the Test button to verify that you can successfully connect to Salesforce.com.
- In Visual Studio, create a new F# console application.
- Right-click the Solution Explorer pane. From the shortcut menu, choose Add > Reference.
The Reference Manager dialog box is displayed.
- Choose System.Data from the list, and then choose OK.
- In the ConsoleApplication1 pane, add this code:
open System open System.Data.Odbc let connectToDb() = // Salesforce ODBC Data Source let connectionString = "DSN=Salesforce" new OdbcConnection(connectionString) let getODBCData()= let connection = connectToDb() let tableName = "Account" let sqlQuery = "SELECT * FROM " + tableName let cmd = new OdbcCommand(sqlQuery,connection) connection.Open() let result = cmd.ExecuteReader() // Retrieve the first column from the Account table if (result.HasRows) then while result.Read() do Console.WriteLine(result.GetValue(0)) else Console.WriteLine("No rows found.") result.Close() connection.Close() Console.ReadKey() [<EntryPoint>] let main argv = let salesforceData = getODBCData() 0
- From the Build menu, choose Build Solution.
- Choose the Start button.