首页 新闻 工控搜 论坛 厂商论坛 产品 方案 厂商 人才 文摘 下载 展览
中华工控网首页
  P L C | 变频器与传动 | 传感器 | 现场检测仪表 | 工控软件 | 人机界面 | 运动控制
  D C S | 工业以太网 | 现场总线 | 显示调节仪表 | 数据采集 | 数传测控 | 工业安全
  电 源 | 嵌入式系统 | PC based | 机柜箱体壳体 | 低压电器 | 机器视觉
WinCC 如何访问数据库(VBS)
启程自动化培训中心
收藏本文     查看收藏

WinCC 如何访问数据库(VBS) 

 一、声名全局变量

在全局脚本中声明全局变量,如下:Public gocn

2、建立连接,如下:
Set gocn = CreateObject("ADODB.Connection")
gocn.open "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=GJS_SY;Data Source=LGL"

3、执行代码。例如插入一条记录:
gocn.Execute "insert into TB_FJ_DATA(LSH,GKH,T_time) VALUES('1',99,GETDATE( ))"
 

二、访问数据记录和查询

1、查询。代码如下:
On Error Resume Next
Dim NoOfRecords
Set NoOfRecords = HMIRuntime.Tags("NoOfRecords")
Dim CurrentRec
Set CurrentRec = HMIRuntime.Tags("CurrentRec")
Dim AuthorID
Set AuthorID = HMIRuntime.Tags("Au_ID")
Dim AuthorFirstName
Set AuthorFirstName = HMIRuntime.Tags("AuthorFirstName")
Dim AuthorLastName
Set AuthorLastName = HMIRuntime.Tags("AuthorLastName")
Dim AuthorsQueryRan
Set AuthorsQueryRan = HMIRuntime.Tags("AuthorsQueryRan")
ActivateDBHandling()
If gocn.State = 0 Then
HMIRuntime.Trace "no connection ... reconnecting"
ConnectDB()
End If
gorsAuthors.open "SELECT au_id, au_fname, au_lname, contract FROM Authors ORDER BY au_lname", gocn, 1, 3
If Not gorsAuthors.Eof Then
NoOfRecords.Write gorsAuthors.RecordCount
gorsAuthors.MoveFirst
AuthorID.Write gorsAuthors.Fields(0).Value
AuthorFirstName.Write gorsAuthors.Fields(1).Value
AuthorLastName.Write gorsAuthors.Fields(2).Value
CurrentRec.Write 1
AuthorsQueryRan.Write 1
Else
MsgBox "No Data Returned", vbOK, "Query Results"
End If
On Error Goto 0
 

2、插入
On Error Resume Next
Dim AuthorID
Set AuthorID = HMIRuntime.Tags("Au_ID_INSERT")
Dim AuthorFirstName
Set AuthorFirstName = HMIRuntime.Tags("AuthorFirstName_INSERT")
Dim AuthorLastName
Set AuthorLastName = HMIRuntime.Tags("AuthorLastName_INSERT")
Dim sSql
ActivateDBHandling()
If gocn.State = 0 Then
HMIRuntime.Trace "no connection ... reconnecting"
ConnectDB()
End If

Set gorsAuthorsChange = CreateObject("ADODB.Recordset")
sSql = "INSERT INTO Authors(au_id, au_fname, au_lname, contract) VALUES('" + AuthorID.Read + "','" + AuthorFirstName.Read + "', '" + AuthorLastName.Read + "',1)"
gorsAuthorsChange.open sSql, gocn, 1
MsgBox "Added new author", vbOKOnly, "INSERT"
Set gorsAuthorsChange = Nothing
On Error Goto 0
 

3、修改
On Error Resume Next
Dim AuthorID
Set AuthorID = HMIRuntime.Tags("Au_ID_UPDATE")
Dim AuthorFirstName
Set AuthorFirstName = HMIRuntime.Tags("AuthorFirstName_UPDATE")
Dim AuthorLastName
Set AuthorLastName = HMIRuntime.Tags("AuthorLastName_UPDATE")
Dim sSql
ActivateDBHandling()
If gocn.State = 0 Then
HMIRuntime.Trace "no connection ... reconnecting"
ConnectDB()
End If
Set gorsAuthorsChange = CreateObject("ADODB.Recordset")
sSql = "UPDATE Authors SET au_fname = '" + AuthorFirstName.Read + "', au_lname = '" + AuthorLastName.Read + "' WHERE au_id = '" + AuthorID.Read + "'"
gorsAuthorsChange.open sSql, gocn, 1
MsgBox "Modified author details", vbOKOnly, "UPDATE"
Set gorsAuthorsChange = Nothing
On Error Goto 0
 

4、删除

On Error Resume Next
Dim AuthorID
Set AuthorID = HMIRuntime.Tags("Au_ID_DELETE")
Dim sSql
ActivateDBHandling()
If gocn.State = 0 Then
HMIRuntime.Trace "no connection ... reconnecting"
ConnectDB()
End If
Set gorsAuthorsChange = CreateObject("ADODB.Recordset")
sSql = "DELETE FROM Authors WHERE au_id = '" + AuthorID.Read + "'"
gorsAuthorsChange.open sSql, gocn, 1
MsgBox "Deleted author", vbOKOnly, "DELETE"
Set gorsAuthorsChange = Nothing
On Error Goto 0
 更多技术资料,请加微信公众帐号“gongkong8 

工控技术博客: http://blog.sina.com.cn/wincchome 

启程自动化培中心 http://www.gongkong8.com/  

 联系电话:0755-85292922

  


 

状 态: 离线

会员简介

会员代号: 1347546707
联 系 人: 启程plc培训
电  话: 0755-32837892
传  真:
地  址: 深圳市宝安区82区华美居商务中心区D区1号楼
邮  编: 518101
主  页:
 
该厂商相关技术文摘:
西门子S7-200 PLC通讯连接不上
WinCC 如何在特定的时间点(非周期)归档过程变量?
绝对精通S7-200【PROFIBUS总线】网络的可靠性
称重传感器的接线方法
变频器和变频电源的工作原理及区别介绍
S7-200的表指令使用例程
跟我学西门子300之新GSD文件的安装使用【转载】
PLC梯形图编程基础认识全解【转载】
带你搞懂伺服电机和步进电机【转载】
跟我学西门子300之新GSD文件的安装使用【转载】
两线制三线制和四线制信号【转载至启程PLC培训】
更多文摘...
立即发送询问信息在线联系该技术文摘厂商:
用户名: 密码: 免费注册为中华工控网会员
请留下您的有效联系方式,以方便我们及时与您联络

关于我们 | 联系我们 | 广告服务 | 本站动态 | 友情链接 | 法律声明 | 不良信息举报
工控网客服热线:0755-86369299
版权所有 中华工控网 Copyright©2022 Gkong.com, All Rights Reserved