1. 程式人生 > >PC軟體開發技術之一:在WinCC中通過VBS操作SQL Server2005

PC軟體開發技術之一:在WinCC中通過VBS操作SQL Server2005

 

在專案中需要在一定條件滿足時,儲存一些資料到資料庫中,並可根據條件查詢。考慮到WinCC6.2以後採用的就是SQL Server2005資料庫,所以直接利用該資料庫即可,通過SQL Server Management Studio(SSMS)可以建立自己的資料庫,並按要求建立好表。

一、資料庫連線

在SQL Server Management Studio(SSMS)中建立名為evcp的資料庫,再建立名為evcp的表,然後根據需要建立Columns,在本專案中建立了norder(流水號)、pileno(樁號)、cardno(卡號)、operno(員工號)、energy(電量)、cost(金額)、period(時長)、rate(費率)、pdate(日期)和ptime(時間)。

在本專案中採用ODBC的方式連線資料庫,首先在控制面板中建立好資料來源,配置好SQL Server驅動資料來源,命名為evcs。

二、資料寫入

要求在一個狀態量值為1的時候完成資料庫的儲存,等資料儲存完後將狀態量清0。

1、先在全域性指令碼VBS專案模組中建立函式savedata,程式碼如下:

Sub savedata

Dim objConnection

Dim objCommand

Dim objRecordset

Dim strConnectionString

Dim strSQL

Dim norder,pileno,cardno,operno,energy,cost,period,rate,pdate,ptime

 

norder=HMIRuntime.Tags("norder").Read

pileno= HMIRuntime.Tags("pileno").Read

cardno=HMIRuntime.Tags("cardno").Read

operno= HMIRuntime.Tags("operno").Read

energy= HMIRuntime.Tags("energy").Read

cost= HMIRuntime.Tags("cost").Read

period= HMIRuntime.Tags("period").Read

rate= HMIRuntime.Tags("rate").Read

pdate= HMIRuntime.Tags("pdate").Read

ptime= HMIRuntime.Tags("ptime").Read

 

strConnectionString = "Provider=MSDASQL;DSN=evcs;UID=;PWD=;"

Set objConnection = CreateObject("ADODB.Connection")

objConnection.ConnectionString = strConnectionString

objConnection.Open

 

Set objRecordset = CreateObject("ADODB.Recordset")

Set objCommand = CreateObject("ADODB.Command")

objCommand.ActiveConnection = objConnection

 

strSQL = "insert into evcp (norder,pileno,cardno,operno,energy,cost,period,rate,pdate,ptime) values ("&_

"'"&norder&"',"&_

"'"&pileno&"',"&_

"'"&cardno&"',"&_

"'"&operno&"',"&_

"'"&energy&"',"&_

"'"&cost&"',"&_

"'"&period&"',"&_

"'"&rate&"',"&_

"'"&pdate&"',"&_

"'"&ptime&"')"

'MsgBox (strSQL)

objCommand.CommandText = strSQL

objCommand.Execute

 

Set objCommand = Nothing

objConnection.Close

Set objRecordset = Nothing

Set objConnection = Nothing

End Sub

2、在全域性指令碼VBS動作中建立1秒週期的週期性出發動作,並新增如下程式碼:

Option Explicit

Function action

Dim v1

v1=HMIRuntime.Tags("satuse").Read

 

If v1 Then

Call savedata

HMIRuntime.Tags("satuse").Write 0

End if

End Function

這樣當satuse值為1時系統自動儲存資料

三、資料查詢

資料的查詢要複雜一些,需要用到MSFlexGrid控制元件、MS Form2 ComboBox控制元件和MS Form2 TextBox,這幾個控制元件可以單獨註冊也可以安裝VB6後自動新增。

在查詢頁面上新增開啟頁面執行指令碼如下:

Sub OnOpen()                         

Dim MSFlexGrid1,cb1,tb1,tb2

Set MSFlexGrid1 = ScreenItems("控制元件1")

Set cb1 = ScreenItems("cb1")

Set tb1 = ScreenItems("tb1")

Set tb2 = ScreenItems("tb2")

MSFlexGrid1.Clear

 

MSFlexGrid1.ColWidth(0) = 620

MSFlexGrid1.ColWidth(1) = 1500

MsFlexGrid1.ColWidth(2) = 1500

MSFlexGrid1.ColWidth(3) = 1500

MSFlexGrid1.ColWidth(4) = 1500

MSFlexGrid1.ColWidth(5) = 1500

MsFlexGrid1.ColWidth(6) = 1500

MSFlexGrid1.ColWidth(7) = 1500

MSFlexGrid1.ColWidth(8) = 1600

MSFlexGrid1.ColWidth(9) = 2000

MsFlexGrid1.ColWidth(10) = 2000

 

MSFlexGrid1.TextMatrix(0,0) = "編號"

MSFlexGrid1.TextMatrix(0,1) = "流水號"

MSFlexGrid1.TextMatrix(0,2) = "樁號"

MSFlexGrid1.TextMatrix(0,3) = "卡號"

MSFlexGrid1.TextMatrix(0,4) = "操作員號"

MSFlexGrid1.TextMatrix(0,5) = "電量(度)"

MSFlexGrid1.TextMatrix(0,6) = "金額(元)"

