Dataset SQL Query (Tutorial) teaches you to:
Prerequisites:
Name: ProductionDB
Provider: System.Data.SQLite
Database: SQLite
ConnectionString: [Auto-configured]
Name: ProductionDB
Provider: System.Data.SqlClient
Database: SQLServer
ConnectionString:
Data Source=SERVER\INSTANCE;
Initial Catalog=Production;
Integrated Security=True
ActiveOrders
ProductionDB
sql
SELECT
OrderID,
ProductName,
Quantity,
DueDate,
Status
FROM Orders
WHERE Status = 'Active'
ORDER BY DueDate
Create query with tag parameters:
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.
@Dataset.Query.ActiveOrders
Create 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;
}
}