/ Assembly List / LJCDBMessage / DbRequest

Namespace - LJCDBMessage


Syntax

C#
public class DbRequest

Represents a database request. (E)

Static
Deserialize Deserializes the DbRequest message.

Constructor
#ctor Initializes an object instance.
#ctor The Copy constructor.
#ctor Initializes an object instance with the supplied values.

Data
Clone Clones the structure of the object.
Serialize Serializes the object and returns the serialized string.
Serialize Serialize the object to the specified file.
7 Methods

Properties
AddMissingColumns Indicates if the missing column should be added.
ClientSql Gets or sets the ClientSql value.
Columns The included table columns.
DataConfigName The data configuration name.
DbAssignedColumns The Database Assigned columns.
Filters The where clause filters.
Joins The table joins.
KeyColumns The key column values.
OrderByNames The OrderBy column names.
PageSize The number of records in the page.
PageStartIndex The page starting index.
Parameters Gets or sets the Parameters list reference.
ProcedureName Gets or sets the ProcedureName value.
RequestTypeName The request type. (R)
SchemaName The schema name.
TableName The table name.

Example

The following example shows each step needed to retrieve data. Applications should generally encapsulate much of this code in a table specific Manager class to make application programming easier and cleaner.

A Manager class is the client code that creates a table specific DbQuery object. It then serializes the DbRequest object into a request XML message to pass to the DbService.Execute() method.
See examples in LJCDBServiceLib.DbService Methods Add, Retrieve, Load, Update and Delete.

The data service DbService can be hosted in a windows service or web service which could run on a separate server machine.

C#
using System;
using System.Collections.Generic;
using LJCNetCommon;
using LJCDBMessage;
using LJCDBServiceLib;
        
// A Person Data Record class.
public class Person
{
  /// <summary>Gets or sets the PersonId value.</summary>
  public Int32 PersonId { get; set; }
        
  /// <summary>Gets or sets the Name value.</summary>
  public string Name { get; set; }
}
        
// A console program.
internal class Program
{
  private static void Main(string[] args)
  {
    // Create a Data Definition which is a collection of column definitions.
    // Column definitions are required to communicate with the database.
    // The collection initializer uses an available collection "Add" method.
    // public DbColumn Add(string name, string propertyName = null, string renameAs = null
    //   , string dataTypeName = "String", string caption = null)
    string personIDColumnName = "Person_ID";
    string personIDDataPropertyName = "PersonId";
    DbColumns personDefinition = new DbColumns()
    {
      { personIDColumnName, personIDDataPropertyName, "Int32"},
      { "Name" } // Defaults to "String"
    };
        
    // Create a list of the columns to be retrieved.
    List<string> propertyNames = new List<string>()
    {
      "PersonId",
      "Name"
    };
        
    // Create a key record to specify the record to be retrieved.
    var keyColumns = new DbColumns()
    {
      { "PersonId", 1}
    };
        
    // Create the DbRequest object.
    string tableName = "Person";
    DbRequest dbRequest = new DbRequest("Select", tableName)
    {
      DataConfigName = "PersonData",
      Columns = DbCommon.GetRequestColumns(personDefinition, propertyNames),
      KeyColumns = DbCommon.GetRequestKeyColumns(keyColumns, personDefinition)
    };
        
    // Create the Request XML message.
    string request = dbRequest.Serialize();
        
    // Create the Data Service object.
    DbService dbService = new DbService();
        
    // Execute the Request and get the Result XML message.
    string result = dbService.Execute(request);
        
    // Create a Data Record object to receive the values.
    Person person = new Person();
        
    // Populate the Data Record object with the result values.
    // Uses resultData as an object and processes with reflection.
    DbCommon.SetObjectValues(result, person);
        
    int id = person.PersonId;
    string name = person.Name;
  }
}

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