在官网上和网上搜了很多资料,只找到了经典版wincc直接读取自带数据库的vb脚本,如下
“TAG:R,<ValueID or ValueName>,<TimeBegin>,<TimeEnd>[,<SQL_clause>][,<TimeStep>]”
其中:
ValueName:格式为“ArchiveNameValue_Name”,可以使用多个名称。
TimeBegin,TimeEnd:时间范围,格式 “YYYY-MM-DD hh:mm:ss.msc”。
SQL_Clause:SQL 语法中的过滤标准。
没找到TIA中实现同样功能的脚本结构,有哪位大神出来解惑一下。
Dim con
con = "Provider=SQLOLEDB.1;Password=123456;Persist Security Info=True;User ID=sa;Initial Catalog=CC_HMI_4KVJ_25_06_24_14_35_09R;Data Source=WH-GJEIR8TIL1D4WINCC"
Dim begintime,endtime,Sqlbegintime,Sqlendtime,timesp
Set begintime=HMIRuntime.Tags("begintime")
begintime.Read
Set endtime=HMIRuntime.Tags("endtime")
endtime.Read
Set timesp=HMIRuntime.Tags("timesp")
timesp.Read
Sqlbegintime = DateAdd("h" ,-8,begintime.Value)
Sqlendtime= DateAdd("h" ,-8,endtime.Value)
Sqlbegintime = Year(Sqlbegintime) & "-" & Month(Sqlbegintime) & "-" & Day(Sqlbegintime) & " " & Hour(Sqlbegintime) & ":" & Minute(Sqlbegintime) & ":" & Second(Sqlbegintime)
Sqlendtime = Year(Sqlendtime) & "-" & Month(Sqlendtime) & "-" & Day(Sqlendtime) & " " & Hour(Sqlendtime) & ":" & Minute(Sqlendtime) & ":" & Second(Sqlendtime)
Dim strsql,conn1
Set conn1=CreateObject("ADODB.Connection")
conn1.ConnectionString=con
conn1.CursorLocation=3
conn1.Open
MsgBox "数据库连接成功"
Dim rst
Set rst = CreateObject("ADODB.Recordset")
Dim com1
Set com1= CreateObject("ADODB.Command")
With com1
.CommandType=1
.ActiveConnection=conn1
End With
Dim fso,myfile
Dim ObjExcelApp
Set ObjExcelApp = CreateObject("Excel.Application")
ObjExcelApp.Visible = False
Set fso=ObjExcelApp.Workbooks.Open("D:wincc_excell_testExcellcode2.xls")
ObjExcelApp.worksheets ("sheet2").Cells(1, 1)="时间"
Dim checkbox,i,j,TagName
Set checkbox=ScreenItems("checkbox_1")
For i=1 To checkbox.CountVisibleItems
checkbox.Index =i
TagName = checkbox.Text
ObjExcelApp.worksheets ("sheet2").Cells(1, 1+i).VAlue = checkbox.Text
MsgBox TagName
strsql = "Tag:R,'TagName','" & Sqlbegintime & "','" & Sqlendtime & "'," & "'order by Timestamp ASC','TimeStep=60,1'"
com1.CommandText=strsql
Set rst=com1.Execute
Dim count
count=rst.RecordCount
MsgBox "3"
rst.movefirst
MsgBox "4"
For j=2 To count+1
ObjExcelApp.worksheets ("sheet2").Cells(j, 1).VAlue = DateAdd("h",+8,rst.fields(1).value)
ObjExcelApp.worksheets ("sheet2").Cells(j, i+1).VAlue = rst.fields(2).value
Next
rst.movenext
Next
MsgBox "5"
ObjExcelApp.ActiveWorkbook.Save
ObjExcelApp.Workbooks.Close
ObjExcelApp.Quit
Set ObjExcelApp = Nothing
Set rst=Nothing
conn1.close
Set conn1=Nothing
这是刚刚重新修改过的脚本,在“msgbox TagName”这一行指令还是能正常执行的,显示出来了下面的图片,
。然后就报错下面的内容
============Microsoft OLE DB Provider for SQL Server============
Application :
Picture : 画面_2.pdl_Events
Function : Sub CON_SQL_SERVER()
Line : 75
Error : Incorrect syntax near 'Tag:'.
============Microsoft OLE DB Provider for SQL Server============

这是修改了一些脚本之后的显示出来strsql里面的内容,还是一样报错