dotConnect for Salesforce Documentation
In This Topic
    Query Preparation
    In This Topic

    Query preparation is a complex process, that optimizes execution of the prepared statement. In dotConnect for Salesforce this process is even more complex because of the data source specificity. It takes longer and requires more resources than query preparation on usual relational databases. It is recommended to prepare only queries that will be executed multiple times in your application. Only in this case query preparation will give you performance gain.

    using (SalesforceConnection connection = new SalesforceConnection(
      "Server=login.salesforce.com;User Id= [email protected];Password=mypassword;Security Token=qweASDzcx1234567890rtyui;"
    )) {
      connection.Open();
      using (SalesforceCommand command = connection.CreateCommand()) {
    
        command.CommandText = "SELECT Phone FROM Account WHERE Name = :p1";
        command.Parameters.Add("p1", SalesforceType.String);
        command.Prepare();
    
        string[] accountNames = {"Account 1", "Account 2", "Account 3"};
    
        for (int i = 0; i < accountNames.Length; i++) {
          command.Parameters[0].Value = accountNames[i];
          string phone = (string)command.ExecuteScalar();
          Console.WriteLine(phone);
        }
      }
    }
    
    
    Using connection As New SalesforceConnection( _
        "Server=login.salesforce.com;User Id= [email protected];" & _
        "Password=mypassword;Security Token=qweASDzcx1234567890rtyui;")
      connection.Open()
      Using command As SalesforceCommand = connection.CreateCommand()
    
        command.CommandText = "SELECT Phone FROM Account WHERE Name = :p1"
        command.Parameters.Add("p1", SalesforceType.[String])
        command.Prepare()
    
        Dim accountNames As String() = {"Account 1", "Account 2", "Account 3"}
    
        For i As Integer = 0 To accountNames.Length - 1
          command.Parameters(0).Value = accountNames(i)
          Dim phone As String = DirectCast(command.ExecuteScalar(), String)
          Console.WriteLine(phone)
        Next
      End Using
    End Using
    
    

    Prepared queries depend on cache that existed at the moment they were prepared. If the cache for a table was deleted, the prepared statement will throw an exception when it is executed. You need to prepare the statement again to use it.

    If a table that is used in the prepared query was added to the cache after the query was prepared, its data will still be queried from the Salesforce.com or Database.com when executing the prepared query. The cache will not be used for this query. You need to prepare the query again to query cached data.