dotConnect for Salesforce Documentation
On this page
    Retrieving Data
    On this page

    Introduction

    This tutorial describes how to use the SalesforceCommand, SalesforceDataReader, and SalesforceDataAdapter components.

    Requirements

    This tutorial assumes that you know how to connect to the server.

    Note that if you don't use design time (specifically, if you don't place a SalesforceConnection component from the toolbox on a designer), you have to embed license information manually. This is described in the Licensing topic.

    General Information

    The main function of any database application is establishing a connection to a data source and working with the data it contains. ADO.NET data providers serve as a bridge between an application and a data source and allow you to execute commands as well as retrieve data using a DataReader or a DataAdapter. Updating data involves using the Command and DataAdapter objects.

    Retrieving Data using SalesforceCommand and SalesforceDataReader

    In this sample, we use SalesforceCommand and SalesforceDataReader to retrieve data. SalesforceDataReader retrieves data in pages. While you read data from it, it automatically queries the next pages from Salesforce.com or Database.com. SalesforceDataReader offers higher performance than SalesforceDataAdapter, especially when you query a lot of data. For more information, refer to the description of these classes in our documentation.

    using Devart.Data.Salesforce;
    ...
    class Program
    {
        static void Main(string[] args) {
    
            const string connectionString = "Server=login.salesforce.com;User Id= [email protected];Password=mypassword;Security Token=qweASDzcx1234567890rtyui;";
            const string sql = "SELECT FirstName, LastName FROM Contact";
    
            using (SalesforceConnection connection = new SalesforceConnection(connectionString)) {
    
                connection.Open();
                using (SalesforceCommand command = connection.CreateCommand()) {
    
                    command.CommandText = sql;
                    using (SalesforceDataReader reader = command.ExecuteReader()) {
    
                        while (reader.Read()) {
    
                            Console.WriteLine("{0}\t{1}", reader.GetValue(0), reader.GetValue(1));
                        }
                    }
                }
            }
            Console.ReadKey();
        }
    
    }
    
    Imports Devart.Data.Salesforce
    ...
    Module Module1
    
        Sub Main()
    
            Const connectionString As String = "Server=login.salesforce.com;User Id= [email protected];Password=mypassword;Security Token=qweASDzcx1234567890rtyui;"
            Const sql As String = "SELECT FirstName, LastName FROM Contact"
    
            Using connection As New SalesforceConnection(connectionString)
    
                connection.Open()
                Using command As SalesforceCommand = connection.CreateCommand()
    
                    command.CommandText = sql
                    Using reader As SalesforceDataReader = command.ExecuteReader()
    
                        While reader.Read()
    
                            Console.WriteLine(reader.GetValue(0) & vbTab & reader.GetValue(1))
                        End While
                    End Using
                End Using
            End Using
            Console.ReadKey()
        End Sub
    
    End Module
    
    

    Retrieving Data using SalesforceDataAdapter

    Note Applicable only for projects targeting full .NET Framework.

    In this sample, we use the SalesforceDataAdapter component. It retrieves data when you call its Fill method. Note that this method retrieves all the queried data from Salesforce.com or Database.com, which can take some time.

    Here is a small sample that demonstrates how to use SalesforceDataAdapter to retrieve data.

    
        static void Main(string[] args) {
    
            const string connectionString = "Server=login.salesforce.com;User Id= [email protected];Password=mypassword;Security Token=qweASDzcx1234567890rtyui;";
            const string sql = "SELECT FirstName, LastName FROM Contact";
    
            using (SalesforceConnection connection = new SalesforceConnection(connectionString)) {
    
                connection.Open();
                var table = new System.Data.DataTable("Contact");
    
                using (SalesforceCommand command = connection.CreateCommand()) {
    
                    command.CommandText = sql;
                    using (SalesforceDataAdapter adapter = new SalesforceDataAdapter(command)) {
                        adapter.Fill(table);
                    }
                }
    
                foreach (DataRow row in table.Rows) {
                    Console.WriteLine("{0}\t{1}", row[0], row[1]);
                }
            }
            Console.ReadKey();
        }
    
    
    
        Sub Main()
    
            Const connectionString As String = "Server=login.salesforce.com;User Id= [email protected];Password=mypassword;Security Token=qweASDzcx1234567890rtyui;"
            Const sql As String = "SELECT FirstName, LastName FROM Contact"
    
            Using connection As New SalesforceConnection(connectionString)
    
                connection.Open()
                Dim table As New System.Data.DataTable("Campaigns")
    
                Using command As SalesforceCommand = connection.CreateCommand()
    
                    command.CommandText = sql
                    Using adapter As New SalesforceDataAdapter(command)
                        adapter.Fill(table)
                    End Using
                End Using
    
                For Each row As DataRow In table.Rows
                    Console.WriteLine(row(0) & vbTab & row(1))
                Next
            End Using
            Console.ReadKey()
        End Sub
    
    

    Additional Information

    This tutorial describes only the basic ways of working with data. You can also use the Entity Framework ORM technology, which is intended for converting data between incompatible type systems in different data sources and object-oriented programming languages. This technology allows you to reduce the amount of code and maintenance required for data-oriented applications. For more information, see the Entity Framework section.

    See Also

    Entity Framework  | Updating Data