Configure database connections and execute queries.
Tutorials → Datasets | Tutorial | How-to Guide | Reference
Configure database connections
Create queries with parameters
Display data in tables
Table of Contents maxLevel 2 minLevel 2 indent 10px exclude StepsTeaches style none
Datasets → Tutorial | Concept | How-to Guide | Reference
Step 1:Name: ProductionDB
Provider: SQLite System.Data.SQLite Provider
Database: .Net Framework Data Provider for SQLite
ConnectionString: [Auto-configured]
Name: ProductionDB
Provider: SqlClient System.Data.SqlClient Provider
Database: SQL Server SQLServerDatabase
ConnectionString:
Data Source=SERVER\INSTANCE;
Initial Catalog=Production;
(your Integrated Security=Truedatabase name);
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 |
ActiveOrders
ProductionDB
sql
SELECT
OrderID,
ProductName,
Quantity,
DueDate,
Status
FROM Orders
WHERE Status = 'Active'
ORDER BY DueDate
Create query with tag parameters:
SELECT "TankFarm/Tank1/Temperature" FROM ProcessData
ProductionReport
sql
SELECT
ProductID,
SUM(Quantity) as Total,
AVG(Quality) as AvgQuality
FROM Production
WHERE ProductionDate BETWEEN {{Tag.Report_StartDate}}
AND {{Tag.Report_EndDate}}
AND Line = {{Tag.Selected_Line}}
GROUP BY ProductID
The {{Tag.Name}}
syntax embeds tag values at runtime.
SELECT "TankFarm/Tank1/Temperature" FROM ProcessData WHERE UTCTimestamp_Ticks >= (strftime('%s', {Tag.TankFarm/Tank1/Temperature.Timestamp}, '-10 days') * 10000000) + 621355968000000000
@Dataset.Query.ActiveOrders
AutoRefresh: 30 secondsCreate button to run query:
csharp
public void RefreshData_Click(object sender, EventArgs e)
{
// Set parameters
@Tag.Report_StartDate = DateTime.Today;
@Tag.Report_EndDate = DateTime.Now;
@Tag.Selected_Line = 1;
// Execute query
@Dataset.Query.ProductionReport.Select = true;
// Get results
DataTable results = @Dataset.Query.ProductionReport.SelectCommand();
// Process data
if (results.Rows.Count > 0)
{
@Tag.TotalProduction = results.Compute("SUM(Total)", "");
}
}
Create insert query:
LogEvent
sql
INSERT INTO EventLog
(Timestamp, EventType, Description, UserName, TagValue)
VALUES
(GETDATE(), {{Tag.EventType}}, {{Tag.Description}},
{{Tag.UserName}}, {{Tag.Value}})
csharp
// Set values
@Tag.EventType = "Alarm";
@Tag.Description = "High Temperature";
@Tag.UserName = @Client.UserName;
@Tag.Value = @Tag.TankFarm/Tank1/Temp;
// Execute insert
@Dataset.Query.LogEvent.ExecuteCommand();
UpdateInventory
sql
EXEC sp_UpdateInventory
@ProductID = {{Tag.ProductID}},
@Quantity = {{Tag.Quantity}},
@Location = '{{Tag.Location}}'
csharp
@Tag.ProductID = 101;
@Tag.Quantity = 50;
@Tag.Location = "Warehouse-A";
@Dataset.Query.UpdateInventory.ExecuteCommand();
Create scheduled task:
UpdateMetrics
Server.Minute
csharp
public void UpdateProductionMetrics()
{
// Run every minute
if (@Server.Minute % 5 == 0) // Every 5 minutes
{
@Dataset.Query.ProductionReport.Select = true;
// Log execution
@Tag.LastUpdate = DateTime.Now;
}
}
csharp
public void MouseLeftButtonDown1(object sender, System.Windows.Input.InputEventArgs e)
{
DataTable results = @Dataset.Query.ActiveOrders.SelectCommand();
}
Info | ||
---|---|---|
| ||
You can use the Debugging Scripts Code to debug this piece of code and view the values in the "results" variable |
Excerpt Include | ||||||
---|---|---|---|---|---|---|
|
Page Tree | ||||
---|---|---|---|---|
|