Data interaction between wincc and Access database (data)

The data storage of wincc is currently mainly stored in the database, and the current mainstream databases are mainly SQL Server and Access databases. This article is mainly about how to read data between wincc and Access databases.

entry description
Operating environment wincc7.5 SP1
Access database 2010 edition

Run screen

Insert picture description here

Operating procedures

Insert picture description here

Create an Access database table

1. Create an Access database file, file name: Wincc_Access_Data.accdb
2. Create a database table, table name: Machineinfo, the fields in the table are: ID, Pressure, Temperature, Flow. As shown in the figure:
Insert picture description here
3. Put the created Access database file in the root directory of the disk.

Configuration screen

1. Wincc creates the picture, the picture name is DataRead.Pdl
2. The picture adds the control object.

Object name
Button default
Microsoft Hierarchical FlexGrid Control,Version6.0(OLEDB) 控件 Grid

3. Write a script, in the event of the button, click the mouse to add a VB script

Sub OnClick(Byval Item)    
Dim conn,ors
Dim Grid
Set conn = CreateObject("ADODB.Connection")'创建对象
conn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Persist Security Info=False;Data Source=C:\Wincc_Access_Data.accdb"'创建链接字符串,Data Source后面为数据库文件的路径和名称
conn.open '打开对象
Set ors = CreateObject("ADODB.RecordSet") '创建记录对象
ors.open "select ID,Pressure,Temperature,Flow from Machineinfo",conn,3,3 '创建sql语句,其中ID等为字段名,machineinfo为数据库表名
Set Grid = ScreenItems("Grid")
Set Grid.DataSource = ors  '将读取到的数据写入到MSHFGrid控件表格中
Grid.Refresh               '刷新表格
'关闭数据链接
Set ors = Nothing
conn.close
Set conn = Nothing

End Sub

Precautions

  1. When testing, be careful not to set the field in the Access database to Chinese, preferably in English
  2. When creating an Access database file, there will be two file suffixes, mdb and accdb. In fact, these two are the file formats of the Access database, but they represent different versions of the Access database. The mdb is before version 07 (not including version 07). accdb is version 07 or later. Therefore, the Provider link string is also different for different file suffixes.
'mdb:
Provider=Microsoft.Jet.OleDb.4.0
'accdb:
Provider=Microsoft.ACE.OLEDB.12.0
  1. When creating a database, do not put the database file in the Chinese directory, try to put it in the root directory or the English folder under the root directory, and the path should not be too long.
  2. The data queried by the sql statement is guaranteed to be correct.
  3. The variables defined by the wincc script should remain the same as those actually used.

Resource acquisition

Guess you like

Origin blog.csdn.net/yue008/article/details/114048883