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 demonstrates a read operation from the data source using the DbDataReader and retrieves data by executing the Select command.
C# |
Copy Code |
static void ReadDataReader()
{
Console.WriteLine("Read operation started !!!");
using (C1MagentoConnection con = new C1MagentoConnection(MagentoConnectionString))
{
con.Open();
//Create Read command
var cmd = con.CreateCommand();
cmd.CommandText = "Select * FROM Products ";
//Execute Read command and display fetched data
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 demonstrates a read operation from the data source using the C1MagentoDataAdapter and retrieves data by executing the Select command.
C# |
Copy Code |
static void ReadDataAdapter()
{
using (C1MagentoConnection con = new C1MagentoConnection(MagentoConnectionString))
{
//Populate DataTable
C1MagentoDataAdapter adapter = new C1MagentoDataAdapter(con, "Select * from Products");
DataTable dataTable = new DataTable();
adapter.Fill(dataTable);
//Display fetched data
foreach (DataRow row in dataTable.Rows)
{
Console.WriteLine(String.Format("\\t{0} --> \\t\\t{1} --> \\t\\t{2}", rdr["Id"], rdr["Name"], rdr["Price"]));
}
}
} |