MSFlexGrid1.TextMatrix(0,7) = "時長(分)"

MSFlexGrid1.TextMatrix(0,8) = "費率(元/度)"

MSFlexGrid1.TextMatrix(0,9) = "日期"

MSFlexGrid1.TextMatrix(0,10) = "時間"

MSFlexGrid1.ColAlignment(0) = 4

MSFlexGrid1.ColAlignment(1) = 4

MSFlexGrid1.ColAlignment(2) = 4

MSFlexGrid1.ColAlignment(3) = 4

MSFlexGrid1.ColAlignment(4) = 4

MSFlexGrid1.ColAlignment(5) = 4

MSFlexGrid1.ColAlignment(6) = 4

MSFlexGrid1.ColAlignment(7) = 4

MSFlexGrid1.ColAlignment(8) = 4

MSFlexGrid1.ColAlignment(9) = 4

MSFlexGrid1.ColAlignment(10) = 4

 

Dim i

For i= 1 To 39 Step 1

MSFlexGrid1.TextMatrix(i,0) = i

Next

 

cb1.Text="*"

cb1.AddItem "*"

cb1.AddItem "1"

cb1.AddItem "2"

tb1.Text="*"

tb2.Text="*"

 

End Sub

這段程式碼主要是用來初始化控制元件的顯示。

在查詢按鈕加入相應的程式碼實現三個鍵值(樁號、卡號、操作員號)條件組合的查詢,程式碼如下:

Sub Click(Byval Item)                                                                              

Dim objConnection

Dim objCommand

Dim objRecordset

Dim strConnectionString

Dim strSQL

Dim MSFlexGrid1,cb1,tb1,tb2

Dim i1,i2,cv1,cv2,cv3,cv

Set MSFlexGrid1 = ScreenItems("控制元件1")

Set cb1 = ScreenItems("cb1")

Set tb1 = ScreenItems("tb1")

Set tb2 = ScreenItems("tb2")

 

'清除原有記錄

For i1 = 1 To 39 Step 1

For i2=1 To 10 Step 1

MSFlexGrid1.TextMatrix(i1, i2) =""

Next

Next

 

strConnectionString = "Provider=MSDASQL;DSN=evcs;UID=;PWD=;"

Set objConnection = CreateObject("ADODB.Connection")

objConnection.ConnectionString = strConnectionString

objConnection.Open

Set objRecordset = CreateObject("ADODB.Recordset")

Set objCommand = CreateObject("ADODB.Command")

objCommand.ActiveConnection = objConnection

 

cv1=0

cv2=0

cv3=0

If cb1.Text<>"*" Then

cv1=4

End If

 

If tb1.Text<>"*" Then

cv2=2

End if

 

If tb2.Text<>"*" Then

cv3=1

End If

cv=cv1+cv2+cv3

Select Case cv

Case 7

strSQL = "select * from evcp where pileno like '"&cb1.Text&"'And cardno like '"&tb1.Text&"'And operno like '"&tb2.Text&"'"

Case 6

strSQL = "select * from evcp where pileno like '"&cb1.Text&"'And cardno like '"&tb1.Text&"'"

Case 5

strSQL = "select * from evcp where pileno like '"&cb1.Text&"'And operno like '"&tb2.Text&"'"

Case 4

strSQL = "select * from evcp where pileno like '"&cb1.Text&"'"

Case 3

strSQL = "select * from evcp where cardno like '"&tb1.Text&"'And operno like '"&tb2.Text&"'"

Case 2

strSQL = "select * from evcp where cardno like '"&tb1.Text&"'"

Case 1

strSQL = "select * from evcp where operno like '"&tb2.Text&"'"

Case Else

strSQL = "select * from evcp"

End Select

 

objCommand.CommandText = strSQL

 

Set objRecordset = objCommand.Execute

 

Dim i

i=0

 

If (objRecordset.Bof And objRecordset.Eof) Then

MsgBox("沒有符合要求的記錄")

Else

While Not objRecordset.EOF

i=i+1

MSFlexGrid1.TextMatrix(i, 1) = CStr(objRecordset.Fields(0).Value)

MSFlexGrid1.TextMatrix(i, 2) = CStr(objRecordset.Fields(1).Value)

MSFlexGrid1.TextMatrix(i, 3) = CStr(objRecordset.Fields(2).Value)

MSFlexGrid1.TextMatrix(i, 4) = CStr(objRecordset.Fields(3).Value)

MSFlexGrid1.TextMatrix(i, 5) = CStr(objRecordset.Fields(4).Value)

MSFlexGrid1.TextMatrix(i, 6) = CStr(objRecordset.Fields(5).Value)

MSFlexGrid1.TextMatrix(i, 7) = CStr(objRecordset.Fields(6).Value)

MSFlexGrid1.TextMatrix(i, 8) = CStr(objRecordset.Fields(7).Value)

MSFlexGrid1.TextMatrix(i, 9) = CStr(objRecordset.Fields(8).Value)

MSFlexGrid1.TextMatrix(i, 10) = CStr(objRecordset.Fields(9).Value)

objRecordset.movenext

Wend

End If

 

Set objCommand = Nothing

objConnection.Close

Set objRecordset = Nothing

Set objConnection = Nothing

 

End Sub

歡迎關注: