Configuring a MSSQL Database Connection with Vbscript in wincc

Published on Dec26, 2024 | Category: wincc tia
Share this article:

Microsoft SQL Server (MSSQL) is a relational database management system (RDBMS). it stores and retrieves data as request. in MSSQL data organized in table each table have row and column. MSSQL is easily connect with third party software by Data Source Name (DSN).A DSN is often used with ODBC (Open Database Connectivity).in an automation industries we use MSSQL to store tag value and application data which we required for many application like report generation, trend analyses, alarm history, data logging etc. in this article step by step guide to configure database connection and how to read and write tag value. vbscript is a programming language which also support database connection and read and write tagvalue.

what is Microsoft SQL server Management studio

Microsoft SQL Server Management Studio (SSMS) is an integrated environment for managing, configuring, and interacting with SQL Server instances. It is a powerful tool provided by Microsoft for database administrators (DBAs), developers, and analysts to manage databases, write queries, and perform administrative tasks. SSMS is free and can be downloaded from the official Microsoft website.

img/wincc vbscript for sql/what is Microsoft SQL server Management studio.webp

how to create tag database in MSSQL

first step to create tag database to store tag value so we can read and write these tag value. a tag database an organized collection of tag values that can be easily accessed, managed, and updated. You can create database by Using the GUI (Graphical User Interface) or Using SQL Query. follow these steps to create a database in MSSQL. Using the GUI (Graphical User Interface):

  1. In the Object Explorer, expand the Server node.
  2. Right-click on the Databases folder.
  3. Select New Database from the context menu.
  4. In the New Database window:
    • Enter the Database Name (tagdatabase).
    • Review or modify settings like file locations or sizes if needed (optional).
  5. Click OK to create the database.

img/wincc vbscript for sql/how to create tag database in MSSQL.webp

how to create database Using SQL Query:

  1. Open a new query window by clicking New Query in the toolbar.
  2. Write the following SQL code: "CREATE DATABASE tagdatabase2;"
  3. Execute the query by pressing F5 or clicking the Execute button.

img/wincc vbscript for sql/how to create database Using SQL Query.webp

how to configure dsn for wincc sql server

A Data Source Name (DSN) is a configuration that provides the necessary information for an application to connect to a database, such as the database driver, server address, and authentication details. DSN commonly stands for Data Source Name, a configuration used in computing to define how an application connects to a database. Step 1: Open ODBC Data Source Administrator

  1. open control panel
  2. open windows tools
  3. Choose the appropriate version of ODBC:
    • Use the 32-bit version if your application is 32-bit.
    • Use the 64-bit version if your application is 64-bit.
here we select 32 bit version for this article.

img/wincc vbscript for sql/how to configure dsn for sql server.webp

Step 2: Add a New DSN

  1. In the ODBC Data Source Administrator, go to the System DSN or User DSN tab:
    • System DSN: Available to all users on the system.
    • User DSN: Available only to the current user.
  2. Click the Add button.

img/wincc vbscript for sql/Step 2 Add a New DSN.webp

select driver for DSN

  • Select SQL Server or ODBC Driver for SQL Server from the list of drivers and click Finish.
  • img/wincc vbscript for sql/configure the dsn.webp
    Step 3: Configure the DSN
    1. In the Create a New Data Source to SQL Server window:
      • Enter a name for the DSN (e.g., MySql).
      • Provide an optional description.
      • Select the SQL Server instance to connect.
    2. Click Next.

    img/wincc vbscript for sql/enter name of DSN.webp

    Step 4: Configure Authentication

    1. Choose the authentication method:
      • Windows Authentication: Uses the current Windows user's credentials.
      • SQL Server Authentication: Requires a username and password.
    2. Click Next.

    img/wincc vbscript for sql/Configure Authentication.webp

    Step 5: Configure the Default Database

    1. Select the default database for the DSN from the dropdown menu.
    2. Click Next.

    img/wincc vbscript for sql/Configure the Default Database.webp

    Step 6: Test the DSN

    1. Click the Test Data Source button to ensure the DSN is working correctly.
    2. If the test is successful, click Finish.

    img/wincc vbscript for sql/Test the DSN.webp

    open tia portal and create a project

    open tia portal and create a new project enter details of project or add hmi in your project in hmi screen add following component

    img/wincc vbscript for sql/open tia portal and create a project.webp

    vbscript for sql database connection in wincc tia portal

    add a script and rename to check_connection. assign vbscript to button1 press event property. when button is pressed if database is connected than text field changed to database connection successfully otherwise text field changed database not connected.

    img/wincc vbscript for sql/vbscript for sql database connection in wincc tia portal.webp

    Step-by-Step Explanation