Read records with a parent-child relationship
- Last UpdatedApr 08, 2026
- 3 minute read
This scenario guides you through the steps required to read records with a parent-child relationship.
A parent-child relationship exists between tables that are created when the Lock method is used on a child record and that lock also locks a record in the parent table. This relationship implies a hierarchy. For example, consider the relationship between a pipeline and a segment wherein a pipeline comprises a set of segments and a segment is linked to a pipeline. In this example, the pipeline is the "parent" and a segment is the "child."
For this relationship to exist, you must overwrite the Lock and the Unlock methods of the child table so that the parent record locks with the same lock used for the child record and then unlocks when the child record is unlocked. Because of this need, there must be a reference in the child record, usually a foreign key related to the parent table, and this reference is used in the Lock method to lock the right record in the parent table.
In a typical High Performance Database (HPDB) operation, you would lock the record using the Lock method when you plan to access a record (either read or write) but in the case of parent-child relationships, as the parent record has already been locked, you must use the AttachInsteadOfLock method to retrieve the parent record reference.
To identify the code associated with each step, see the code comments in the Example code section.
To read records with a parent-child relationship
- Open the Program.cs file from the created project.
- Open the RealTime databse (RTDB).
- Lock the child table for reading.
- Locate the child record.
- Lock the child record for reading.
Locking the child record also locks the parent record with the same lock type.
- Locate the parent record.
- Read the field that links both tables (slot number).
- Attach to the parent record instead of lock for reading by using the AttachInsteadOfLock method.
- Unlock the child record.
The parent record also unlocks. - Unlock the child and parent tables.
Example code
IRealTimeDatabase database = new RealTimeDatabase();
try
{
// Open the RTDB.
if (!database.IsOpen())
{
database.Open();
}
// Tables with a parent-child relationship.
var parentTableName = "pipeline";
var childTableName = "pipe_segment";
// Lock types.
IRecordLockRequestType recordLockRequest = new RecordLockRequestType();
ITableLockRequestType tableLockRequest = new TableLockRequestType();
using (IRealTimeTable childTable = new RealTimeTable(childTableName))
using (IRealTimeTable parentTable = new RealTimeTable(parentTableName))
{
// Lock the child table for reading.
childTable.Lock(tableLockRequest.SAFE_READ);
var nameField = new RealTimeStringField(childTable, "name");
// Locate the child record.
using (var childRecord = childTable.FindRecord(nameField, "ATBT->Segment01"))
{
// Lock the child record for reading.
childRecord.Lock(recordLockRequest.SAFE_READ);
// Read the parent slot number.
var parentSlot = childRecord.ReadData<Int32>("pipeline");
// Locate the parent record.
using (var parentRecord = parentTable.FindRecord(parentSlot))
{
// Using AttachInsteadOfLock instead of Lock for reading.
parentRecord.AttachInsteadOfLock(recordLockRequest.SAFE_READ);
// User code.
var parentRecordName = parentRecord.ReadString("name");
} // Leaving scope releases the parent record lock.
} // Leaving scope releases the child record lock.
} // Leaving scope releases child and parent table locks.
}
catch (Exception ex)
{
Console.WriteLine(quot;An error occurred while reading parent-child records: {ex.Message}");
}