How to Connect to Salesforce in .NET with C#
Salesforce has become the backbone for many businesses, centralizing customer data and keeping workflows in perfect sync. But no tool works in isolation. To get the most out of Salesforce, you need to connect this platform with the rest of your tech stack.
That's where ADO.NET steps in. By treating Salesforce data like a traditional database, ADO.NET allows developers to access, query, and manage data with the tools they already know. For easier and smoother connection and performance, you can refer to dotConnect for Salesforce - a high-performance data provider that ensures connectivity with real-time queries, bulk data synchronization, and support for Salesforce's REST API.
Why dotConnect for Salesforce
dotConnect for Salesforce is built to simplify integration between .NET applications and Salesforce. It allows the application developers to connect to Salesforce directly and perform data-related operations efficiently, handling real-time queries and processing large datasets.
The compatibility with ORM frameworks like Entity Framework also enables developers to simplify all their workflows and work with Salesforce data in an object-oriented way.
Prerequisites
Before you begin, make sure you have the following prerequisites at place:
- Visual Studio 2022: This is the IDE of choice for .NET development. If you don't have it installed, you can download the free Community Edition from the official Microsoft website.
- dotConnect for Salesforce: A high-performance ADO.NET data provider for Salesforce with an enhanced ORM support and flawless database connectivity.
- Salesforce account: An active Salesforce account with the necessary permissions for accessing and managing data.
Download and activate dotConnect for Salesforce
30-day free trial version
Download and install dotConnect for Salesforce directly on your machine, or install the Devart.Data.Salesforce NuGet package.
No license key is required, and you can start exploring the product immediately.
Full version
After purchasing the full version, go to your profile's Licenses page. Choose your product and click Details. Here, you'll find the license details and the Activation Key.
To activate a connection in your application, add the License Key to your connection string.
Get the Salesforce Security API token
To connect to Salesforce and access the data, you need your valid credentials: the User ID, the password, and the security token.
To obtain that token, sign in to your Salesforce account, go to your profile, and select Settings.
Navigate to Reset My Security Token and request it. A new token will be sent to your email.

Use this Security Token for API access when connecting to Salesforce
Install NuGet packages
To integrate Salesforce with your C# application, you need to install the Devart.Data.Salesforce NuGet package.
- 1. In Visual Studio, navigate to Tools > NuGet Package Manager > Manage NuGet packages for Solution.
- 2. In the NuGet Package Manager window, go to the Browse tab and search for Devart.Data.Salesforce. When you find it, click Install.
- 3. Once the packages are installed, check the Installed tab to confirm if Devart.Data.Salesforce is listed.

This process will integrate the necessary components into your project for working with Salesforce data.
Set up the project and establish a connection
To connect your C# application to Salesforce, you need the Devart.Data.Salesforce library. The connection is established through the SalesforceConnection object. You need to provide a connection string with the necessary authentication details:
- Authentication Type - the authentication method (in this case, a username and password).
- Host - the Salesforce login URL. For production and developer environments, it is login.salesforce.com.
- UserId - the Salesforce username.
- Password - the password for the Salesforce account.
- Security Token - the security token provided by Salesforce for API access. If you don’t have one, generate it from the Salesforce settings.
- License Key - the Salesforce license key (if applicable).
Have a look at the example of the code below (make sure to replace the placeholders with your actual credentials):
string connectionString = "Authentication Type=UserNamePassword;" + "Host=salesforce.com;" + "User Id=user@test.com;" + "Password=**********;" + "Security Token=**********;" + "License Key=**********";
Fetch Salesforce data with an SQL query
Now that your connection is set up, you can pull data from Salesforce. Here’s an example SQL query to retrieve specific fields (Id, Name, Phone, and Website columns) from the Account table:
string query = "SELECT Id, Name, Phone, Website FROM Account";
You can modify this query according to your needs and retrieve additional fields, filter results with a WHERE clause, or sort the data. The SalesforceDataReader ADO.NET class allows you to perform this task and use the results in your application.
To run a query against Salesforce, you need to create a SalesforceCommand object and tie it to your query and active Salesforce connection. Using ExecuteReader on the command gives you the SalesforceDataReader to read the query results one row at a time.
See the basic example below:
using (SalesforceCommand command = new SalesforceCommand(query, salesforceConnection)) { using (SalesforceDataReader reader = command.ExecuteReader()) { // Read and process data } }
After running the query, the SalesforceDataReader class lets you read the results one row at a time. This approach is ideal for large datasets because it is memory-efficient. For each row, you can extract the values of fields like Id, Name, Phone, and Website. If a field is missing data, you can set it to a default value, like "N/A", as shown below.
while (reader.Read()) { string id = reader["Id"]?.ToString() ?? "N/A"; string name = reader["Name"]?.ToString() ?? "N/A"; string phone = reader["Phone"]?.ToString() ?? "N/A"; string website = reader["Website"]?.ToString() ?? "N/A"; Console.WriteLine($"Id: {id}"); Console.WriteLine($"Name: {name}"); Console.WriteLine($"Phone: {phone}"); Console.WriteLine($"Website: {website}"); }
This loop processes each row of data and prints it in a readable format. It’s straightforward and ensures that if any field is missing, it won’t break your output.
Connect to Salesforce using dotConnect
Let us see how to connect using dotConnect for Salesforce, retrieve data from Salesforce using SQL-like queries, and display results in a C# console application. dotConnect uses Salesforce's API to access and manipulate Salesforce data in .NET applications.
using System; using Devart.Data.Salesforce; namespace SalesforceConsoleApp { class Program { static void Main(string[] args) { string connectionString = "Authentication Type=UserNamePassword;" + "Host=salesforce.com;" + "User Id=user@test.com;" + "Password=**********;" + "Security Token=**********;" + "License Key=**********;"; try { using (SalesforceConnection salesforceConnection = new SalesforceConnection()) { salesforceConnection.ConnectionString = connectionString; salesforceConnection.Open(); string query = "SELECT Id, Name, Phone, Website FROM Account"; using (SalesforceCommand command = new SalesforceCommand(query, salesforceConnection)) { using (SalesforceDataReader reader = command.ExecuteReader()) { if (reader.HasRows) { while (reader.Read()) { string id = reader["Id"]?.ToString() ?? "N/A"; string name = reader["Name"]?.ToString() ?? "N/A"; string phone = reader["Phone"]?.ToString() ?? "N/A"; string website = reader["Website"]?.ToString() ?? "N/A"; // Process the data (e.g., store in a list or database) } } } } salesforceConnection.Close(); } } catch (Exception) { // Handle exceptions (log or rethrow) } } } }
In this query, the SalesforceConnection class opens a connection to Salesforce, the SalesforceCommand class prepares and executes the SQL query, and the SalesforceDataReader class iterates over the query results row by row, allowing you to process each row.
After successfully connecting to Salesforce, the application fetches and displays the data from the Account table in the console.

Video tutorial: How to Connect to Salesforce in .NET with C#
Conclusion
dotConnect for Salesforce is the optimal solution when you need to connect your C# application to Salesforce via ADO.NET. It simplifies what used to be a complex process and lets you integrate Salesforce data smoothly into your .NET applications. This way, you save time and can focus on what matters most—building better applications.
On top of that, advanced features like LINQ and Entity Framework give you the power to take full advantage of your data. To experience the full potential of dotConnect for Salesforce, download the free trial and see how it handles your specific workload!