External Database Query Setup

The example below details how to apply an external database query to a specific property on a component. For this example we will be applying the query to the DEVICE_TYPE_CODE for a CONTROL_VALVE. Once defined, when you place a new instrument, the DEVICE_TYPE_CODE field will provide a drop down list with values to choose from which are stored in an existing external database. The same procedure can be followed for any property and any EC Class.

This example is being applied to OpenPlant PID schema but will work in OpenPlant Modeler as well.

  1. Open the Bentley Class Editor and load the OpenPlant_PID.01.08.ecschema.xml schema.
  2. Next, load the OpenPlant_CustomAttribute.08.11.ecschema.xml schema.


    This will let you add the custom attribute EXTERNAL_DATSOURCE_DEFINITION_MAPPING to the DEVICE_TYPE_CODE property.

  3. Search for the Control Valve class and click the DEVICE_TYPE_CODE from the Properties tab.
  4. Override the Device Type Code property as it is disabled.
  5. Add the following custom attributes to the DEVICE_TYPE_CODE property.
    • External Data source Definition Mapping
    • ExtendType
  6. Remove the Value description attribute custom attribute if present.
  7. In the ExtendType attribute add the following string exactly as shown:


  8. In the External Data source Definition Mapping attribute, fill out the field with information as follows:


    • Data connection string: Provider=Microsoft.Jet.OLEDB.4.0;Data Source=
    • File Name of database (can be blank): Schema.mdb
    • Path location of database (can be blank): c:\
    • Sql Select Statement: Select VALUE,DESCRIPT from ins_pick Where VAR='ITYP' and [SERVICE] = '<SERVICE>'
  • SQL Query

    User can define the whole query including, file to be queried, table name, whereclause, and display order. It should be a standard SQL query with the following exceptions.

    • All the field names in the whereclause should be incapsulated in square brackets "[FIELD NAME]".
    • All the EC Property names which are the placeholder for their actual values should be encapsulated in greater than and lesser than signs. '<EC PROPERTY NAME>'.
    • The EC Property names and FIELD names are also case sensitive use uppercase in both.
    1. Update the "Category" attribute so that Device Type Property is shown in Tag Information tab in the Placement dialog:




      Limitations:
      • This feature is only tested for String type fields and EC properties.
      • This feature is only tested for "LIKE" and "=" operators in the SQL query.

    Query Data from Excel Files

    For an Excel query all the steps are the same except for the query statement. In Excel, you have to define the query in a way that Excel understands it and can access data from the sheets. "Data" as defined in the example below is the sheet name.

    1. In the SQL Select Statement field enter the following sample query:

      Select [Data$].VALUE,[Data$].DESCRIPT from [Data$] Where [Data$].VAR='ITYP'

      OR, for a more detailed query the statement below searches for the value from a specific (SERVICE) column in the database:

      <SERVICE>



  • To setup your data in the excel sheet you have to make put data into a table.
    1. Paste the data columns into a new excel sheet and then select all the data along with the Header and select Format as Table option from the "Home" Task Bar.


    2. Rename the sheet to Data.


      The Excel database can now be queried using the query described above.

    Connecting to SQL Server Databases using External Database Query

    In order to connect to a SQL Server Database using the external database query, the connection string can be formed in the following ways:

    1. Standard security:

      Server=myServerAddress;Database=myDataBase;User Id=myUsername;Password=myPassword;

    2. Trusted connection:

      Server=myServerAddress;Database=myDataBase;Trusted_Connection=True;

    3. Connection to a SQL Server instance:

      Server=myServerName\myInstanceName;Database=myDataBase;User Id=myUsername;Password=myPassword;

    Sample database using the Microsoft SQL Server Management Studio with the credentials as:



    The name of my sample database is toTest as:



    And on the schema end everything remains the same except for the part where we make the connection string and query the database, it becomes:



    Adding an Extended Type that Reads and Displays a Database Table

    To add the ability to read an external datasource (Access, SQL) and display results in a list view, to be used in a picklist.

    The custom attribute would be defined on a property that would be picked in element info.

    1. A Microsoft Access database file named as “Manufactures.mdb” is provided with this training's dataset. Place this file in the following directory location: C:\ProgramData\Bentley\OpenPlant\Configuration\WorkSpaces\OPTrainingWorkspace\WorkSets\OPTrainingImperial\Standards\OpenPlant\
    2. In the Class Editor open the OpenaPlant_PID.01.08 schema and supplement with OpenPlant_PID_Supplemental_Design.01.08

      Navigate to Plant Base Object > Named Item > Device > Equipment > Container > Vessel Class.

      Select the Properties tab, select the MANUFACTURER property and click Override. This will localize the property.

      Add the following custom attributes to this property.


    3. Populate these custom attributes.

      Provider=Microsoft.Jet.OLEDB.4.0; Data Source= Manufacturers.mdb

      OPENPLANT_WORKSET_STANDARDS

      Select MANU_NAME as “Manufacturer Name” from Manufacturer “ORDER BY MANU_NAME”

      Table Field Name “Manufacturer Name”
      ECProperty Name MANUFACTURER
      Extend Type Name ShowPickListviewDataBase
    4. Save all schemas. Restart OpenPlant PID and test your changes by placing a Vessel. You can see that the Manufacturer property for the Vessel class now shows the values from the DB.

    Connecting to Oracle Databases using External Database Query

    In order to connect with an Oracle Database using External Database Query, firstly you must have Oracle Client installed on your machine, where the databases shall reside.

    The connection string in the following case is formed as:

    DATA SOURCE=Svrora12201:1521/orcl;DBA PRIVILEGE=SYSDBA;USER ID=sys;PASSWORD=mgr
    Where "Svrora12201" is the Hostname, "1521" is the Port and "orcl" is the Service name. The data source is followed by the DBA (Database administrator) privilege, followed by the User ID and the Password.

    One can simply get this information from the properties of the database connection.



    And in the class editor it becomes: