请 大神们帮我看看把wincc变量的值通过按钮的脚本事件存入SQL数据库,从SQL数据库取出数据并显示在wincc控件中的代码,先谢过各位
写数据到数据库的代码如下:
Sub OnClick(ByVal Item)
Dim objConnection
Dim strConnectionString
Dim onjRecordset
Dim objCommand
Dim lngValue
Dim strSQL
strconnectionString="Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=" & yejian.Value &";Data Source=.\WINCC"
lngValue=HMIRuntime.Tags("Tag1").Read
Set objConnection=CreateObject("ADODB.Connection")
objConnection.ConnectionString=strConnectionString
objConnection.CursorLocation=3
objConnection.Open
strSQL="INSERT INTO WINCC_DATA(TagValue) VALUES(" & lngValue &");"
Set objRecordset=CreateObject("ADODB.Recordset")
Set objCommand=CreateObject("ADODB.Command")
objCommand.CommandType=1
Set objCommand.ActiveConnection=objConnection
objCommand.CommandText=strSQL
Set objRecordset=Nothing
objConnection.Close
Set objConnection=Nothing
End Sub
读数据到wincc控件的代码:
Sub OnClick(ByVal Item)
Dim objConnection
Dim objCommand
Dim objRecordset
Dim strConnectionString
Dim strSQL
Dim lngValue
strConnectionString = "Provider=SQLOLEDE.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=" & yejian.Value & ";Data Source=.\WINCC"
Set objConnection = CreateObject("ADODB.Connection")
objConnection.ConnectionString = strConnectionString
objConnection.CursorLocation=3
objConnection.Open
strSQL = "select TagValue from WINCC_DATA where ID = 1"
Set objRecordset = CreateObject("ADODB.Recordset")
Set objCommand = CreateObject("ADODB.Command")
objCommand.ActiveConnection = objConnection
objCommand.CommandType=1
objCommand.CommandText = strSQL
Set objRecordset = objCommand.Execute
lngValue = objRecordset.Fields(0).Value
HMIRuntime.Tags("dbValue").Write lngValue
Set objCommand = Nothing
objConnection.Close
Set objRecordset = Nothing
Set objConnection = Nothing
End Sub