0% found this document useful (0 votes)
130 views

Tech Note 1007 - Wonderware Application Server Scripting Implementations Part 1 Inserting A DB Row During Each Scan Cycle

This document demonstrates how to insert one data row into a database table during each scan cycle of an Application Server engine using scripts. It discusses using the Scheduler's ScanCyclesCnt attribute to trigger a script on each scan cycle. The implementation creates scripts that connect to a SQL database, insert a row with a unique value during each scan in the Execute phase, and disconnect on completion in the OffScan phase. It emphasizes best practices for database connections in scripts.

Uploaded by

profilemail8
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
130 views

Tech Note 1007 - Wonderware Application Server Scripting Implementations Part 1 Inserting A DB Row During Each Scan Cycle

This document demonstrates how to insert one data row into a database table during each scan cycle of an Application Server engine using scripts. It discusses using the Scheduler's ScanCyclesCnt attribute to trigger a script on each scan cycle. The implementation creates scripts that connect to a SQL database, insert a row with a unique value during each scan in the Execute phase, and disconnect on completion in the OffScan phase. It emphasizes best practices for database connections in scripts.

Uploaded by

profilemail8
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Wonderware Application Server Scripting Implementations Part 1: Inserting a DB Row During Each Scan Cycle

Tech Note 1007


Wonderware Application Server Scripting Implementations Part 1: Inserting a DB Row During
Each Scan Cycle

All Tech Notes, Tech Alerts and KBCD documents and software are provided "as is" without warranty of any kind. See the Terms of Use for more information.

Topic#: 002839
Created: January 2014

Introduction
Scripting is one of the most powerful features in the Application Server world. This Tech Note will demonstrate

The approach that can insert one data row into a database table during each AppEngine's scan cycle.

The proper sequence for using scripts’ Execution Types.

Effective use of ArchestrA Objects' predefined attributes in the script's Expression for special requests.

Application Versions
Wonderware Application Server 2012 and later

Inserting a DB Row in Each Scan Cycle


The most interesting part of this task is how to set up the proper script expression that triggers the corresponding implementation. Since
the condition is each Scan Cycle, we know this condition is related to the AppEngine's Scheduler. From the Object Viewer, we found
some predefined Scheduler's attributes:

file:///C|/inetpub/wwwroot/t002839/t002839.htm[1/28/2014 8:55:54 AM]


Wonderware Application Server Scripting Implementations Part 1: Inserting a DB Row During Each Scan Cycle

FIGuRE 1: APPENGINE's ScHEDuLER's AttRIButEs

From examining each Scheduler's attribute, Scheduler.ScanCyclesCnt is the attribute we can use if we set the trigger at every
CHANGING point. This analysis brings the following screenshot which shows the script Trigger Conditions.

file:///C|/inetpub/wwwroot/t002839/t002839.htm[1/28/2014 8:55:54 AM]


Wonderware Application Server Scripting Implementations Part 1: Inserting a DB Row During Each Scan Cycle

FIGuRE 2: EXPREssION aND TRIGGER TYPE SEtuP

As the result, we see the DB row is indeed inserted during each Scan Cycle.

file:///C|/inetpub/wwwroot/t002839/t002839.htm[1/28/2014 8:55:54 AM]


Wonderware Application Server Scripting Implementations Part 1: Inserting a DB Row During Each Scan Cycle

FIGuRE 3: DB ROWs ARE INsERtED

Implementation Details
The following steps show the detail implementation of inserting a DB row during each Scan Cycle.

Create a Database and Table in SQL Server


Before inserting data, we must create a single table database. The SQL script below creates a database, ScriptInsert and a table,
InsertUDA. The table has two columns, TableIndex and UDAValue.

Create database ScriptInsert


Go
Use ScriptInsert
Go

file:///C|/inetpub/wwwroot/t002839/t002839.htm[1/28/2014 8:55:54 AM]


Wonderware Application Server Scripting Implementations Part 1: Inserting a DB Row During Each Scan Cycle
Create Table dbo.InsertUDA
(TableIndex int identity(1,1) ,
UDAValue nvarchar(50),
)
Go

FIGuRE 4: CREatE INsERtUDA TaBLE IN ScRIPtINsERt DB

Application Server Scripts


The following steps are showing the details that can insert data into SQL database during each AppEngine's scan cycle.

1. Define attributes in the UDA. For this example, we define three UDAs with string type:
• ConnectStringUDA
• DatabaseName with default value ScriptInsert
• UDAValue

file:///C|/inetpub/wwwroot/t002839/t002839.htm[1/28/2014 8:55:54 AM]


Wonderware Application Server Scripting Implementations Part 1: Inserting a DB Row During Each Scan Cycle

FIGuRE 5: CREatE UDAs

2. Create a script called InsertDB, and define the following variables in the script's Declarations pane.

Dim aaDBConn As aaDBClient.aaDBConnection;


Dim aaDBCmd As aaDBClient.aaDBCommand;
Dim nodeName As String;
Dim lineValue As String ;
Dim sqlConnString As String ;
Dim QueryString As String;
Dim logMsg as String;
Dim retCode As Integer;
Dim loopCount As Integer;
Dim currDataSet As System.Data.DataSet;
Dim sqlDataTable As System.Data.DataTable;

