dotConnect for FreshBooks Documentation
On this page
    Using Parameters
    On this page

    dotConnect for FreshBooks enhances SQL handling capabilities through the use of parameters in SQL queries. You can make execution of a query or stored procedure very flexible using several techniques. This article describes some basics you must know when working with parameters in dotConnect for FreshBooks, as well as parameters synchronization and some nuances related to the use of stored procedures.

    Parameter Basics

    In general, a parameter is a placeholder for a variable that contains a value of a certain type when a general-purpose query is executed, or for arguments and return values when a function is executed. A parameter is represented by the FreshBooksParameter class. All parameters that take part in query execution constitute a collection that can be accessed through the FreshBooksCommand.Parameters property.

    Parameters in the query text must correspond to the names of the FreshBooksParameter instances in the collection. Named parameters are declared using the ':' or '@' prefix followed by the parameter name. Use the name of the FreshBooksParameter object in the command's collection without any prefix. You can use these prefixes in any combination. Named parameters have two main advantages. First, you do not have to care about the order in which parameters are created. Second, a named parameter can appear more than once in the query text, but you have to create only one instance of it in the Parameters collection.

    For example, a simple UPDATE statement with named parameters might look like the following:

    UPDATE Task SET Rate = :rate WHERE Name = :name

    To set parameters for this query, you can use the following code:

    
        using (var connection = new FreshBooksConnection("Server=newcompany1111.freshbooks.com;Authentication Token=d2edadb1d00dfb2ff1209f0409952e70;")) {
            connection.Open();
    
            FreshBooksCommand cmd = connection.CreateCommand();
            cmd.CommandText = "UPDATE Task SET Rate = :rate WHERE Name = :name";
            cmd.Parameters.Add("rate", DbType.Decimal).Value = 100.0m;
            cmd.Parameters.Add("name", DbType.String).Value = "sample name 1";
    
            cmd.ExecuteNonQuery();
        }
    
    
    
        Using connection As New FreshBooksConnection("Server=newcompany1111.freshbooks.com;Authentication Token=d2edadb1d00dfb2ff1209f0409952e70;")
            connection.Open()
    
            Dim cmd As FreshBooksCommand = connection.CreateCommand()
            cmd.CommandText = "UPDATE Task SET Rate = :rate WHERE Name = :name"
            cmd.Parameters.Add("rate", DbType.Decimal).Value = 100.0D
            cmd.Parameters.Add("name", DbType.String).Value = "sample name 1"
    
            cmd.ExecuteNonQuery()
        End Using
    
    
    
    

    Using Automatic Parameter Synchronization

    The behavior described above assumes that FreshBooksCommand.ParameterCheck is false (by default). If you set it to true, you enable automatic synchronization of the query text and the FreshBooksCommand.Parameters collection. In this mode, all input parameters are checked for validity, new ones are added if necessary, and redundant parameters are deleted. Thus, you do not have to care about the number of items in the FreshBooksCommand.Parameters collection and can specify only the ones you really need. The synchronization is performed as follows:

    The synchronization takes place when:

    When FreshBooksCommand.ParameterCheck is true, every change of FreshBooksCommand.CommandText or FreshBooksCommand.CommandType deletes all ReturnValue parameters. You have to add them manually each time you change one of these properties.