Configure database connections and execute queries.
Tutorials → Datasets | Tutorial | How-to Guide | Reference
Connect to external databases, execute queries, and integrate SQL data with your FrameworX solution. Work with multiple database types and display query results.
Configure database connections
Create queries with parameters
Display data in tables
Table of Contents maxLevel 2 minLevel 2 indent 10px exclude Teaches style none
SQL Server:
ProductionDB
SqlClient
Name: ProductionDB
Provider: SQLite Data Provider
Database: .Net Framework Data Provider for SQLite
ConnectionString: [Auto-configured]
Name: ProductionDB
Provider: SqlClient Data Provider
Database: SQL Server Database
ConnectionString:
Data Source=SERVER\INSTANCE;
Initial Catalog=Production;Integrated Security=True
MySQL:
MySQLData
MySQL
Server=localhost;Database=scada;Uid=user;Pwd=password;
(your database 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 parameters:
Production by Date:
ProductionReport
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:
Bind parameters to tags:
Report_StartDate
Report_EndDate
Selected_Line
SELECT "TankFarm/Tank1/Temperature" FROM ProcessData
SELECT "TankFarm/Tank1/Temperature" FROM ProcessData WHERE UTCTimestamp_Ticks >= (strftime('%s', {Tag.TankFarm/Tank1/Temperature.Timestamp}, '-10 days') * 10000000) + 621355968000000000
@Dataset.Query.ActiveOrders
Call SP:
UpdateInventory
sql
EXEC sp_UpdateInventory
@ProductID = @ProductID,
@Quantity = @Quantity,
@Location = @Location
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
);
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:
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)", "");
}
Create button to run query:
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 | ||||
---|---|---|---|---|
|