利用vb脚本操作WINCC V6.2 ASIA自带SQL2005数据库,在SQL2005数据库中新建一个数据表,利用全局脚本向此表中写数据。此表中设一id列,数据类型为int ,其中Identity Specification中Identity increment 为1,Identity seed设为1。问题是我向数据库中写数据时,写第1条数据时,id值应该为1,写第2条数据时,id值应该写2...而实际上id值不是按设定进行保存,id值保存值是1,2,3,84,85,136...,存在保存无序现象,另外经常存在数据保存不上,十条数据只能保存2-3条不等。请问专家我的数据库操作,保存数据错误问题到底出在哪? 是否是自建数据表通过vb全局脚本操作WINCC SQL2005数据库不可靠,还是操作数据库时与WINCC调用SQL2005有冲突?
请专家给予详细解答!谢谢!
最佳答案
放到Action里面去,时间周期自己定,内容自己修改
Option Explicit
Function action
Dim objConnection
Dim strConnectionString
Dim lngValue
Dim strSQL
Dim objCommand
Dim fdcount
Dim objRecordSet
Dim RecordCount
Dim temp1,temp2,temp3,temp4,temp5,temp6,temp7,temp8,temp9,temp10,temp11,temp12,temp13,temp14,temp15,temp16,temp17
'MsgBox "here"
If Minute(Now)=0 Then
Dim Dates
Dim Hours
Dim Cn
Dim F1,F2,F3,F4,F5,F6,F7,F8,F9,F10
Dim F11,F12,F13,F14,F15,F16,F17,F18,F19,F20
Dim F21,F22,F23,F24,F25,F26,F27,F28,F29,F30
Dim F31,F32,F33,F34,F35,F36,F37,F38,F39,F40
Dim F41,F42,F43,F44,F45,F46,F47,F48,F49,F50,F51,F52
Dates=Int(Now)
Hours=Hour(Now)
Set F1=HMIRuntime.Tags("OT2")
F1.read
Set F2=HMIRuntime.Tags("OT3")
F2.read
Set F3=HMIRuntime.Tags("OT4")
F3.read
Set F4=HMIRuntime.Tags("OT5")
F4.read
Set F5=HMIRuntime.Tags("CX201ZGYL01IND")
F5.read
Set F6=HMIRuntime.Tags("CX201QTLL04IND")
F6.read
Set F7=HMIRuntime.Tags("CX201QTLL03IND")
F7.read
Set F8=HMIRuntime.Tags("CX201QTLL02IND")
F8.read
Set F9=HMIRuntime.Tags("CX201QTLL01IND")
F9.read
Set F10=HMIRuntime.Tags("OT11")
F10.read
Set F11=HMIRuntime.Tags("OT12")
F11.read
Set F12=HMIRuntime.Tags("OT13")
F12.read
Set F13=HMIRuntime.Tags("OT14")
F13.read
Set F14=HMIRuntime.Tags("OT15")
F14.read
Set F15=HMIRuntime.Tags("OT16")
F15.read
Set F16=HMIRuntime.Tags("OT17")
F16.read
Set F17=HMIRuntime.Tags("OT19")
F17.read
Set F18=HMIRuntime.Tags("OT20")
F18.read
Set F19=HMIRuntime.Tags("OT21")
F19.read
Set F20=HMIRuntime.Tags("OT22")
F20.read
Set F21=HMIRuntime.Tags("OT23")
F21.read
Set F22=HMIRuntime.Tags("OT24")
F22.read
Set F23=HMIRuntime.Tags("OT25")
F23.read
Set F24=HMIRuntime.Tags("OT26")
F24.read
Set F25=HMIRuntime.Tags("OT27")
F25.read
Set F26=HMIRuntime.Tags("OT28")
F26.read
Set F27=HMIRuntime.Tags("OT29")
F27.read
Set F28=HMIRuntime.Tags("OT30")
F28.read
Set F29=HMIRuntime.Tags("OT31")
F29.read
Set F30=HMIRuntime.Tags("OT32")
F30.read
Set F31=HMIRuntime.Tags("OT33")
F31.read
Set F32=HMIRuntime.Tags("OT34")
F32.read
Set F33=HMIRuntime.Tags("CX201GFJ801CUR")
F33.read
Set F34=HMIRuntime.Tags("CX201GFJ802CUR")
F34.read
Set F35=HMIRuntime.Tags("CX201GFJ803CUR")
F35.read
Set F36=HMIRuntime.Tags("CX201GFJ804CUR")
F36.read
Set F37=HMIRuntime.Tags("CX201GFJ805CUR")
F37.read
Set F38=HMIRuntime.Tags("CX102DQSP01TTOT")
F38.read
Set F39=HMIRuntime.Tags("CX102DQSP02TTOT")
F39.read
Set F40=HMIRuntime.Tags("CX102DQSP03TTOT")
F40.read
Set F41=HMIRuntime.Tags("CX102DQSP04TTOT")
F41.read
Set F42=HMIRuntime.Tags("CX102TQSP01TTOT")
F42.read
Set F43=HMIRuntime.Tags("OT46")
F43.read
Set F44=HMIRuntime.Tags("OT47")
F44.read
Set F45=HMIRuntime.Tags("OT48")
F45.read
Set F46=HMIRuntime.Tags("OT49")
F46.read
Set F47=HMIRuntime.Tags("OT50")
F47.read
Set F48=HMIRuntime.Tags("CX301TJWL01IND")
F49.read
Set F49=HMIRuntime.Tags("CX301TJWL02IND")
F50.read
Set F50=HMIRuntime.Tags("CX301TJWL03IND")
F51.read
Set F51=HMIRuntime.Tags("CX301TJWL04IND")
F52.read
'MsgBox "here"
strSQL="insert into D_data("
strSQL=strSQL & "Date,Hours,dtime,F1,F2,F3,F4,F5,F6,F7,F8,F9,F10,"
strSQL=strSQL & "F11,F12,F13,F14,F15,F16,F17,F18,F19,F20,"
strSQL=strSQL & "F21,F22,F23,F24,F25,F26,F27,F28,F29,F30,"
strSQL=strSQL & "F31,F32,F33,F34,F35,F36,F37,F38,F39,F40,"
strSQL=strSQL & "F41,F42,F43,F44,F45,F46,F47,F48,F49,F50,F51,F52"
strSQL=strSQL & ") values('" & Date & "','" & Time & "','" & Date + Time & "'," & F1.Value & "," & F2.Value & "," & F3.Value & "," & F4.Value & "," & F5.Value & "," & F6.Value & "," & F7.Value & "," & F8.Value & "," & F9.Value & "," & F10.Value & ","
strSQL=strSQL & F11.Value & "," & F12.Value & "," & F13.Value & "," & F14.Value & "," & F15.Value & "," & F16.Value/10 & "," & F17.Value & "," & F18.Value & "," & F19.Value & "," & F20.Value & ","
strSQL=strSQL & F21.Value & "," & F22.Value & "," & F23.Value & "," & F24.Value & "," & F25.Value & "," & F26.Value & "," & F27.Value & "," & F28.Value & "," & F29.Value & "," & F30.Value & ","
strSQL=strSQL & F31.Value & "," & F32.Value & "," & F33.Value & "," & F34.Value & "," & F35.Value & "," & F36.Value & "," & F37.Value & "," & F38.Value & "," & F39.Value & "," & F40.Value & ","
strSQL=strSQL & F41.Value & "," & F42.Value & "," & F43.Value & "," & F44.Value & "," & F45.Value & "," & F46.Value & "," & F47.Value/10 & "," & 0 & "," & F49.Value & "," & F50.Value & "," & F51.Value & "," & F52.Value & ")"
' MsgBox "here"
'strConnectionString = "Provider=MSDASQL;DSN=CXWS_DSN;UID=;PWD=;"
'
'Set objConnection = CreateObject("ADODB.Connection")
'
'objConnection.ConnectionString = strConnectionString
'objConnection.Open
'Set objCommand = CreateObject("ADODB.Command")
'With objCommand
' .ActiveConnection = objConnection
' .CommandText = strSQL
'End With
Set Cn=CreateObject("ADODB.Connection")
Cn.open "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=CXWS_DB;Data Source=cxoperator3\wincc"
Cn.Execute strSQL
Cn.close
Set Cn=Nothing
'objCommand.Execute
'Set objCommand = Nothing
'objConnection.Close
'Set objConnection = Nothing
End If
End Function
提问者对于答案的评价:
谢谢专家和各位热心的解答,利用VB脚本操作SQL2005数据库确实存在不稳定问题。特别是当脚本较多时,数据存储可能问题更多。
原创文章,作者:more0621,如若转载,请注明出处:https://www.zhaoplc.com/plc267912.html