Datasets Module (How-to Guide) walks you through configuring the Datasets module for bi-directional database communication. You'll create database connections, build queries, map tables to tags, and integrate query results with displays and scripts.
Prerequisites:
- Database connection information
- Understanding of SQL syntax
- Tags created for data mapping (optional)
In this page:
Configuration Workflow
- Define Database Connections - Configure DatasetDB objects
- Create Queries - Build SQL statements with parameters
- Map Tables (Optional) - Link database tables to tags
- Configure Files (Optional) - Setup text/XML file I/O
- Test and Display - Verify queries and show results
Step 1: Define Database Connections
Using Default SQLite Databases
Every solution includes four pre-configured SQLite databases:
Database | Purpose | File Location |
---|---|---|
TagHistorian | Time-series data | <SolutionName>.dbTagHistorian |
AlarmHistorian | Alarm events & audit | <SolutionName>.dbAlarmHistorian |
Retentive | Persistent tag values | <SolutionName>.dbRetentive |
RuntimeUsers | Dynamic users | <SolutionName>.dbRuntimeUsers |
Adding External Database
- Navigate to Datasets → DBs
- Click Plus icon
- Configure connection:
- Name: Database identifier
- Provider: Connection type
- Database: Database system
- Description: Documentation
- Click OK
- Configure ConnectionString properties
Connection Examples
Database | Provider | Connection String |
---|---|---|
SQL Server | SqlClient | Server=.\SQLEXPRESS;Database=MyDB; |
PostgreSQL | PostgreSQL | Server=localhost;Database=MyDB;Port=5432; |
MySQL | ODBC | Driver={MySQL};Server=localhost;Database=MyDB; |
Oracle | OracleClient | Data Source=//localhost:1521/XE; |
Testing Connection
- After configuring, click Test button
- Provide credentials if required
- Verify successful connection
Step 2: Create Queries
Building a Query
- Go to Datasets → Queries
- Click Plus icon
- Enter Name (no spaces, use underscores)
- Select DB from dropdown
- Click OK
- Double-click SqlStatement to open editor
SQL Statement with Parameters
Use curly brackets to embed tag values:
Static Query:
sql
SELECT * FROM Products WHERE Price > 100
Dynamic Query with Tags:
sql
SELECT * FROM Products
WHERE Price > {{Tag.MinPrice}}
AND Category = '{{Tag.Category}}'
Query Editor Features
- Syntax highlighting
- IntelliSense support
- Parameter validation
- Test execution
Visual Query Builder
For complex queries without SQL knowledge:
- Go to Datasets → DBs
- Click Visual Query Builder
- Drag tables and connect fields visually
Step 3: Map Database Tables
Creating Table Mapping
- Navigate to Datasets → Tables
- Click Plus icon
- Configure:
- Name: Table identifier
- DB: Database connection
- TableName: Actual database table
- WhereCondition: Filter criteria
- Access: Read/Insert/ReadWrite
- Click OK
Mapping Tags to Columns
- Select table in grid
- Click Mapping button (...)
- Map database columns to tags:
- Left: Database columns
- Right: Solution tags
- Configure DateTimeMode (UTC/Local)
Step 4: Configure Files (Optional)
Setting Up File I/O
- Go to Datasets → Files
- Click Plus icon
- Configure:
- Name: File identifier
- FileName: Path with optional tag values
- FileType: ASCII/Unicode/XML
- Objects: Tags to read/write
- Click OK
Dynamic File Names
Use tags in file paths:
C:\Recipes\Recipe_{{Tag.RecipeID}}.xml
Step 5: Test and Display Data
Testing Queries
- Go to DataExplorer → SQL Queries
- Select database and query
- Click Execute
- Review results in grid
Displaying in Runtime
Using DataGrid Control:
- In Displays → Draw
- Add DataGrid control
- Set DataSource property:
Dataset.Query.QueryName
Using Scripts:
csharp
// Execute query
DataTable result = @Dataset.Query.MyQuery.SelectCommand();
// Process results
foreach(DataRow row in result.Rows)
{
@Tag.Value = row["ColumnName"];
}
Runtime Execution
Synchronous vs Asynchronous
Synchronous - Blocks until complete:
csharp
DataTable dt = @Dataset.Query.MyQuery.SelectCommand();
Asynchronous - Non-blocking:
csharp
@Dataset.Query.MyQuery.ExecuteAsync();
// Check LastStatus property for completion
Store and Forward
For critical data operations:
- Enable in database connection
- System buffers data during connection loss
- Automatic forward when connection restored
Common Issues
Connection Failed
- Verify server address and port
- Check firewall settings
- Confirm credentials
- Test network connectivity
Query Syntax Error
- Validate SQL syntax for your database type
- Check table and column names
- Verify data types in WHERE conditions
- Test in SQL tool first
No Data Returned
- Check WHERE conditions
- Verify table has data
- Confirm user permissions
- Review date/time filters
Performance Issues
- Add database indexes
- Limit result sets (TOP/LIMIT)
- Use stored procedures
- Enable connection pooling
Best Practices Checklist
- Use parameterized queries - Prevent SQL injection with tag parameters
- Test in DataExplorer - Verify queries before runtime use
- Handle errors - Check LastStatus property
- Limit result sets - Use WHERE and LIMIT clauses
- Document connections - Use description fields
- Secure credentials - Only admins set passwords
- Plan database schema - Design before implementation
Security Considerations
SQL Injection Prevention
- Use parameterized queries with
{{Tag}}
syntax - Never concatenate user input directly
- Validate tag values before execution
Network Gateway
For restricted databases:
- Set ServerIP in DB configuration
- Install TWebServices on gateway machine
- Route through secure connection
Next Steps
- [DataGrid Control →] Display query results
- [Scripts Module →] Process database data
- [Reports Module →] Generate database reports
In this section...