SQL Server CRUD Operations with WinCC VBScript and ADODB
This lab targets wincc vbscript sql server crud with copy-ready examples, expected results and diagnostic guidance.
1. Use a Least-Privilege ADODB Connection
Dim connection
Set connection = CreateObject("ADODB.Connection")
connection.Open "Provider=MSOLEDBSQL;Server=SOFTWELL\WINCC;" & _
"Database=SQF_DB;Trusted_Connection=Yes;"
If connection.State <> 1 Then
HMIRuntime.Trace "SQL connection did not open"
End IfUse the installed, approved provider and the Runtime service identity. Grant only the required operations on the intended tables.
2. Insert with ADODB Parameters
Dim command
Set command = CreateObject("ADODB.Command")
Set command.ActiveConnection = connection
command.CommandText = "INSERT INTO dbo.ProcessLog " & _
"(EventTime, Temperature) VALUES (?, ?)"
command.CommandType = 1
command.Parameters.Append command.CreateParameter("pTime", 135, 1, , Now)
command.Parameters.Append command.CreateParameter("pTemp", 5, 1, , 650.5)
command.ExecuteParameter markers represent values, not table or column names. Keep identifiers as reviewed constants.
3. Read Records Safely
Dim recordset
Set recordset = connection.Execute( _
"SELECT TOP (10) EventTime, Temperature " & _
"FROM dbo.ProcessLog ORDER BY EventTime DESC")
Do Until recordset.EOF
HMIRuntime.Trace CStr(recordset.Fields("EventTime").Value) & _
" | " & CStr(recordset.Fields("Temperature").Value)
recordset.MoveNext
Loop
4. Update and Delete with Guard Conditions
Every UPDATE and DELETE needs a reviewed WHERE clause based on a key. Test against disposable rows, check affected-record counts and never expose unrestricted SQL text to an operator field.
5. Transactions and Cleanup
Use BeginTrans, CommitTrans and RollbackTrans when several writes must succeed together. Close the recordset, release the command, close the connection and handle rollback on every failure path.
6. SQL CRUD Troubleshooting
| Problem | Action |
|---|---|
| Provider not found | Install/approve the matching OLE DB provider and check process bitness |
| Login failed | Verify the WinCC Runtime identity and SQL permissions |
| Parameter type error | Match ADODB parameter type, size and direction to the SQL column |
| Database remains locked | Close recordsets/connections and avoid long transactions in Runtime scripts |
Hands-On Verification Lab
Hands-onPrepare a controlled test
Create only the internal tags, folders or disposable database rows required by this tutorial.
Run the smallest example
Execute one operation and inspect WinCC diagnostics before expanding the script.
Test a failure path
Use a safe backup copy to test an invalid tag, path or disposable input.
Frequently Asked Questions
Should this WinCC VBScript be tested in production?
No. Use a backed-up training or staging project and follow the plant change-control process.
Which WinCC versions does the example target?
The examples target classic WinCC Explorer V7.x/V8.x. Verify object properties, action signatures and providers in the installed WinCC help.
How should Runtime failures be diagnosed?
Use scoped Err checks, WinCC object diagnostics and HMIRuntime.Trace while recording the exact station, trigger, tag and Runtime identity.
