Dataset SQL Query (Tutorial) teaches you to:
- Configure database connections
- Create queries with parameters
- Display data in tables
- Schedule automated queries
Prerequisites:
In this page:
Table of Contents maxLevel 2 minLevel 2 indent 10px exclude Steps style none
Configure Database DB Connection
- Navigate to Datasets → DBs
- Add database:
SQLite:
- Name:
ProductionDB
SQL Server:
- Name:
ProductionDB
- Provider:
SqlClient
- Connection String: Use builder or:
Data Source=SERVER\INSTANCE;Initial Catalog=Production;Integrated Security=True
MySQL:
- Name:
MySQLData
- Provider:
MySQL
- Connection String:
Server=localhost;Database=scada;Uid=user;Pwd=password;
- Test connection → Should show "Connected"
Create Simple Query
- Go to Datasets → Queries
- Add query:
Get Current Orders:
- Name:
ActiveOrders
- Database:
ProductionDB
- SQL:
sql
SELECT
OrderID,
ProductName,
Quantity,
DueDate,
Status
FROM Orders
WHERE Status = 'Active'
ORDER BY DueDate
- Test Query → View results
Parameterized Query
Create query with parameters:
Production by Date:
- Name:
ProductionReport
- SQL:
sql
SELECT
ProductID,
SUM(Quantity) as Total,
AVG(Quality) as AvgQuality
FROM Production
WHERE ProductionDate BETWEEN @StartDate AND @EndDate
AND Line = @LineNumber
GROUP BY ProductID
Parameters:
- @StartDate: DateTime
- @EndDate: DateTime
- @LineNumber: Integer
Link Query to Tags
Bind parameters to tags:
- Select parameter
- Link to tag:
- @StartDate →
Report_StartDate
- @EndDate →
Report_EndDate
- @LineNumber →
Selected_Line
- @StartDate →
Display Query Results
- Open Displays → Mainpage
- Add DataGrid control
- Configure:
- Data Source:
@Dataset.Query.ActiveOrders
- Auto-refresh: 30 seconds
- Allow sorting: Yes
- Allow filtering: Yes
- Data Source:
Create Stored Procedure Call
- Datasets → Queries
- Add stored procedure:
Call SP:
- Name:
UpdateInventory
- Type: Stored Procedure
- SQL:
sql
EXEC sp_UpdateInventory
@ProductID = @ProductID,
@Quantity = @Quantity,
@Location = @Location
Write to Database
Create insert query:
sql
INSERT INTO EventLog
(Timestamp, EventType, Description, UserName, TagValue)
VALUES
(GETDATE(), @EventType, @Description, @User, @Value)
Execute from script:
csharp
@Dataset.Query.LogEvent.Execute(
"@EventType", "Alarm",
"@Description", "High Temperature",
"@User", @Client.UserName,
"@Value", @Tag.Temperature
);
Create Data Table
- Navigate to Datasets → Tables
- Create table mapping:
Recipe Table:
- Name:
Recipes
- Database:
ProductionDB
- Table:
RecipeData
- Key Column:
RecipeID
- Auto-load: On startup
Access in scripts:
csharp
DataRow recipe = @Dataset.Table.Recipes.SelectRow("RecipeID=101");
@Tag.Recipe_Name = recipe["Name"];
@Tag.Recipe_Temp = recipe["Temperature"];
Schedule Query Execution
Create scheduled task:
csharp
public void UpdateProductionMetrics()
{
// Run query every hour
@Dataset.Query.ProductionReport.Execute();
// Store results in tags
DataTable dt = @Dataset.Query.ProductionReport.ResultData;
@Tag.TotalProduction = dt.Compute("SUM(Total)", "");
}
Next Steps
- [Create Dashboards] - Display SQL data
- [Reports Module] - Database reports
- [Enrich Data & Scripts] - Process query results
In this section...
Page Tree | ||||
---|---|---|---|---|
|