Extensibility > Using External Databases > Using Structured Query Language > Connecting to an SQL Database

Connecting to an SQL Database

Using Legacy Functions

Before you can use SQL commands, you need to connect to the SQL database. In previous versions of Citect SCADA you would use the SQLConnect function to provide the access. It has the format:

SQLConnect(sConnect);

where sConnect is the connection string, for example:

INT hSQL;
hSQL = SQLConnect("DSN=DBASE_FILES;DB=C:\ODBC\EMP;LCK=NONE;CS=ANSI");
! Connect to a dBASE Compatible Database File.

INT hSQL;
hSQL = SQLConnect("DSN=EXCEL_FILE;DB=C:\ODBC\EMP;FS=10");
! Connect to an Excel File.

INT hSQL;
hSQL = SQLConnect("DSN=ORACLE_TABLES;SRVR=X:ACCTS;UID=SCOTT;PWD=TIGER");
! Connect to an Oracle Database.

Refer to the documentation that accompanied your SQL server for details about connecting to an SQL database.

To close a legacy database connection, use the SQLDisconnect function with the handle to the connection object.

Using ADO.NET Functions

The new interface requires two calls in order to open a connection to a database. The first call uses SQLCreate which prepares the database connection object. A second call SQLOpen establishes the SQL connection to the database.

SQLOpen can be used to reopen previously closed connections.

Note: SQL connections to databases can be closed sometimes by either databases or ADO.NET when certain conditions are fulfilled (for example through idle time expiration, active pooling mechanism logic).

To close an ADO.NET database connection, use the SQLClose function to close the connection and then the SQLQueryDispose with the handle to the connection object.

New functions can be used with the pre-existing ones. However, as a good programming practice, it is advised not to mix the old and the new interface. A hardware alarm will be generated if a SQLDisconnect is detected when there are connections opened with SQLCreate/SQLOpen, or if SQLClose/SQLDispose are detected and there are connections opened with SQLConnect.

See Also

Published June 2018