如何使用WinCC在SQL Server中插入当前日期和时间

时间:2018-12-06 10:07:20

标签: scada wincc

我正在使用WINCC将数据保存在SQL Server中,但无法插入当前数据和时间,请帮助我解决此问题

Sub OnClick(ByVal Item)                                      
Dim objConnection
Dim strConnectionString
Dim lngValue
Dim strSQL
Dim objCommand
strConnectionString = "Provider=MSDASQL;DSN=winnrc;UID=;PWD=;"
lngValue = HMIRuntime.Tags("JOB_ID").Read(1)
strSQL = "INSERT INTO NEWDATA (JOB_ID,SHIFT,QTY,WEIGHT,DECREASING_ST,DECREASING_TEMP_PV,DECREASING_LEVEL,CASCADE_RINSE_T4_ST,CASCADE_RINSE_T4_TEMP_PV,CASCADE_RINSE_T4_LEVEL,CASCADE_RINSE_T3_ST,CASCADE_RINSE_T3_TEMP_PV,CASCADE_RINSE_T3_LEVEL,PHOSPHATE_ST,PHOSPHATE_TEMP_PV,PHOSPHATE_LEVEL,trigger_0) VALUES (" & HMIRuntime.Tags("REPORT_BLOCK_JOB_ID").Read(1) & "," & HMIRuntime.Tags("REPORT_BLOCK_SHIFT").Read(1) & "," & HMIRuntime.Tags("REPORT_BLOCK_QUANTITY").Read(1) & "," & HMIRuntime.Tags("REPORT_BLOCK_WEIGHT").Read(1) & "," & HMIRuntime.Tags("REPORT_BLOCK_DECREASING_ST").Read(1) & "," & HMIRuntime.Tags("REPORT_BLOCK_DECREASING_TEMP_PV").Read(1) & "," & HMIRuntime.Tags("REPORT_BLOCK_DECREASING_LEVEL").Read(1) & "," & HMIRuntime.Tags("REPORT_BLOCK_CASCADE_RINSE_T4_ST").Read(1) & "," & HMIRuntime.Tags("REPORT_BLOCK_CASCADE_RINSE_T4_TEMP_PV").Read(1) & "," & HMIRuntime.Tags("REPORT_BLOCK_CASCADE_RINSE_T4_LEVEL").Read(1) & "," & HMIRuntime.Tags("REPORT_BLOCK_CASCADE_RINSE_T3_ST").Read(1) & "," & HMIRuntime.Tags("REPORT_BLOCK_CASCADE_RINSE_T3_TEMP_PV").Read(1) & "," & HMIRuntime.Tags("REPORT_BLOCK_CASCADE_RINSE_T3_LEVEL").Read(1) & "," & HMIRuntime.Tags("REPORT_BLOCK_PHOSPHATE_ST").Read(1) & "," & HMIRuntime.Tags("REPORT_BLOCK_PHOSPHATE_TEMP_PV").Read(1) & "," & HMIRuntime.Tags("REPORT_BLOCK_PHOSPHATE_LEVEL").Read(1) & "," & HMIRuntime.Tags("trigger_0").Read(1) & ");"
Set objConnection = CreateObject("ADODB.Connection")
objConnection.ConnectionString = strConnectionString
objConnection.Open
Set objCommand = CreateObject("ADODB.Command")
With objCommand
.ActiveConnection = objConnection
.CommandText = strSQL
End With
objCommand.Execute
Set objCommand = Nothing
objConnection.Close
Set objConnection = Nothing
End Sub

1 个答案:

答案 0 :(得分:0)

请按照以下步骤操作:

1)如果尚未在表NEWDATA中添加日期和时间列,请添加新的列名(例如dt),其数据类型为datetime

2)然后在vbscript中添加以下行

dim dtat
dtat = now

并如下所示重写插入语句:

strSQL = "INSERT INTO NEWDATA(JOB_ID,SHIFT,QTY,WEIGHT,DECREASING_ST,DECREASING_TEMP_PV,DECREASING_LEVEL,CASCADE_RINSE_T4_ST,CASCADE_RINSE_T4_TEMP_PV,CASCADE_RINSE_T4_LEVEL,CASCADE_RINSE_T3_ST,CASCADE_RINSE_T3_TEMP_PV,CASCADE_RINSE_T3_LEVEL,PHOSPHATE_ST,PHOSPHATE_TEMP_PV,PHOSPHATE_LEVEL,trigger_0,dt) VALUES (" & HMIRuntime.Tags("REPORT_BLOCK_JOB_ID").Read(1) & "," & HMIRuntime.Tags("REPORT_BLOCK_SHIFT").Read(1) & "," & HMIRuntime.Tags("REPORT_BLOCK_QUANTITY").Read(1) & "," & MIRuntime.Tags("REPORT_BLOCK_WEIGHT").Read(1) & "," & HMIRuntime.Tags("REPORT_BLOCK_DECREASING_ST").Read(1) & "," & HMIRuntime.Tags("REPORT_BLOCK_DECREASING_TEMP_PV").Read(1) & "," & HMIRuntime.Tags("REPORT_BLOCK_DECREASING_LEVEL").Read(1) & "," & HMIRuntime.Tags("REPORT_BLOCK_CASCADE_RINSE_T4_ST").Read(1) & "," & HMIRuntime.Tags("REPORT_BLOCK_CASCADE_RINSE_T4_TEMP_PV").Read(1) & "," & HMIRuntime.Tags("REPORT_BLOCK_CASCADE_RINSE_T4_LEVEL").Read(1) & "," & HMIRuntime.Tags("REPORT_BLOCK_CASCADE_RINSE_T3_ST").Read(1) & "," & HMIRuntime.Tags("REPORT_BLOCK_CASCADE_RINSE_T3_TEMP_PV").Read(1) & "," & HMIRuntime.Tags("REPORT_BLOCK_CASCADE_RINSE_T3_LEVEL").Read(1) & "," & HMIRuntime.Tags("REPORT_BLOCK_PHOSPHATE_ST").Read(1) & "," & HMIRuntime.Tags("REPORT_BLOCK_PHOSPHATE_TEMP_PV").Read(1) & "," & HMIRuntime.Tags("REPORT_BLOCK_PHOSPHATE_LEVEL").Read(1) & "," & HMIRuntime.Tags("trigger_0").Read(1) & ",convert(datetime,'"&dtat&"',103));"

希望有帮助!