ReadStruct

This method reads a specified number of records from a database table with any table structure.

Syntax

METHOD ReadStruct : BOOL
VAR_INPUT
    hDBID: UDINT;
    sTableName: T_MaxString;
    pColumnNames: POINTER TO ARRAY [0..MAX_DBCOLUMNS] OF STRING(50);
    cbColumnNames: UDINT;
    sOrderByColumn: STRING(50);
    eOrderType: E_OrderType := E_OrderType.eOrder_ASC
    nStartIndex: UDINT;
    nRecordCount: UDINT;
    pData: POINTER TO BYTE;
    cbData: UDINT;
END_VAR

ReadStruct 1: Inputs

Name

Type

Description

hDBID

UDINT

Indicates the ID of the database to be used.

sTableName

T_MaxString

Name of the table that is to be read.

pColumnNames

POINTER TO ARRAY [0..MAX_DBCOLUMNS] OF STRING(50)

Address of the array containing the column name to be read.

cbColumnNames

UDINT

Length of the column name array

sOrderByColumn

STRING(50)

Name the sorting column

eOrderType

E_OrderType

Sorting direction (ASC or DESC)

nStartIndex

UDINT

Indicates the index of the first record to be read.

nRecordCount

UDINT

Indicates the number of records to be read.

pData

POINTER TO BYTE

Address of the structure array into which the records are to be written.

cbData

UDINT

Indicates the size of the structure array in bytes.

ReadStruct 2: Return value

Name

Type

Description

ReadStruct

BOOL

Displays the status of the method. Returns TRUE as soon as the method execution is finished, even in the event of an error.

Sample

VAR
    fbPLCDBRead    : FB_PLCDBReadEvt (sNetID := '', tTimeout := T#5S);
    myCustomStruct : ST_Record;
    tcMessage      : I_TcMessage;
END_VAR
TYPE ST_Record :
STRUCT
    nID        : LINT;
    dtTimestamp: DATE_AND_TIME;
    sName      : STRING;
    nSensor1   : LREAL;
    nSensor2   : LREAL;
END_STRUCT
END_TYPE
// set columnnames
ColumnNames[0] := 'ID';
ColumnNames[1] := 'Timestamp';
ColumnNames[2] := 'Name';
ColumnNames[3] := 'Sensor1';
ColumnNames[4] := 'Sensor2';

IF fbPLCDBRead.ReadStruct(
    hDBID:= 1, 
    sTableName:= 'MyTable_Struct', 
    pColumnNames:= ADR(ColumnNames), 
    cbColumnNames:= SIZEOF(ColumnNames), 
    sOrderByColumn:= ColumnNames[0], 
    eOrderType:= E_OrderType.DESC, 
    nStartIndex:= 0, 
    nRecordCount:= 1, 
    pData:= ADR(myCustomStruct), 
    cbData:= SIZEOF(myCustomStruct))
THEN
    IF fbPLCDBRead.bError THEN
        tcMessage:= fbPLCDBRead.ipTcResult;
        nState := 255; 
    ELSE
        nState := 0;
    END_IF
END_IF

Result in the PLC:

ReadStruct 3: