DataConnector
データのクエリ
ADO.NET provider for Google Analytics > データのクエリ

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#
コードのコピー
static void ReadDataReader()
{
    //接続文字列を定義します。
    string connectionString = string.Format("Key File={0};View Id={1}", KeyFile, ViewId);

    //コマンドを定義します。
    string sql = "SELECT Source, Sessions FROM Traffic WHERE Sessions > 500 AND StartDate = '14DaysAgo' AND EndDate = 'Today'";

    //データを取得します。 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"]));
        }
    }
}
               

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 demonstrate read operation from the data source using the C1GoogleAnalyticsDataAdapter and retrieves data by executing the Select command.                

C#
コードのコピー
static void ReadDataAdapter()
{
    //接続文字列を定義します。
    string connectionString = string.Format("Key File={0};View Id={1}", KeyFile, ViewId);

    //コマンドを定義します。
    string sql = "SELECT Source, Sessions FROM Traffic WHERE Sessions > 500 AND StartDate = '14DaysAgo' AND EndDate = 'Today'";

    //データを取得します。
    using (var con = new C1GoogleAnalyticsConnection(connectionString))
    {
        con.Open();

        //Using C1GoogleAnalyticsDataAdapter
        C1GoogleAnalyticsDataAdapter gaAdapter = new C1GoogleAnalyticsDataAdapter(con, sql);
        DataTable dt = new DataTable();
        gaAdapter.Fill(dt);

        //取得したデータを表示します。
        foreach (DataRow row in dt.Rows)
        {
            Console.WriteLine("{0} --> \t\t{1}", row["Source"], row["Sessions"]);
        }
    }
}