file:///C|/inetpub/wwwroot/t002839/t002839.htm[1/28/2014 8:55:54 AM]


Wonderware Application Server Scripting Implementations Part 1: Inserting a DB Row During Each Scan Cycle

FIGuRE 6: DEcLaRE VaRIaBLEs

3. In the Script's OnScan pane, use the following code snippet for creating a database connection.

nodeName = System.Environment.MachineName;
sqlConnString = "Data Source = " + nodeName + ";Initial Catalog=" + me.DatabaseName + ";Integrated Security=true";
Me.ConnectStringUDA = sqlConnString;
LogMessage("sqlConnString: " + sqlConnString);
aaDBConn = aaDBAccess.CreateConnection(sqlConnString);
loopCount = 1;

file:///C|/inetpub/wwwroot/t002839/t002839.htm[1/28/2014 8:55:54 AM]


Wonderware Application Server Scripting Implementations Part 1: Inserting a DB Row During Each Scan Cycle

FIGuRE 7: CONNEct tO DataBasE

Important Note
As one of the best practices in AppServer’s Scripting, we strongly suggest that you put the above statement,
aaDBAccess.CreateConnection, into the OnScan or Startup Execution type no matter in which situation. In other words,
you only need to create the database connection once and use it in the Execute Execution type.

Please keep in mind that creating a database connection is always expensive. This is because

Creating a DB connection uses a substantial amount of memory on both the database server and database client
machines.

Establishing a DB connection takes multiple network round trips to and from the database server.

Opening numerous connections can contribute to out-of-memory conditions, which might cause paging of memory to
disk and, thus, overall performance degradation.

4. In the Script's Execute pane, use the following code snippet to insert data into database in each AppEngine's Scan Cycle.

• Trigger Expression: Scheduler.ScanCyclesCnt


• Trigger Type: DataChange
file:///C|/inetpub/wwwroot/t002839/t002839.htm[1/28/2014 8:55:54 AM]
Wonderware Application Server Scripting Implementations Part 1: Inserting a DB Row During Each Scan Cycle

loopCount = loopCount + 1;
Me.UDAValu = "String Value: " + loopCount;

QueryString = "Insert Into dbo.InsertUDA(UDAvalue) Values('" + Me.UDAValue + "')";

aaDBCmd = aaDBConn.CreateCommand(QueryString, aaDBCommandType.SqlStatement, true );


retCode = aaDBCmd.ExecuteSync();
logMsg = "aaDBCmd.ExecuteSync() returns " + retCode;
LogMessage(logMsg);

file:///C|/inetpub/wwwroot/t002839/t002839.htm[1/28/2014 8:55:54 AM]


Wonderware Application Server Scripting Implementations Part 1: Inserting a DB Row During Each Scan Cycle

FIGuRE 8: INsERt a ROW INtO DB IN EacH APPENGINE's ScaN CYcLE

5. In the Script's OffScan pane, use the following code snippet to shut down the database connection.

aaDBAccess.Shutdown();

file:///C|/inetpub/wwwroot/t002839/t002839.htm[1/28/2014 8:55:54 AM]


Wonderware Application Server Scripting Implementations Part 1: Inserting a DB Row During Each Scan Cycle

FIGuRE 9: SHutDOWN tHE INsERtING EXEcutION

Scripts' Execution Types


There are five Script Execution types in Application Server: Startup, OnScan, Execute, offScan and Shutdown.

Each type has different executing condition.

Startup: Scripts are called when the object containing the scripts is loaded into memory, such as during deployment, platform or
engine start.

OnScan: Scripts are called the first time an AppEngine calls this object to execute after the object's scan state changes to
OnScan. The OnScan method initiates local object attribute values or provides more flexibility in the creation of .NET or COM
objects.

Execute: Scripts are called each time the AppEngine performs a scan and the object is OnScan.

OffScan: Scripts are called when the object is taken OffScan. This script type is primarily used to clean up the object and account
for any needs to address as a result of the object no longer executing.

Shutdown: Scripts are primarily used to destroy COM objects and .NET objects, and to free memory.

References
file:///C|/inetpub/wwwroot/t002839/t002839.htm[1/28/2014 8:55:54 AM]
Wonderware Application Server Scripting Implementations Part 1: Inserting a DB Row During Each Scan Cycle

Wonderware Application Server 2012 R2 – IDE.PDF

B. Lin and E. Xu

Tech Notes are published occasionally by Wonderware Technical Support. Publisher: Invensys Systems, Inc., 26561 Rancho Parkway South, Lake Forest, CA 92630. There is also
technical information on our software products at Wonderware Technical Support.

For technical support questions, send an e-mail to [email protected].

Back to top

©2014 Invensys Systems, Inc. All rights reserved. No part of the material protected by this copyright may be reproduced or utilized in any form or by any means, electronic or
mechanical, including photocopying, recording, broadcasting, or by any information storage and retrieval system, without permission in writing from Invensys Systems, Inc.
Terms of Use.

file:///C|/inetpub/wwwroot/t002839/t002839.htm[1/28/2014 8:55:54 AM]

You might also like