[]
        
(Showing Draft Content)

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.

Data Reader

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.

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"]));
        }
    }
}

Data Adapter

Read Operation With C1GoogleAnalyticsDataAdapter

The C1GoogleAnalyticsDataAdapter class can be used to retrieve a single result set containing all the data that matches a given query. The C1GoogleAnalyticsDataAdapter uses its Fill method to fetch data from the data source. An empty DataTable instance is passed as an argument to the Fill method. Once the method returns, the DataTable instance is populated with the queried data. Since the Fill method must retrieve all the data from the data source before returning, the C1GoogleAnalyticsDataAdapter is slower compared to the DbDataReader.

The following code demonstrates a read operation from the data source using the C1GoogleAnalyticsDataAdapter and retrieves data by executing the Select command.

static void ReadDataAdapter()
{
    //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 (var con = new C1GoogleAnalyticsConnection(connectionString))
    {
        con.Open();
        //Using C1GoogleAnalyticsDataAdapter
        C1GoogleAnalyticsDataAdapter gaAdapter = new C1GoogleAnalyticsDataAdapter(con, sql);
        DataTable dt = new DataTable();
        gaAdapter.Fill(dt);
        //Display fetched data
        foreach (DataRow row in dt.Rows)
        {
            Console.WriteLine("{0} --> \t\t{1}", row["Source"], row["Sessions"]);
        }
    }
}