ADO.NET provider for QuickBooks Online / Querying Data
Querying Data

The ADO.NET Provider for QuickBooks Online implements two classes you can use to perform CRUD (Create, Read, Update, and Delete) operations: DbDataReader and C1QuickBooksOnlineDataAdapter classes. The tabs below describe the interfaces and code implementations.

Querying With DbDataReader

The DbDataReader class can be used to fetch data in subset increments as required. It can retrieve data quicker than the C1QuickBooksOnlineDataAdapter as it retrieves data in pages. When you read data from the DbDataReader, it requests the succeeding page from the data source to load the result, which makes the data retrieval faster.

The following code example demonstrates create, read, update, and delete operations from the data source using the DbDataReader.

 

Create Data

The following example adds new records by executing the Insert command.

C#
Copy Code
static void InsertDataReader()
{
    using (C1QuickBooksOnlineConnection conn = new C1QuickBooksOnlineConnection(connectionString))
    {
        conn.Open();
        //Create Insert command
        C1QuickBooksOnlineCommand command = new C1QuickBooksOnlineCommand(conn, $@"Insert into Attachables(Note, Category) values('{uniqueNote}', 'Document')");

        Console.WriteLine("Insert operation started !!! \n \n");
        //Execute Insert command
        int i = command.ExecuteNonQuery();
        if (i != -1)
        {
            Console.WriteLine("Insert operation successful !!! \n \n");
        }
    }
}

Read Data

The following example retrieves data by executing the Select command.

C#
Copy Code
static void ReadDataReader()
{
    //Setup Connection
    using (C1QuickBooksOnlineConnection conn = new C1QuickBooksOnlineConnection(connectionString))
    {
        conn.Open();

        //Create Read command
        var cmd = conn.CreateCommand();
        cmd.CommandText = "Select * FROM Attachables";

        Console.WriteLine("Read operation started !!! \n \n");
        //Execute Read command and display fetched data
        DbDataReader rdr = cmd.ExecuteReader();
        while (rdr.Read())
        {
            Console.WriteLine(String.Format("{0}\t{1}\t{2}", rdr["Id"], rdr["Category"], rdr["Note"]));
        }
        Console.WriteLine("Read operation successful !!! \n \n");
    }
}

Update Data

The following example modifies data by executing the Update command.

C#
Copy Code
static void UpdateDataReader()
{
    using (C1QuickBooksOnlineConnection conn = new C1QuickBooksOnlineConnection(connectionString))
    {
        conn.Open();
        //Create Update command              
        C1QuickBooksOnlineCommand command = new C1QuickBooksOnlineCommand(conn, "UPDATE Attachables SET Category = @Category WHERE Note = @Note");
        command.Parameters.AddWithValue("@Category", "Other");
        command.Parameters.AddWithValue("@Note", uniqueNote);

        Console.WriteLine("Update operation started !!! \n \n");
        //Execute Update command
        int i = command.ExecuteNonQuery();
        if (i != -1)
        {
            Console.WriteLine("Update operation successful !!! \n \n");
        }
    }
}

Delete Data

The following example removes data by executing the Delete command.

C#
Copy Code
static void DeleteDataReader()
{
    using (C1QuickBooksOnlineConnection conn = new C1QuickBooksOnlineConnection(connectionString))
    {
        conn.Open();
        //Create Delete command
        C1QuickBooksOnlineCommand command = new C1QuickBooksOnlineCommand(conn, "Delete From Attachables where Note = @Note");
        command.Parameters.AddWithValue("@Note", uniqueNote);

        Console.WriteLine("Delete operation started !!! \n \n");
        //Execute Delete command
        int i = command.ExecuteNonQuery();
        if (i != -1)
        {
            Console.WriteLine("Delete operation successful !!! \n \n");
        }
    }
}