IMDTouchKnowledge BaseShop main store

Explainer

Connecting WinCC to an external database: direct options

HMI and touchscreens Published 6 min read

Short answer

Yes — classic WinCC on a PC runtime reaches an external database by itself: a VBScript or C script over ODBC, the C-API, or OPC when the source publishes tags. Add a second application only when the runtime cannot open the connection or a measured retrieval misses the required cycle.

On this page
  1. The direct interfaces at a glance
  2. Reading rows over ODBC from a script
  3. OPC moves tags, not result sets
  4. Leave WinCC's own database out of it
  5. Put numbers on "fairly high speed"
  6. When a second application earns its place
  7. Questions asked

WinCC does not need a second application to read an external database. Classic WinCC on a PC opens the database itself: a VBScript over ODBC (VBScript arrived with WinCC 6.0), a C script through the ODBC API, or the WinCC C-API when compiled code is warranted. OPC is on the list of connection options too, but it transports tag values rather than query results, so it applies when the source system can publish items over OPC. Build the direct path first, measure it against the required refresh cycle, and add an intermediary only when the runtime cannot open the connection or the measured retrieval misses the budget.

The direct interfaces at a glance #

Interface What it actually carries Commit to it when
ODBC from a VBScript Script opens the database and runs queries (WinCC 6.0 and later) Default answer for retrieving rows into a screen
ODBC from a C script Same driver path driven from C (in use through the V6 SP3 generation) You prefer C, or the release predates VBScript
C-API Compiled integration into WinCC A needed hook is out of scripting's reach, or a measured cycle demands compiled performance
OPC, server or client Tag values between WinCC and another system The data source already exposes an OPC server

The WinCC release decides between the first two rows. Below WinCC 6.0 there is no VBScript, so the ODBC path runs through C scripts and the interface examples Siemens published for those releases; from 6.0 on the documentation itself carries a VBScript ODBC example, and that is the quickest route to a working read.

Reading rows over ODBC from a script #

The database is reached through its own ODBC driver installed on the runtime PC. The driver is the compatibility boundary, not WinCC: engineers have pointed the same script approach at SQL Server, Oracle, Access, MySQL and PostgreSQL. Three design decisions make the read fast, and none of them is middleware:

  • Open the connection once, at first read or screen start, and reuse it. Opening the connection is the expensive step; reopening it every cycle multiplies the round trip.
  • Fetch everything a screen needs in one SELECT. Ten fields at ten round trips cost ten times one round trip.
  • Trigger the read on the refresh cycle, write the results into internal tags, and let the screen objects bind to those tags.

One rule belongs here for safety: give the runtime a read-only database account. An operator-facing script has no business holding credentials that can update production tables.

If the runtime is WinCC Advanced rather than classic WinCC, stop before blaming the driver: the same read can come back empty on that runtime, and why a WinCC Advanced SQL read script returns nothing works through the reasons.

OPC moves tags, not result sets #

WinCC ships an OPC server — in WinCC V8.0 it is configured through the file OPCUAServerWinCC.xml under <WinCC project folder>\OPC\UAServer — and it can act as an OPC client toward other servers. Neither role runs SQL: the server publishes WinCC tags, the client subscribes to foreign items. Which end acts as server and which as client is its own design question, and HMI OPC UA server vs client: which direction works covers it.

So the decision is short. If the source system already has an OPC server, OPC is the clean transport and you are done. If the data lives in a bare database, OPC helps only when some other program publishes the rows as OPC items — and that program is the middleware you were trying to avoid.

Leave WinCC's own database out of it #

Older WinCC releases are built on a Sybase SQL Anywhere database. That is WinCC's internal archive, not a general-purpose endpoint, and it is the wrong direction for this job: it holds what WinCC logged, never the external rows you must fetch. Siemens also ties direct access to that internal database to a licensing condition. Read history through the archive and trend functions; read external data through your own connection.

Put numbers on "fairly high speed" #

"High speed" is not an interface choice; it is two numbers you set before choosing. Define T, the refresh period the operation tolerates, and measure q, the query round trip under real load on the bench. Two rules decide everything:

  • Keep q at or under T/2, so the script never occupies more than half the cycle and graphics keep the rest.
  • A serial script cannot exceed 1/q reads per second, whatever drives it.

