请问一下,我想利用wincc的vb脚本在自己创建的sql数据库(该自定义数据库是常规数据库,在sql中用sql语句创建)中周期存储变量值(包括时间,变量名,变量值)
我利用create database guowincc创建了数据源是guowincc
利用create table testwincc(
one 变量名,
tow 归档时间,
three 归档值,
primary key(one,tow)
)
创建这个testwincc表的话我不知道one,tow,three各应该定义为什么类型。
wincc里面的变量的类型各对应数据库中自定义的哪些类型呢?
比如wincc里的“有符号16位数”该对应sql中的哪个类型?
“浮点数 32位 IEEE 754”又对应sql中的什么类型呢?
wincc中的“日期时间”对应sql中是什么呢?
如果我想把wincc中用vb的Now()得到的时间变成字符串存储的话是不是用
Dim nowTimeStr
nowTimeStr = Format(Now,"yyyy年MM月dd日 HH时nn分ss秒")
就可以了呢?
我在winncc里写VB的脚本
Dim objConnection
Dim strConnectionString
Dim nowTime
Dim lngValue
Dim strSql
Dim objCommand
strConnectionString = "Provider = MSDASQL;DSN = guowincc;UID = ;PWD = ;"
lngValue = HMIRuntime.Tags("NewTag1_inter").Read 'NewTag1_inter是我自定义的归档变量
tagTime = Now() '这句语句正确吗?
strSql = "insert into testwincc values("'NewTag1_inter',&nowTime&,&lngValue&");"
'尤其是这句语句正确吗?
Set objConnection = CreateObject("ADOB.Connection");
objConnection.ConnectionString = strConnectinString
objConnection.open
Set objCommand = CreateObject("ADOB.Command")
with objCommand
.ActiveConnection = objConnection
.CommandText = strSql
end with
objCommand.Execute
Set objCommand = Nothing
objConnection.Close
Set objConnection = Nothing
看看上面的有没有错误啊,wincc运行起来太耗时间了,所以我想把疑问都解决了再去激活wincc。
vb动作脚本调试出错:Microsoft vbscript 运行时错误:Activex部件不能创建对象:'ADODB.Connection
出错位置是:Set objConnection = CreateObject("ADOB.Connection")
以下是脚本(有省略)
Sub 按钮1_OnClick(ByVal Item)
....
Dim nowTime
Dim lngValue
Dim strSql
Dim objCommand
strConnectionString = "Provider = MSDASQL;DSN = guowincc;UID = ;PWD = ;"
lngValue = HMIRuntime.Tags("NewTag1_inter").Read
nowTime = Now()
strSql = "insert into testwincc values('NewTag1_inter',"& nowTime & "," & lngValue & ");"
Set objConnection = CreateObject("ADOB.Connection")
objConnection.ConnectionString = strConnectinString
....
End Sub
WinCC的VBS虽可以声明Public变量,但作用域仅限于本画面,如下:
如果WinCC版本>=V6.0 SP3,则可以使用Dataset对象,该对象可以在不同的VBS脚本中进行访问,下面是个简单的例子:
1、连接
Dim objConnection,strConnectionString
strConnectionString ="Driver={SQL Server};Server=.\WINCC;uid=;pass=;database=guowincc"
Set objConnection = CreateObject("ADODB.Connection")
objConnection.ConnectionString = strConnectionString
objConnection.open
HMIRuntime.DataSet.Add "objConnection",objConnection
2、全局动作中引用
Dim objConnection
objConnection= HMIRuntime.DataSet("objConnection").Value
with objCommand
.ActiveConnection = objConnection
xxxxxxxx
3、关闭连接:
Dim objConnection
objConnection= HMIRuntime.DataSet("objConnection").Value
objConnection.Close
Set objConnection = Nothing