ADO.NET provider for Google Analytics / Querying Data
Querying Data

The ADO.NET Provider for Google Analytics implements two classes you can use to perform the Read operation: DbDataReader and C1GoogleAnalyticsDataAdapter classes. The tabs below describe the classes and code implementations.

Read operation With DbDataReader

The DbDataReader class can be used to fetch data in subset increments as required. It can retrieve data quicker than the C1GoogleAnalyticsDataAdapter 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 examples demonstrate read operation from the data source using the DbDataReader and retrieves data by executing the Select command.                

C#
Copy Code
static void ReadDataReader()
{
    //Define connection string
    string connectionString = string.Format("Key File={0};View Id={1}", KeyFile, ViewId);

    //Define command
    string sql = "SELECT Source, Sessions FROM Traffic WHERE Sessions > 500 AND StartDate = '14DaysAgo' AND EndDate = 'Today'";

    //Fetch data using DataReader
    using (var con = new C1GoogleAnalyticsConnection(connectionString))
    {
        con.Open();
        var command = con.CreateCommand();
        command.CommandText = sql;                
        var reader = command.ExecuteReader();
        while (reader.Read())
        {
            Console.WriteLine(String.Format("{0} --> \t\t{1}", reader["Source"], reader["Sessions"]));
        }
    }
}