Worked example — the numbers below are example bench values, not specifications; substitute your own measurements. A recipe screen needs 8 values refreshed every T = 2.0 s and must answer a Load press within 1.0 s. On the bench, one SELECT returning all 8 values over a held-open connection takes q = 120 ms; the same data pulled as 10 separate lookups, each opening its own connection, takes 480 ms per lookup.

  • One query, held connection: 120 ms against a 2000 ms cycle is 6% utilization, and 120 ms answers the Load press well inside the 1.0 s limit. The direct script passes — ship it, no second application.
  • Ten queries, held connection: 10 × 120 ms = 1200 ms. That meets the 2.0 s refresh but eats 60% of the cycle and misses the 1.0 s Load limit. Still no middleware — merge the lookups into one SELECT.
  • Ten queries, each opening a connection: 10 × 480 ms = 4800 ms, which is 4800/2000 = 2.4× the whole cycle. The interface is identical; only the query design failed.

The one outcome that justifies a second application is measured: if the merged, held-connection query still reads over budget on your bench, move the retrieval into a C-API component or a small service that pushes the values into WinCC as tags.

When a second application earns its place #

Three cases, and only three:

  • The runtime cannot open the connection at all. Panel-based runtimes are not PC runtimes; if the station is a panel, put the database read on a PC, either as a PC runtime or as a small service feeding tags.
  • The interface is unsupported on the runtime PC — no driver for that database, or a protocol the scripts cannot speak.
  • The measured miss above. Performance you have measured, not performance you fear.

The old interface examples also list coarser cooperation mechanisms: Win32 DLL functions called from scripts, window messages handed to a WinCC callback, and plain text-file exchange. They are legitimate, but a text file adds a file-system hop and no query capability; commit to it only when data minutes old is acceptable.

Direction matters as much as interface. Retrieving reference data into a screen is the job above; continuously writing process values out to SQL is a different architecture, and logging PLC data to SQL: use a gateway, not the HMI sets out why the HMI is the wrong writer. If the plant runs no SCADA package at all, writing PLC data directly to SQL databases without SCADA covers the writer side.

Questions asked

Can the script read MySQL or PostgreSQL, or only SQL Server?

Yes, within the driver boundary. Any database with an ODBC driver installed on the runtime PC is reachable from a classic WinCC script, and the same ADO approach has been pointed at MySQL, PostgreSQL, Oracle and Access as well as SQL Server. Where it fails is the runtime, not the database: panel-based runtimes are the ones where a database read comes back empty.

Is a C script faster than VBScript for ODBC reads?

Not where it matters. The round trip is dominated by the connection open, query execution and moving the result over the network; the interpreter overhead of either script language is small next to those. Fix connection reuse and query shape first, then pick C for the API hooks and compile-time checks it offers, not for speed.

How do I calculate the maximum read rate?

One divided by the measured round trip: a 120 ms query tops out at about 8 reads per second run serially, and you should stay well under that ceiling so the cycle keeps room for graphics. Raise the rate by holding the connection open, shortening the query and fetching several values per query, not by polling faster.

Can I get the external data out of WinCC's tag logging instead?

No, and it is the wrong direction. Tag logging stores what WinCC itself archived; it never holds your external system's rows, so no amount of archive access retrieves them. For history, use WinCC's archive and trend functions; for external data, open your own connection, and note that direct access to the internal database carries a licensing condition.

Why isn't OPC the answer for database rows?

Because OPC transports tag values, not query results. The server WinCC ships, configured in V8.0 through OPCUAServerWinCC.xml, publishes WinCC tags and never runs SQL. OPC serves this need only when another program already publishes the database as OPC items, and that program is exactly the middleware you were trying to avoid.

Keep reading

  • How-to

    Connecting an Omron CV1000 PLC to WinCC over an OPC server

    Classic WinCC has no Omron CV-series channel, so the working route is an OPC server that speaks Host Link to the CV1000 and OPC DA to WinCC. Configure the server against the PLC first, prove one read, then build WinCC tags from the server's item addresses. Use the documented CV1000-CPU01-EV1 Host Link reference values: ASCII, 7 data bits, even parity, 2 stop bits, unit 00, 9600 or 19200 bps.

    HMI and touchscreens

  • How-to

    Connecting Operator Terminal Expert to a Third-Party PLC

    Operator Terminal Expert connects to a third-party PLC through a protocol both ends document — typically Modbus TCP or RTU, or OPC UA with the HMI as client and the PLC as server. Select the driver for your exact terminal target, match the driver's address notation to the PLC's register references, and prove one read and one controlled write on the deployed terminal before mapping the rest.

    HMI and touchscreens

  • Troubleshooting

    Why a WinCC Advanced SQL Read Script Returns Nothing

    The read script fails for faults the working INSERT never meets: a missing space turns SELECT * FROM into FROMKayitlar, a dangling ORDER BY has no column, and MoveFirst after MoveNext pins the cursor on row one. Clear them in order: DSN visible to the runtime, SQL text valid, recordset non-empty, value inside the DInt range. Each fault masks the next.

    HMI and touchscreens