/ Home / HowTo / RetrieveWithJoins

How to Configure DataAccess with ConnectionBuilder.
How to Configure DataAccess with DataConfigs.

The SQLManager class methods accept DataObjects and parts of a DbRequest message. It then creates the SQL statements for the developer.

These DbRequest message parts are:
KeyColumns    (LJCNetCommon.DbColumns)    - Used to create a simple "Where" clause.
PropertyNames (List<string>())            - Modifies which columns appear in the SQL statement.
Filters       (LJCDBMessageLib.DbFilters) - Used to create more complex "Where" clauses.
Joins         (LJCDBMessageLib.DbJoins)   - Used to create "Join" clauses.

The SQLManager class is similar to DataManager except SQLManager is a
Client/Server only object whereas DataManager can do remote message based
data access.

SQLManager data methods return a DataTable and DataManager data
methods return a DbResult message object.
See the Technical Documentation for more information.
using LJCDBClientLib;
using LJCDBMessage;
using LJCNetCommon;
using System.Collections.Generic;
using System.Data;

// Selecting data with the SQLManager object.
internal Province RetrieveWithJoins(string connectionString, string providerName)
{
  Province retValue;

  // Create the SQLManager.
  var sqlManager = new SQLManager(null, "Province", connectionString
    , providerName);

  // Identify the records and properties to be selected.
  var keyColumns = new DbColumns()
  {
    { "ID" , 1 }
  };
  var propertyNames = new List()
  {
    { "Name" },
    { "Description" },
    { "Abbreviation" }
  };

  // Create the Join definition.
  DbJoins dbJoins = new DbJoins();
  DbJoin dbJoin = new DbJoin()
  {
    TableName = "Region",
    Columns = new DbColumns()
    {
      // PropertyName is "RegionName" as data object cannot have duplicate
      // properties.
      // RenameAs is "JoinName" as DataTable cannot have duplicate columns.
      { "Name", "RegionName", "JoinName" }
    },
    JoinOns = new DbJoinOns()
    {
      // By default the fromColumnName is qualified with the base table name
      // and the toColumnName is qualified with the join table name.
      // To override this behavior, simple include your own qualifier.
      { "RegionID", "ID" }
    }
  };
  dbJoins.Add(dbJoin);

  // Perform the Select
  var dataTable = sqlManager.GetDataTable(keyColumns, propertyNames
    , dbJoins: dbJoins);
  //Created SQL statement - sqlManager.SQLStatement is:
  // select
  //   Province.Name,
  //   Province.Description,
  //   Province.Abbreviation,
  //   Region.Name as JoinName
  // from Province
  // left join Region
  //   on ((Province.RegionID = Region.ID))
  // where Province.ID = 1;

  // Create the Data Object.
  // Use definition for different values.
  // Must add "RegionName" property to Province to receive the join value.
  DbColumns dataDefinition = new DbColumns()
  {
    // Property and DataTable Column name different than DB column name.
    { "Name", "RegionName", "JoinName" }
  };

  // Sets object values where DataTable column names match
  // the object property names and DataTable column row
  // values are not null.
  // If DataRow is not provided, first row is used if available.
  var converter = new ResultConverter<Province, Provinces>();
  retValue = converter.CreateDataFromTable(dataTable);
    , dataDefinition: dataDefinition);
  return retValue;
}
/ Home / HowTo / RetrieveWithJoins

Copyright © Lester J. Clark and Contributors.
Licensed under the MIT License.