You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 15 Next »

Configure database connections and execute queries

Datasets → Tutorial | Concept | How-to Guide | Reference


This Tutorial Teaches you to:

  • Configure database connections
  • Create queries with parameters
  • Display data in tables

Prerequisites:


1. Configure Database Connection

  1. Navigate to Datasets → DBs
  2. Click Plus icon to add database

Option A: Use Default SQLite

Name: ProductionDB
Provider: SQLite Data Provider
Database: .Net Framework Data Provider for SQLite
ConnectionString: [Auto-configured]

Option B: SQL Server

Name: ProductionDB
Provider: SqlClient Data Provider
Database: SQL Server Database
ConnectionString: 
  Data Source=SERVER\INSTANCE;
  Initial Catalog=Production (your database name);

  1. Click Test → Should show "Connected"


Info

These tables will not be used further in this Tutorial, once there is no data available. If you have a database table with data, feel free to use, if not you can use the Table created on the Historian Module Tutorial



2. Create Simple Query

  1. Go to Datasets → Queries
  2. Click Plus icon
  3. Configure:
    • Name: ActiveOrders
    • DB: TagHistorian
  4. Click OK
  5. Double-click SqlStatement to edit:
    1. SELECT TankFarm/Tank1/Temperature FROM ProcessData



3. Add Dynamic Parameters

  1. Go to Datasets → Queries
  2. Double-click SqlStatement to edit
  3. Edit SQL with tag placeholders:
    1. SELECT TankFarm/Tank1/Temperature FROM ProcessData WHERE UTCTimestamp_Ticks >= (strftime('%s', {Tag.TankFarm/Tank1/Temperature.Timestamp}, '-10 days') * 10000000) + 621355968000000000
  4. It is important to remember that historian data is in UTCTicks, thats why the time calculation must to be done



4. Display Query Results

  1. Open Displays → Draw
  2. Add DataGrid control
  3. Set properties:
    • DataSource: @Dataset.Query.ActiveOrders
  4. Run solution 
  5. Verify data appears in grid



5. Execute from Script

Create button to run query:

  1. Add button to display
  2. Double-click and go to Dynamics:
  3. Check Action and configure:
    1. Event: MouseLeftButtonDown
    2. Action: RuScript
  4. Click New
  5. The CodeBehind display will automatically open
  6. Add the following script:

 csharp

public void MouseLeftButtonDown1(object sender, System.Windows.Input.InputEventArgs e)

{

DataTable results = @Dataset.Query.ActiveOrders.SelectCommand();

}


Debug

You can use the Debugging Scripts Code to debug this piece of code and view the values in the "results" variable



6. Test Your Configuration

  1. Start runtime
  2. Open Datasets → Datasets Monitor
  3. Verify:
    • LastStatus
    • RowCcount
    • No errors in LastStatusMessage
  4. Check DataGrid displays:
    • Data refreshes automatically



In this section...

The root page @parent could not be found in space 93Draft.




  • No labels