DataConnector
はじめに
ADO.NET provider for OData > はじめに

The ADO.NET provider for OData provides a wide range of features that enable connectivity to OData from .Net applications. The documentation will help you understand the C1.AdoNet.OData namespace, which includes all the available classes that can be used to connect and retrieve data from an OData service.

DataConnectors are mostly used in combination with other ComponentOne components, such as DataEngine and FlexPivot. For a better understanding of this application, please see OData Sample live demo. The procedure below describes how to use the DataConnector in a console application within Visual Studio.

How to create a new Console Application

The ADO.NET provider for OData can be used in any application. In this guide, a console application is created:

  1. Open Visual Studio.        
  2. In the Get started pane, select Create a new project.        
  3. In the Create a new project window, select Console Application and click Next, as in the screenshot below:  

    Create new project window

  4. In the Configure your new project window,write the project's name choose location to save the project then click Create.

How to add the NuGet packages

To use the ADO.NET provider for OData in an application, the respective NuGet package should be added:

  1. From the Project menu, select Manage NuGet Packages.        
  2. In the NuGet Package Manager, click the Package source drop down and select nuget.org
  3. In the left pane of the Browse tab, select C1.AdoNet.OData
  4. In the right pane of the Browse tab, click Install to add the reference to the package.

How to use ADO.Net provider for OData to retrieve data

Quick example

C1ODataConnection implements the ADO.NET DbConnection similar to standard ADO.NET connection object. Thus retrieving data can be as easy as the following few lines, which print some data from the Northwind data service, by using C1ODataDataAdapter to retrieve a single result set of all the data that matches a query (for other connection options, click here).

C#
コードのコピー
var adapter = new C1.AdoNet.OData.C1ODataDataAdapter(
    connectionString: "Url = https://services.odata.org/V4/Northwind/Northwind.svc",
    commandText: "SELECT * FROM Products WHERE UnitPrice > 50");

var table = new System.Data.DataTable();
adapter.Fill(table);

Console.WriteLine("Fetched {0} rows.", table.Rows.Count);

The above code is only for quick script-like usage. Real applications require more structured approaches. However, the high-level steps are similar:

  1. Create a connector.
  2. Use the connector to fill an in-memory table.
  3. Render the table's contents.

 

Detailed structured example

This example expands the above steps into reusable methods within a class.

  1. Prepare the general structure of the code:
    C#
    コードのコピー
    using C1.AdoNet.OData;
    using System.Data;
    using System.Text;
    
    public static class ODataGettingStarted
    {
        public static void Run()
        {
            // データ ソースと取得するデータを指定します。
    
            // 指定されたソース用に新しいアダプターを準備し、それを再利用して指定されたデータを取得します。
    
            // 取得したデータを出力します。
        }
    }
    
    ODataGettingStarted.Run();
    
  2. Add inside the class the following method, which prepares the needed C1 AdoNet OData objects:
    C#
    コードのコピー
    // 渡された接続文字列で提供されるデータをフェッチする準備ができている新しいアダプターを返します。
    static C1ODataDataAdapter MakeAdapterForConString(string conString)
    {
        var connection = new C1ODataConnection(conString);
        var command = new C1ODataCommand(connection);
        return new C1ODataDataAdapter(command);
    }
    
  3. Add inside the class the following extension method, which fills a table with the data:
    C#
    コードのコピー
    // 渡された SELECT ステートメントのデータを取得し、それを新しいデータテーブルに返します。
    static DataTable FetchTableForSql(this C1ODataDataAdapter adapter, string sql)
    {
        adapter.SelectCommand.CommandText = sql;
        var table = new DataTable();
        adapter.Fill(table);
        return table;
    }
    
  4. Add inside the class the following extension method, which appends the table to a string builder:
    C#
    コードのコピー
    // 渡されたデータテーブル行の出力をビルダーに追加します。
    static StringBuilder AppendRowsOfTable(this StringBuilder builder, DataTable table)
    {
        foreach (DataRow row in table.Rows)
        {
            builder.AppendJoin(" - ", row.ItemArray).AppendLine();
        }
        return builder.AppendLine(); 
    }
    
  5. Call the above methods in order, by filling the entry method Run with the following code:
    C#
    コードのコピー
    // データ ソースと取得するデータを指定します。
    string northwindConString = "Url = https://services.odata.org/V4/Northwind/Northwind.svc";
    string quantitiesSql = "SELECT ProductID, ProductName, QuantityPerUnit FROM Products LIMIT 5";
    string pricesSql = "SELECT ProductID, ProductName, UnitPrice FROM Products WHERE ProductID < 6";
    
    // 指定されたソース用に新しいアダプターを準備し、それを再利用して指定されたデータを取得します。
    using C1ODataDataAdapter adapter = MakeAdapterForConString(northwindConString);
    using DataTable quantitiesTable = adapter.FetchTableForSql(quantitiesSql);
    using DataTable pricesTable = adapter.FetchTableForSql(pricesSql);
    
    // 取得したデータを出力します。
    var builder = new StringBuilder();
    builder.AppendLine()
           .AppendLine("Printing for quantities:")
           .AppendRowsOfTable(quantitiesTable)
           .AppendLine("Printing for prices:")
           .AppendRowsOfTable(pricesTable)
           .AppendLine("End of printing.");
    Console.Write(builder);
    

Running the console application should produce an output like this one:

Output