API Data Provider - Online Help

Using the Profile in ADO.NET

This section provides a walk-through of writing data access code to this profile in ADO.NET. See Tables for more information on the available data source entities and how to query them with SQL. See SQL Compliance for the SQL syntax.

Creating Connection Objects

See Establishing a Connection for guides to defining the connection string and authenticating. Below is a typical invocation to create APIConnection objects.

C#

using (APIConnection connection =  
  new APIConnection("Profile=MyProfile.apip;ProfileSettings=[Profile Configuration Settings]")) 
{ 
  connection.Open(); 
}

Using the APIDataReader

The APIDataReader retrieves data faster than the APIDataAdapter because it can retrieve data in pages. As you read data from the APIDataReader, it periodically requests the next page of results from the data source, if required. This causes results to be returned at a faster rate. The following example selects all the columns from the Account table:

C#

string connectionString = "Profile=MyProfile.apip;ProfileSettings=[Profile Configuration Settings]";
using (APIConnection connection = new APIConnection(connectionString)) {
  APICommand cmd = new APICommand("SELECT * FROM Account", connection);
  
  APIDataReader rdr = cmd.ExecuteReader();
  
  while (rdr.Read()) {
    Console.WriteLine(String.Format("\t{0} --> \t\t{1}", rdr["Id"], rdr["Name"]));
  }
}

Using the APIDataAdapter

Use the adapter's Fill method to retrieve data from the data source. An empty DataTable instance is passed as an argument to the Fill method. When the method returns, the DataTable instance is populated with the queried data. Note that the APIDataAdapter is slower than the APIDataReader because the Fill method needs to retrieve all data from the data source before returning.

The following example selects the Id and Name columns of the Account table:

C#

string connectionString = "Profile=MyProfile.apip;ProfileSettings=[Profile Configuration Settings]";
using (APIConnection connection = new APIConnection(connectionString)) {
  APIDataAdapter dataAdapter = new APIDataAdapter(
  "SELECT Id, Name FROM Account", connection);
  
  DataTable table = new DataTable();
  dataAdapter.Fill(table);
  
  Console.WriteLine("Contents of Account.");
  
  foreach (DataRow row in table.Rows) {
    Console.WriteLine("{0}: {1}", row["Id"], row["Name"]);
  }
}

Copyright (c) 2024 CData Software, Inc. - All rights reserved.
Build 23.0.8839.0