Dataset SQL Query (Tutorial) teaches you to:
Prerequisites:
Table of Contents maxLevel 2 minLevel 2 indent 10px exclude Steps style none
Name: ProductionDB
Provider: System.Data.SQLite
Database: SQLite
ConnectionString: [Auto-configured]
Name: ProductionDB
Provider:
SqlClient
System.Data.SqlClient
Database: SQLServer
ConnectionString:
Data Source=SERVER\INSTANCE;
Initial Catalog=Production;
Integrated Security=True
MySQL:
MySQLData
MySQL
Server=localhost;Database=scada;Uid=user;Pwd=password;
ActiveOrders
ProductionDB
sql
SELECT
OrderID,
ProductName,
Quantity,
DueDate,
Status
FROM Orders
WHERE Status = 'Active'
ORDER BY DueDate
Create query with tag parameters:
Production by Date:
Name:ProductionReport
sql
SELECT
ProductID,
SUM(Quantity) as Total,
AVG(Quality) as AvgQuality
FROM Production
WHERE ProductionDate BETWEEN @StartDate {{Tag.Report_StartDate}}
AND @EndDate{{Tag.Report_EndDate}}
AND Line = @LineNumber
{{Tag.Selected_Line}}
GROUP BY ProductID
Parameters:
Bind parameters to tags:
Report_StartDate
Report_EndDate
Selected_Line
The {{Tag.Name}}
syntax embeds tag values at runtime.
@Dataset.Query.ActiveOrders
Call SP:
UpdateInventory
sql
EXEC sp_UpdateInventory
@ProductID = @ProductID,
@Quantity = @Quantity,
@Location = @Location
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(), @EventType{{Tag.EventType}}, @Description, @User, @Value{{Tag.Description}},
{{Tag.UserName}}, {{Tag.Value}})
csharp
@Dataset.Query.LogEvent.Execute(
"@EventType",// Set values
@Tag.EventType = "Alarm",;
"@Description",@Tag.Description = "High Temperature",;
@Tag.UserName = "@User", @Client.UserName,;
@Tag.Value = @Tag.TankFarm/Tank1/Temp;
// "@Value", @Tag.Temperature
);
Recipe Table:
Recipes
ProductionDB
RecipeData
RecipeID
Access in scripts:
csharp
DataRow recipe = @Dataset.Table.Recipes.SelectRow("RecipeID=101");
@Tag.Recipe_Name = recipe["Name"];
@Tag.Recipe_Temp = recipe["Temperature"];
Create scheduled task:
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 query every hourminute
if @Dataset.Query.ProductionReport.Execute();
// Store results in tags(@Server.Minute % 5 == 0) // Every 5 minutes
{
DataTable dt = @Dataset.Query.ProductionReport.ResultData;Select = true;
// Log execution
@Tag.TotalProductionLastUpdate = dt.Compute("SUM(Total)", "");
}DateTime.Now;
}
}
Page Tree | ||||
---|---|---|---|---|
|