哪位知道用VBS代码去*作数据库SQL2005吗?例如在wincc画面中的按钮中编VBS代码去*作SQL2005,例如实现新建、修改、复制、删除数据库的功能。先谢谢了!
最佳答案
我发个连接数据库,插入数据库的例子吧,下面是根据一个变量的值的变化,触发动作插入数据库(SQL2005)。仅供参考!
Option Explicit
Function action
Dim objConnection
Dim strConnectionString
Dim strSQL
Dim objCommand
Dim objRecordSet
Dim RecordCount
Dim DeviceName
On Error Resume Next
Dim data(5)
Dim i
For i = 0 To 4
data(i) = "Null"
Next
DeviceName=CStr("1#设备")
Data(1)=HMIRuntime.tags("Ractor/Motor1.AUT_ON_OP").read
Data(2)=HMIRuntime.tags("Ractor/Motor1.QRUN").read
If Data(1)="1" Then
Data(1)="自动"
End If
If Data(1)="0" Then
Data(1)="手动"
End If
If Data(2)="1" Then
Data(2)="运行"
'MsgBox Data(2)
End If
If Data(2)="0" Then
Data(2)="停止"
'MsgBox Data(2)
End If
'MsgBox Date()&" "&Time()
'MsgBox Data(1)
'MsgBox Data(2)
''''Continute to insert data to database
data(0) ="Insert into Device_record(DeviceName,DeviceTime,DeviceStatus,DeviceMode)values('"& DeviceName &"','"& Date() &" "& Time() &"','"& data(2) &"','"& data(1) &"')"
'MsgBox data(0)
strConnectionString = "Provider=MSDASQL;DSN=ZJG_DevDSN;UID=sa;PWD=;"
Set objConnection = CreateObject("ADODB.Connection")
objConnection.ConnectionString = strConnectionString
objConnection.Open
Set objCommand = CreateObject("ADODB.Command")
With objCommand
.ActiveConnection = objConnection
.CommandText = data(0)
'debug.print objConnection.execute(data(0))
End With
'MsgBox"ok"
objCommand.Execute
Set objCommand = Nothing
objConnection.Close
Set objConnection = Nothing
For i = 0 To 4
data(i) = "Null"
Next
End Function
提问者对于答案的评价:
虽然答案不是我完全要的,但还是受了不少启发,谢谢了
原创文章,作者:more0621,如若转载,请注明出处:https://www.zhaoplc.com/plc271558.html