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

The ADO.NET Provider for Magento implements two classes you can use to perform the Read operation: DbDataReader and C1MagentoDataAdapter classes. The tabs below describe the classes 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 C1MagentoDataAdapter 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()
{
    Console.WriteLine("Read operation started !!!");
    using (C1MagentoConnection con = new C1MagentoConnection(MagentoConnectionString))
    {
      con.Open();      
      //Read コマンドを作成します。
      var cmd = con.CreateCommand();
      cmd.CommandText = "Select * FROM Products ";
  
      //Readコマンドを実行し、取得したデータを表示します。
      var rdr = cmd.ExecuteReader();
      while (rdr.Read())
      {
          Console.WriteLine(String.Format("\\t{0} --> \\t\\t{1} --> \\t\\t{2}", rdr["Id"], rdr["Name"], rdr["Price"]));
      }
      Console.WriteLine("Read operation successful !!! \\n \\n");
    }
}

Querying With C1MagentoDataAdapter

The C1MagentoDataAdapter class can be used to retrieve a single result set containing all the data that matches a given query. The C1MagentoDataAdapter 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 C1MagentoDataAdapter is slower compared to the DbDataReader.

The following code demonstrate read operation from the data source using the C1MagentoDataAdapter and retrieves data by executing the Select command.

C#
コードのコピー
static void ReadDataAdapter()
{
    using (C1MagentoConnection con = new C1MagentoConnection(MagentoConnectionString))
    {
        //DataTable を設定します。
        C1MagentoDataAdapter adapter = new C1MagentoDataAdapter(con, "Select * from Products");
        DataTable dataTable = new DataTable();
        adapter.Fill(dataTable);  
        //取得したデータを表示します。
        foreach (DataRow row in dataTable.Rows)
        {
           Console.WriteLine(String.Format("\\t{0} --> \\t\\t{1} --> \\t\\t{2}", rdr["Id"], rdr["Name"], rdr["Price"]));
        }
     }
}