Ladder Code to send data to SQL You have created a connection setup log into the SQL data base and tested it. Now we want to add ladder code to send data to/from the SQL server. There are 3 basic parts to this step: 1) DB_Connect and DB_Close instructions to log in and log out of the SQL the database. 2) A DB_CreateMapping instruction to create a map between NJ Tags and SQL fields/Table. 3) DB_Insert/Select/Delete/Update instructions. • Insert: to append data to the table • Select: which allows you to query data from the table • Delete: delete a record. • UpDate: modify a record. This is an example of how to write the connection program:
The _DBC_Status.Run bit lets you know the DB service has actually started up and is running. ‘NJDB2’ with single quotes is how you specify the DB Connection you want to use. The “Done” bit lets you know if you successfully connected to the database/table. The SQL_DBConnection variable is the reference variable to this database connection for all the other instructions including SQL_Close. I show it in blue for most of this document. It is a good idea to close the database when the controller is about to shut down. Page 9 of 21