Connecting An S7-1500 To An SQL Database
Connecting An S7-1500 To An SQL Database
Connecting An S7-1500 To An SQL Database
S7-1500 to an
SQL Database
Siemens
TIA Portal V16 / S7-1500 / Microsoft SQL / TDS Industry
Online
https://support.industry.siemens.com/cs/ww/en/view/109779336 Support
Legal information
Legal information
Use of application examples
Application examples illustrate the solution of automation tasks through an interaction of several
components in the form of text, graphics and/or software modules. The application examples are
a free service by Siemens AG and/or a subsidiary of Siemens AG (“Siemens”). They are non-
binding and make no claim to completeness or functionality regarding configuration and
equipment. The application examples merely offer help with typical tasks; they do not constitute
customer-specific solutions. You yourself are responsible for the proper and safe operation of the
products in accordance with applicable regulations and must also check the function of the
respective application example and customize it for your system.
Siemens grants you the non-exclusive, non-sublicensable and non-transferable right to have the
application examples used by technically trained personnel. Any change to the application
examples is your responsibility. Sharing the application examples with third parties or copying the
application examples or excerpts thereof is permitted only in combination with your own products.
The application examples are not required to undergo the customary tests and quality inspections
of a chargeable product; they may have functional and performance defects as well as errors. It is
your responsibility to use them in such a manner that any malfunctions that may occur do not
result in property damage or injury to persons.
Disclaimer of liability
Siemens shall not assume any liability, for any legal reason whatsoever, including, without
limitation, liability for the usability, availability, completeness and freedom from defects of the
application examples as well as for related information, configuration and performance data and
any damage caused thereby. This shall not apply in cases of mandatory liability, for example
under the German Product Liability Act, or in cases of intent, gross negligence, or culpable loss of
life, bodily injury or damage to health, non-compliance with a guarantee, fraudulent
non-disclosure of a defect, or culpable breach of material contractual obligations. Claims for
damages arising from a breach of material contractual obligations shall however be limited to the
© Siemens 2020 All rights reserved
foreseeable damage typical of the type of agreement, unless liability arises from intent or gross
negligence or is based on loss of life, bodily injury or damage to health. The foregoing provisions
do not imply any change in the burden of proof to your detriment. You shall indemnify Siemens
against existing or future claims of third parties in this connection except where Siemens is
mandatorily liable.
By using the application examples you acknowledge that Siemens cannot be held liable for any
damage beyond the liability provisions described.
Other information
Siemens reserves the right to make changes to the application examples at any time without
notice. In case of discrepancies between the suggestions in the application examples and other
Siemens publications such as catalogs, the content of the other documentation shall have
precedence.
The Siemens terms of use (https://support.industry.siemens.com) shall also apply.
Security information
Siemens provides products and solutions with industrial security functions that support the secure
operation of plants, systems, machines and networks.
In order to protect plants, systems, machines and networks against cyber threats, it is necessary
to implement – and continuously maintain – a holistic, state-of-the-art industrial security concept.
Siemens’ products and solutions constitute one element of such a concept.
Customers are responsible for preventing unauthorized access to their plants, systems, machines
and networks. Such systems, machines and components should only be connected to an
enterprise network or the Internet if and to the extent such a connection is necessary and only
when appropriate security measures (e.g. firewalls and/or network segmentation) are in place.
For additional information on industrial security measures that may be implemented, please visit
https://www.siemens.com/industrialsecurity.
Siemens’ products and solutions undergo continuous development to make them more secure.
Siemens strongly recommends that product updates are applied as soon as they are available
and that the latest product versions are used. Use of product versions that are no longer
supported, and failure to apply the latest updates may increase customer’s exposure to cyber
threats.
To stay informed about product updates, subscribe to the Siemens Industrial Security RSS Feed
at: https://www.siemens.com/industrialsecurity.
Table of contents
Legal information ......................................................................................................... 2
1 Introduction ........................................................................................................ 4
1.1 Overview............................................................................................... 4
1.2 Principle of operation............................................................................ 5
1.3 Components Used ................................................................................ 6
2 Engineering ........................................................................................................ 7
2.1 Interface description ............................................................................. 7
2.2 Integration into the user project ............................................................ 9
2.3 Operation of LSql_Microsoft ............................................................... 12
2.4 Error handling ..................................................................................... 15
3 Useful information ........................................................................................... 16
3.1 Microsoft SQL Server Express basics ................................................ 16
3.2 Settings in Microsoft SQL Server Express ......................................... 17
4 Appendix .......................................................................................................... 21
4.1 Service and support ........................................................................... 21
4.2 Links and literature ............................................................................. 22
4.3 Change documentation ...................................................................... 22
© Siemens AG 2020 All rights reserved
1 Introduction
1.1 Overview
Initial situation
The Tabular Data Stream protocol (TDS) gives you the ability to establish a direct
connection with a Microsoft SQL server. Using TDS, you can log in to an SQL
server database and transmit SQL instructions. In this way it is possible to read
data from the database, or send them to the database for storage.
This application example demonstrates how a SIMATIC S7-1500 establishes a
connection to a Microsoft SQL server via TDS and sends data to a database using
"Open User Communication blocks" (TCON, TSEND, TRCV and TDISCON).
SIMATIC S7-1500
SQL
SQL Database
© Siemens AG 2020 All rights reserved
PROFINET / IE
Configure
connection
Connect
Establish error
Error
connection
Connection fully
Connected
established
© Siemens AG 2020 All rights reserved
Execute
Formulate SQL
instruction
Transmit SQL
Executed
instruction
Disconnect
2 Engineering
2.1 Interface description
Function description
The function block "LSql_Microsoft" emulates the TDS protocol on the basis of
"Open User Communication blocks". It facilitates the following actions:
• Logging in to a Microsoft SQL server database (enable, connSettings,
loginInformation)
• Transmitting SQL instructions (sqlCommand, executeSqlCommand)
• Archiving received data (enableArchive, db, dbmax)
Internally, the block works with helper blocks. They are not explained here in more
detail.
Block interface
The following figure shows the interfaces of the function block "LSql_Microsoft" and
the associated data types.
Note Under the following conditions, this module is also functional with an S7 1200
from firmware V4.4 or higher:
Restrictions
The following restrictions apply for this application example:
• The application example only works with the tested hardware and software
© Siemens AG 2020 All rights reserved
versions.
• Using Open User Communication with an S7-1500, a maximum of 65536 bytes
per command can be sent or received.
• The block "LSql_Microsoft" may be called no more than once per Microsoft
SQL server.
Figure 2-2
© Siemens AG 2020 All rights reserved
Figure 2-3
© Siemens AG 2020 All rights reserved
Note The wiring of the parameters described here is an essential requirement for
operating the block. In this application example, data are transmitted from the
controller to storage in the SQL server. It is not necessary to use an archive to
do this.
The connection settings and login information must be cleared on the SQL
server side in order to establish the connection. Further information on this topic
can be found in chapter Settings in Microsoft SQL Server Express.
Table 2-4
Parameter Note
1. IP address and port of the SQL Default port for Microsoft SQL server is 1433.
server
2. SQL server login information See Settings in Microsoft SQL Server Express
3. Name of SQL server In this application example: SQLEXPRESS
4. Name of the database of the SQL An SQL server can contain multiple databases.
server Use this parameter to specify which database
you wish to connect to.
Figure 2-4
You can add a new row to this database table with "insert into". "Values" specifies
the value to be entered in the first (5), second (6) and third (7) column.
The SQL instruction is transmitted to the database with a positive edge at the input
"executeSqlCommand". The following figure shows the contents of the table
"PLCDATA" after this SQL instruction is executed.
Figure 2-5
© Siemens AG 2020 All rights reserved
3 Useful information
3. In the security settings, activate the "SQL Server Authentication Mode" and
confirm this change with "OK".
4. Create a user account under "Security > Logins > New Login…".
5. Assign a user name and select "SQL Server authentication". Set a password
and confirm the entries with "OK".
© Siemens AG 2020 All rights reserved
Note Make sure that this user receives the permissions to access this database.
Note Port 1433 is the default port for Microsoft SQL server databases.
Note With a firewall active on the PC with the Microsoft SQL server database, the TCP
port "1433" must be allowed in the firewall for incoming connections.
A port authorization must be set up in the SQL server so that the SQL server is
reachable on the network. The necessary steps are described below.
1. Start the "Microsoft SQL Server Configuration Manager" and navigate to the
protocols for "SQLEXPRESS". Double click to open the TCP/IP settings.
© Siemens AG 2020 All rights reserved
2. Enable TCP/IP.
3. Under IP3, configure the IP address of the network interface and the port 1433.
Enable the interface and confirm the settings with "OK".
4. Restart the SQL server service for the changes to take effect.
© Siemens AG 2020 All rights reserved
4 Appendix
4.1 Service and support
Industry Online Support
Do you have any questions or need assistance?
Siemens Industry Online Support offers round the clock access to our entire
service and support know-how and portfolio.
The Industry Online Support is the central address for information about our
products, solutions and services.
Product information, manuals, downloads, FAQs, application examples and videos
– all information is accessible with just a few mouse clicks:
support.industry.siemens.com
Technical Support
The Technical Support of Siemens Industry provides you fast and competent
support regarding all technical queries with numerous tailor-made offers
– ranging from basic support to individual support contracts. Please send queries
to Technical Support via Web form:
www.siemens.com/industry/supportrequest
© Siemens AG 2020 All rights reserved
Service offer
Our range of services includes the following:
• Plant data services
• Spare parts services
• Repair services
• On-site and maintenance services
• Retrofitting and modernization services
• Service programs and contracts
You can find detailed information on our range of services in the service catalog
web page:
support.industry.siemens.com/cs/sc