在WinCC中通过VBS操作SQL Server2005
在项目中需要在一定条件满足时,保存一些数据到数据库中,并可根据条件查询。考虑到WinCC6.2以后采用的就是SQL Server2005数据库,所以直接利用该数据库即可,通过SQL Server Management Studio(SSMS)可以创建自己的数据库,并安要求创建好表。
一、数据库连接
在SQL Server Management Studio(SSMS)中创建名为evcp的数据库,再创建名为evcp的表,然后根据需要创建Columns,在本项目中创建了norder(流水号)、pileno(桩号)、cardno(卡号)、operno(员工号)、energy(电量)、cost(金额)、period(时长)、rate(费率)、pdate(日期)和ptime(时间)。
在本项目中采用ODBC的方式连接数据库,首先在控制面板中创建好数据源,配置好SQL Server驱动数据源,命名为evcs。
二、数据写入
要求在一个状态量值为1的时候完成数据库的保存,等数据保存完后将状态量清0。
1、先在全局脚本VBS项目模块中创建函数savedata,代码如下:
Sub savedata
Dim objConnection
Dim objCommand
Dim objRecordset
Dim strConnectionString
Dim strSQL
Dim norder,pileno,cardno,operno,energy,cost,period,rate,pdate,ptime norder=HMIRuntime.Tags("norder").Read
pileno= HMIRuntime.Tags("pileno").Read
cardno=HMIRuntime.Tags("cardno").Read
operno= HMIRuntime.Tags("operno").Read
energy= HMIRuntime.Tags("energy").Read
cost= HMIRuntime.Tags("cost").Read
period= HMIRuntime.Tags("period").Read
rate= HMIRuntime.Tags("rate").Read
pdate= HMIRuntime.Tags("pdate").Read
ptime= HMIRuntime.Tags("ptime").Read strConnectionString = "Provider=MSDASQL;DSN=evcs;UID=;PWD=;"
Set objConnection = CreateObject("ADODB.Connection")
objConnection.ConnectionString = strConnectionString
objConnection.Open Set objRecordset = CreateObject("ADODB.Recordset")
Set objCommand = CreateObject("ADODB.Command")
objCommand.ActiveConnection = objConnection strSQL = "insert into evcp (norder,pileno,cardno,operno,energy,cost,period,rate,pdate,ptime) values ("&_
"'"&norder&"',"&_
"'"&pileno&"',"&_
"'"&cardno&"',"&_
"'"&operno&"',"&_
"'"&energy&"',"&_
"'"&cost&"',"&_
"'"&period&"',"&_
"'"&rate&"',"&_
"'"&pdate&"',"&_
"'"&ptime&"')"
'MsgBox (strSQL)
objCommand.CommandText = strSQL
objCommand.Execute Set objCommand = Nothing
objConnection.Close
Set objRecordset = Nothing
Set objConnection = Nothing
End Sub
2、在全局脚本VBS动作中创建1秒周期的周期性出发动作,并添加如下代码:
Option Explicit
Function action
Dim v1
v1=HMIRuntime.Tags("satuse").Read If v1 Then
Call savedata
HMIRuntime.Tags("satuse").Write
End if
End Function
这样当satuse值为1时系统自动保存数据
三、数据查询
数据的查询要复杂一些,需要用到MSFlexGrid控件、MS Form2 ComboBox控件和MS Form2 TextBox,这几个控件可以单独注册也可以安装VB6后自动添加。
在查询页面上添加打开页面执行脚本如下:
Sub OnOpen()
Dim MSFlexGrid1,cb1,tb1,tb2
Set MSFlexGrid1 = ScreenItems("控件1")
Set cb1 = ScreenItems("cb1")
Set tb1 = ScreenItems("tb1")
Set tb2 = ScreenItems("tb2")
MSFlexGrid1.Clear MSFlexGrid1.ColWidth() =
MSFlexGrid1.ColWidth() =
MsFlexGrid1.ColWidth() =
MSFlexGrid1.ColWidth() =
MSFlexGrid1.ColWidth() =
MSFlexGrid1.ColWidth() =
MsFlexGrid1.ColWidth() =
MSFlexGrid1.ColWidth() =
MSFlexGrid1.ColWidth() =
MSFlexGrid1.ColWidth() =
MsFlexGrid1.ColWidth() = MSFlexGrid1.TextMatrix(,) = "编号"
MSFlexGrid1.TextMatrix(,) = "流水号"
MSFlexGrid1.TextMatrix(,) = "桩号"
MSFlexGrid1.TextMatrix(,) = "卡号"
MSFlexGrid1.TextMatrix(,) = "操作员号"
MSFlexGrid1.TextMatrix(,) = "电量(度)"
MSFlexGrid1.TextMatrix(,) = "金额(元)"
MSFlexGrid1.TextMatrix(,) = "时长(分)"
MSFlexGrid1.TextMatrix(,) = "费率(元/度)"
MSFlexGrid1.TextMatrix(,) = "日期"
MSFlexGrid1.TextMatrix(,) = "时间" MSFlexGrid1.ColAlignment() =
MSFlexGrid1.ColAlignment() =
MSFlexGrid1.ColAlignment() =
MSFlexGrid1.ColAlignment() =
MSFlexGrid1.ColAlignment() =
MSFlexGrid1.ColAlignment() =
MSFlexGrid1.ColAlignment() =
MSFlexGrid1.ColAlignment() =
MSFlexGrid1.ColAlignment() =
MSFlexGrid1.ColAlignment() =
MSFlexGrid1.ColAlignment() = Dim i
For i= To Step
MSFlexGrid1.TextMatrix(i,) = i
Next cb1.Text="*"
cb1.AddItem "*"
cb1.AddItem ""
cb1.AddItem ""
tb1.Text="*"
tb2.Text="*" End Sub
这段代码主要是用来初始化控件的显示。
在查询按钮加入相应的代码实现三个键值(桩号、卡号、操作员号)条件组合的查询,代码如下:
Sub Click(Byval Item)
Dim objConnection
Dim objCommand
Dim objRecordset
Dim strConnectionString
Dim strSQL
Dim MSFlexGrid1,cb1,tb1,tb2
Dim i1,i2,cv1,cv2,cv3,cv
Set MSFlexGrid1 = ScreenItems("控件1")
Set cb1 = ScreenItems("cb1")
Set tb1 = ScreenItems("tb1")
Set tb2 = ScreenItems("tb2") '清除原有记录
For i1 = To Step
For i2= To Step
MSFlexGrid1.TextMatrix(i1, i2) =""
Next
Next
strConnectionString = "Provider=MSDASQL;DSN=evcs;UID=;PWD=;"
Set objConnection = CreateObject("ADODB.Connection")
objConnection.ConnectionString = strConnectionString
objConnection.Open
Set objRecordset = CreateObject("ADODB.Recordset")
Set objCommand = CreateObject("ADODB.Command")
objCommand.ActiveConnection = objConnection cv1=
cv2=
cv3=
If cb1.Text<>"*" Then
cv1=
End If If tb1.Text<>"*" Then
cv2=
End if If tb2.Text<>"*" Then
cv3=
End If
cv=cv1+cv2+cv3
Select Case cv
Case
strSQL = "select * from evcp where pileno like '"&cb1.Text&"'And cardno like '"&tb1.Text&"'And operno like '"&tb2.Text&"'"
Case
strSQL = "select * from evcp where pileno like '"&cb1.Text&"'And cardno like '"&tb1.Text&"'"
Case
strSQL = "select * from evcp where pileno like '"&cb1.Text&"'And operno like '"&tb2.Text&"'"
Case
strSQL = "select * from evcp where pileno like '"&cb1.Text&"'"
Case
strSQL = "select * from evcp where cardno like '"&tb1.Text&"'And operno like '"&tb2.Text&"'"
Case
strSQL = "select * from evcp where cardno like '"&tb1.Text&"'"
Case
strSQL = "select * from evcp where operno like '"&tb2.Text&"'"
Case Else
strSQL = "select * from evcp"
End Select objCommand.CommandText = strSQL
Set objRecordset = objCommand.Execute Dim i
i=
If (objRecordset.Bof And objRecordset.Eof) Then
MsgBox("没有符合要求的记录")
Else
While Not objRecordset.EOF
i=i+
MSFlexGrid1.TextMatrix(i, ) = CStr(objRecordset.Fields().Value)
MSFlexGrid1.TextMatrix(i, ) = CStr(objRecordset.Fields().Value)
MSFlexGrid1.TextMatrix(i, ) = CStr(objRecordset.Fields().Value)
MSFlexGrid1.TextMatrix(i, ) = CStr(objRecordset.Fields().Value)
MSFlexGrid1.TextMatrix(i, ) = CStr(objRecordset.Fields().Value)
MSFlexGrid1.TextMatrix(i, ) = CStr(objRecordset.Fields().Value)
MSFlexGrid1.TextMatrix(i, ) = CStr(objRecordset.Fields().Value)
MSFlexGrid1.TextMatrix(i, ) = CStr(objRecordset.Fields().Value)
MSFlexGrid1.TextMatrix(i, ) = CStr(objRecordset.Fields().Value)
MSFlexGrid1.TextMatrix(i, ) = CStr(objRecordset.Fields().Value)
objRecordset.movenext
Wend
End If Set objCommand = Nothing
objConnection.Close
Set objRecordset = Nothing
Set objConnection = Nothing
End Sub
在WinCC中通过VBS操作SQL Server2005的更多相关文章
- delphi向SQL Server2005中存取图片
SQL Server2005中,我用image类型来存取图片,首先把数据库表设置好 例如我的pic表有如下两列:时间,图片. delphi中,我用ADOQuery来连接数据库,但是数据库中有好几张表, ...
- 使用Hive或Impala执行SQL语句,对存储在HBase中的数据操作
CSSDesk body { background-color: #2574b0; } /*! zybuluo */ article,aside,details,figcaption,figure,f ...
- 使用Hive或Impala执行SQL语句,对存储在Elasticsearch中的数据操作(二)
CSSDesk body { background-color: #2574b0; } /*! zybuluo */ article,aside,details,figcaption,figure,f ...
- 使用Hive或Impala执行SQL语句,对存储在Elasticsearch中的数据操作
http://www.cnblogs.com/wgp13x/p/4934521.html 内容一样,样式好的版本. 使用Hive或Impala执行SQL语句,对存储在Elasticsearch中的数据 ...
- PL/SQL“ ORA-14551: 无法在查询中执行 DML 操作”解决
环境 Oracle 11.2.0 + SQL Plus 问题 根据以下要求编写函数:将scott.emp表中工资低于平均工资的职工工资加上200,并返回修改了工资的总人数.PL/SQL中有更新的操作, ...
- LINQ To SQL在N层应用程序中的CUD操作、批量删除、批量更新
原文:LINQ To SQL在N层应用程序中的CUD操作.批量删除.批量更新 0. 说明 Linq to Sql,以下简称L2S. 以下文中所指的两层和三层结构,分别如下图所示: 准确的说,这里 ...
- SQL点滴33—SQL中的字符串操作
原文:SQL点滴33-SQL中的字符串操作 计算字符串长度len()用来计算字符串的长度 select sname ,len(sname) from student 字符串转换为大.小写lower() ...
- SQL点滴18—SqlServer中的merge操作,相当地风骚
原文:SQL点滴18-SqlServer中的merge操作,相当地风骚 今天在一个存储过程中看见了merge这个关键字,第一个想法是,这个是配置管理中的概念吗,把相邻两次的更改合并到一起.后来在tec ...
- SQL点滴2—重温sql语句中的join操作
原文:SQL点滴2-重温sql语句中的join操作 1.join语句 Sql join语句用来合并两个或多个表中的记录.ANSI标准SQL语句中有四种JOIN:INNER,OUTER,LEFTER,R ...
随机推荐
- 【BZOJ-2653】middle 可持久化线段树 + 二分
2653: middle Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 1298 Solved: 734[Submit][Status][Discu ...
- js获取输入框中当前光标位置并在此位置插入字符串的方法(angularjs+ts)
一半是参照别人代码,一半是自己代码,略笨拙,如果有更好的方法希望分享. 获取当前光标位置的方法 getCaretPosition (obj:any) { //获取输入框中当前光标的位置,obj为此输入 ...
- python 常用内建模块(3) base64
Base64是一种用64个字符来表示任意二进制数据的方法. 用记事本打开exe.jpg.pdf这些文件时,我们都会看到一大堆乱码,因为二进制文件包含很多无法显示和打印的字符,所以,如果要让记事本这样的 ...
- String、StringBuffer与StringBuilder之间区别
关于这三个类在字符串处理中的位置不言而喻,那么他们到底有什么优缺点,到底什么时候该用谁呢?下面我们从以下几点说明一下 1.三者在执行速度方面的比较:StringBuilder > String ...
- eclipse使用sublime配色(转)
转自 Eclipse设置类似Sublime Text 编辑区皮肤,风格,颜色 1.首先打开eclipse 2.help -> Install New SoftWare 3.点击 Add 在Na ...
- 服务器端之间采用http接口调数据时的Cookie传值问题
public static string UrlGet(string url) { string responseContent = ""; string cookieValue ...
- WordPress酷炫CSS3读者墙代码
前几日在大前端看到他站点中最新的CSS3读者墙代码,一看效果绚丽的不得鸟,立刻就开始研究了,多次研究未果,可终究是研究出来了,昨天刚成功,今天啊和童鞋来我站说读者墙头像显示不对,我一看,还真是,头像都 ...
- Android广播接收器BroadcastRceiver
一.使用BroadcastRceiver 1.创建BroadcastRceiver(MyRceiver),重写OnReceiver: public void onReceive(Context con ...
- Java多线程--线程安全问题的相关研究
在刚刚学线程的时候我们经常会碰到这么一个问题:模拟火车站售票窗口售票.代码如下: package cn.blogs.com.isole; /* 模拟火车站售票窗口售票,假设有50张余票 */ publ ...
- 利用beans.xml进行简单的Spring应用上下文创建与使用
继上次配置Spring完成后,我们来创建一个简单的例程来理解Spring中利用beans.xml创建应用上下文的方法. 程序路径包为:com.spring.kinghts(kinght单词拼写错误,怕 ...