WinCC与数据库连接
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
WinCC与数据库连接
2009-06-02 16:03
近来做了一个WinCC与数据库连接的程序,功能如下:
1、要求用户输入“实验目的”和“实验日期”,点击“建立表格”,则在数据库中创建了以“实验目的+时间”为名字的表
2、点击“连接数据库”连接上数据库,并开始写入数据
3、点击“断开数据库”则停止记录数据
做法:
通过一个数字量来判断数否记录数据,若该数字量是1则开始记录数据,否则停止。记录数据的时间间隔是1秒
在”建立表格“键入代码:
Dim objConnection
Dim strConnectionString
Dim strSQL
Dim objCommand
Dim objscreen
Dim obj_item
Dim obj_item1
Dim biaoming
Dim shjian
Set shjian = HMIRuntime.Tags("shjian")
Set biaoming = HMIRuntime.Tags("biaoming")
Set objscreen=HMIRuntime.Screens("database")
Set obj_item=objscreen.ScreenItems("IOField1")
Set obj_item1=objscreen.ScreenItems("IOField") 注:不同界面的不同对象的引用方法
If shjian.Read= "" Or biaoming.Read= "" Then
MsgBox "Please enter the table name and date!"
Else
strConnectionString = "Provider=MSDASQL;DSN=t;UID=;PWD=;"
strSQL="create table " & Trim( obj_item.OutputValue ) &
Trim( obj_item1.OutputValue ) & "
(name varchar(64),age varchar(20))" 注:这是两个输入控件的连接方法(建立的表格)
Set objConnection = CreateObject("ADODB.Connection") objConnection.ConnectionString = strConnectionString objConnection.Open
Set objCommand = CreateObject("mand")
With objCommand
.ActiveConnection = objConnection
.CommandText = strSQL
End With
objCommand.Execute
End If
在”连接数据库“键入代码
Dim turn_on
Set turn_on = HMIRuntime.Tags("turn_on")
Dim k
k=MsgBox("Start Database?",33,"alarm")
If k=1 Then
turn_on.Read
turn_on.Write 1
End If
在”断开数据库“键入代码:
Dim turn_on
Set turn_on = HMIRuntime.Tags("turn_on")
Dim k
k=MsgBox("Start Database?",33,"alarm")
If k=1 Then
turn_on.Read
turn_on.Write 0
End If
在ACTION中设置1秒的触发器并键入代码:
Dim objConnection
Dim strConnectionString
Dim name
Dim age
Dim strSQL
Dim objCommand
Dim turn_on
Dim objscreen
Dim obj_item
Dim obj_item1
Set objscreen=HMIRuntime.Screens("database")
Set obj_item=objscreen.ScreenItems("IOField1")
Set obj_item1=objscreen.ScreenItems("IOField")
Set turn_on = HMIRuntime.Tags("turn_on")
name = HMIRuntime.Tags("tank_temp").Read
age=HMIRuntime.Tags("tank_temp").Read
turn_on.Read
If turn_on.Value =1 Then
strConnectionString = "Provider=MSDASQL;DSN=t;UID=;PWD=;" strSQL = "insert into " & Trim( obj_item.OutputValue ) & Trim( obj_item1.OutputValue ) & "
values(" & name & "," & age & ");"
Set objConnection = CreateObject("ADODB.Connection")
objConnection.ConnectionString = strConnectionString
objConnection.Open
Set objCommand = CreateObject("mand")
With objCommand
.ActiveConnection = objConnection
.CommandText = strSQL
End With
objCommand.Execute
Set objCommand = Nothing
objConnection.Close
Set objConnection = Nothing
End If