项目场景:
将wincc的变量定时存储到sql server数据库(需要设置定时触发器)
代码实现:
Dim sCon, conn, oRs, sql_command, oCom, strcn
Dim R1
Set R1 = HMIRuntime.Tags("S7R2_R1")
R1.Read
Dim R2
Set R2 = HMIRuntime.Tags("S7R2_R2")
R2.Read
Dim R3
Set R3 = HMIRuntime.Tags("S7R2_R3")
R3.Read
Dim R4
Set R4 = HMIRuntime.Tags("S7R2_R4")
R4.Read
Dim R5
Set R5 = HMIRuntime.Tags("S7R2_R5")
R5.Read
sCon = "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist SecurityInfo=False;Initial Catalog=XC;Data Source=WIN-NMP6G9UVVU8\WINCC" '(需要改catalog为数据库名称,source为服务器名称或者计算机名称)
Set conn = CreateObject("ADODB.Connection")
conn.ConnectionString = sCon
conn.CursorLocation = 3
conn.Open
Set oRs = CreateObject("ADODB.Recordset")
Set oCom = CreateObject("ADODB.Command")
oCom.CommandType = 1
Set oCom.ActiveConnection = conn
sql_command = "insert into my_table (DT, R1, R2, R3, R4, R5) Values(GETDATE(), '"&R1.Value&"', '"&R2.Value&"', '"&R3.Value&"', '"&R4.Value&"', '"&R5.Value&"')" '(my_table 为表名,需要修改)
conn.Execute sql_command
conn.Close