主题:WinCC V6.0用VBS连接数据库的几点说明
1、运行数据库DSN
WinCC内部变量:@DatasourceNameRT,如:CC_MyTest_06_04_08_09_05_14R
2、普通连接的ConnectionString
(1)连接运行数据库
strSQL="Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=CC_MyTest_06_04_08_09_05_14R;Data Source=.\WINCC"
(2)连接其它数据库,如:Northwind
strSQL="Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=Northwind;Data Source=.\WINCC"
注:普通连接方式下,所有标准的SQL语句都可以使用。
3、归档数据库的ConnectionString
strSQL="Provider=WinCCOLEDBProvider.1;Catalog=CC_MyTest_06_04_08_09_05_14R;Data Source=.\WinCC"
在此连接方式下,只能用WinCCOLEDBProvider提供的方法访问归档数据库:
(1)单个归档变量查询:
strSQL = "TAG:R,10,'2006-04-12 16:00:00','2006-04-13 15:59:59'"
或
strSQL = "TAG:R,'Archive1\Tag1','2006-04-12 16:00:00','2006-04-13 15:59:59'"
(2)多个归档变量查询:
strSQL = "TAG:R,(10;14;15),'2006-04-12 16:00:00','2006-04-13 15:59:59'"
或
strSQL = "TAG:R,('Archive1\Tag1';'Archive2\Tag1';'Archive2\Tag2'),'2006-04-12 16:00:00','2006-04-13 15:59:59''"
注:如果需要对查询结果再作过滤,可以使用Recordset.Filter属性再次过滤。
(3)报警信息的查询:
strSQL = "ALARMVIEW:Select * From AlgViewEnu Where DateTime>='2006-04-12 16:00:00' And DateTime<='2006-04-13 15:59:59' And MsgNR=11"
注:Where语句可以根据自己的需要填写。
'******************
由于我的系统无法实现多变量归档查询,我只能使用单变量归档查询到数据后一次性放到listview控件中,在完成这个模块期间很感谢城外之人的帮助,作为对论坛的回报,我把这段代码贴出来,希望可以对有我同样困惑的同志起到帮助。
Dim cn , SqlCon, search_sql ,citems,row,listview,rscount,rCom(768)
Dim rs1,rs2,rs3
Dim search_sql1,search_sql2,search_sql3
Dim flag
Dim tagvalue,lbox1,lbox2
Set lbox1=ScreenItems("listbox1")
Set lbox2=ScreenItems("listbox2")
Dim pbar
Set pbar=ScreenItems("pbar")
Dim wendu,weishui,yali
wendu="\wendu"
weishui="\weishui"
yali="\yali"
Set cn=CreateObject("adodb.connection")
'Set rs=CreateObject("adodb.recordset")
Dim starData,starTime, endData,endTime
Dim dp1,dp2,dp3,dp4
Dim dpvalue1,dpvalue2,dpvalue3,dpvalue4
Set dp1=ScreenItems("dtpicker1")
dpvalue1=Left(dp1.Value,9)
Set dp2=ScreenItems("dtpicker2")
dpvalue2=Right(dp2.Value,8)
Set dp3=ScreenItems("dtpicker3")
dpvalue3=Left(dp3.Value ,9)
Set dp4=ScreenItems("dtpicker4")
dpvalue4=Right(dp4.Value,8)
Dim startdt,enddt
startdt=dpvalue1&" "&dpvalue2' qi shi shi jian jian zi fu chuan
enddt=dpvalue3&" "&dpvalue4 'jie shu shi jian zi fu chuan
'MsgBox "the startdata is : "&dpvalue1
'MsgBox "the enddata is:"&dpvalue3
SqlCon="Provider=WinCCOLEDBProvider;Catalog=CC_winccsingle-proj_06_07_23_09_19_11R;Data Source=.\WinCC"
cn.connectionstring=SqlCon
cn.Cursorlocation=3
cn.open
Set listview = ScreenItems("ListView1")
' xiang listview jia biao tou
Dim u
For u=1 To lbox2.listcount-1
If u=1 Then
With listview
.columnheaders.add,,"Date",90
End With
End If
With listview
.ColumnHeaders.add,,lbox2.list(u)&wendu,110
.ColumnHeaders.add,,lbox2.list(u)&weishui,110
.ColumnHeaders.add,,lbox2.list(u)&yali,110
End With
Next
Dim i,str,value
Dim rs(768),tag(768),query(768)
Dim count
Dim p
p=1
' kai shi jin ru xuan huan du qu shu ju ku
pbar.max=lbox2.listcount*3-3
For i=1 To lbox2.listcount-1
' du qu listbox de lie biao xiang
str=Right(lbox2.list(i),3)
If Left(str,1)="c" Then
value=Right(lbox2.list(i),1)
Else If Left(str,1)="e" Then
value=Right (lbox2.list(i),2)
Else
value=Right (lbox2.list(i),3)
End If
End If
'sheng cheng shu ju ji he
tag(p)="'Node"&value&"\Node"&value&"_temperature'"
tag(p+1)="'Node"&value&"\Node"&value&"_moisture'"
tag(p+2)="'Node"&value&"\Node"&value&"_pressure'"
query(p)="TAG:R,"&tag(p)&","&startdt&","&enddt&""
query(p+1)="TAG:R,"&tag(p+1)&","&startdt&","&enddt&""
query(p+2)="TAG:R,"&tag(p+2)&","&startdt&","&enddt&""
Set rCom(p)=CreateObject("ADODB.Command")
rCom(p).CommandType=1
Set rCom(p).ActiveConnection=cn
rCom(p).CommandText=query(p)
Set rs(p)=rCom(p).execute
'MsgBox "the recordcount is "&rs(p).recordcount
pbar.value=p
Set rCom(p+1)=CreateObject("ADODB.Command")
rCom(p+1).CommandType=1
Set rCom(p+1).ActiveConnection=cn
rCom(p+1).CommandText=query(p+1)
Set rs(p+1)=rCom(p+1).execute
pbar.Value=p+1
Set rCom(p+2)=CreateObject("ADODB.Command")
rCom(p+2).CommandType=1
Set rCom(p+2).ActiveConnection=cn
'rCom(p)="rCom"&p
rCom(p+2).CommandText=query(p+2)
Set rs(p+2)=rCom(p+2).execute
pbar.Value=p+2
count=count+1
p=p+3
Next
'MsgBox "the p is "&P
Dim ecount,t
ecount=lbox2.listcount*3' zan shi mei you shen m yong
MsgBox "Ready to fillful the the listview"
pbar.Value=0
MsgBox "the recordcount is "&rs(1).recordcount
If rs(1).recordcount=0 Then
MsgBox "no data find!!!!"
Else
pbar.Max=rs(1).recordcount
'MsgBox "the pbar.max is "&pbar.Max
End If
Dim xx 'gei progress bar yong kong zhi jing du tiao jing du
Do While Not rs(1).eof
Set citems=listview.listitems.add()
citems.Text=CStr(rs(1).fields(1).value)
pbar.Value=xx
For t=1 To 12' yi gong cai zu tai le 4 ge xia wei bianl iiang
citems.subitems(t)=CStr(rs(t).fields(2).value)
rs(t).movenext
next
xx=xx+1
Loop
'MsgBox "the xx is "&xx
xx=HMIRuntime.Tags("listview_count").Read
'MsgBox "the listview's row is "&listview.rows
Dim close
Set cn=Nothing
For close=1 To 256
Set rs(close)=Nothing
next
'********************
,那个文件是uaedit.loc
'***************
__object *Conn =NULL;
__object *rs =NULL;
Conn =__object_Create("ADODB.Connection");
rs =__object_Create("ADODB.Recordset");
Conn->Open("连接字符串");
rs->open(sql,Conn,1,1);
printf("\r\n%s",rs->Field("FieldName"));
rs->Close();
Conn->Close();
__object_delete(rs);
__object_delete(Conn);
'**********************
Dim objWshShell
Set objWshShell = CreateObject("Ws cript.Shell")
objWshShell.Run "CALC.EXE", 1
'***************************
'控制1,0之间切换
Dim wtag0
Set wtag0=HMIRuntime.Tags("bin_1")
wtag0.Read
wtag0.Value = Not CBool(wtag0.Value)
wtag0.Write
'***************
'第一种方案 ,打开画面
'Dim objscrWindows
'Set objscrWindows=ScreenItems ("画面窗口1")
' objscrWindows.ScreenName ="NewPdl0"
'第二种方案
ScreenItems ("画面窗口1").PictureName="NewPdl0"
'设置对象的属性
ScreenItems ("text1").backcolor=RGB(255,0,0)
ScreenItems ("text1").Text="爱情"
ScreenItems ("text1").Visible=not _
CBool(ScreenItems ("text1").Visible)
ScreenItems( "输入输出域1" ).BackColor=RGB(0,255,0)
ScreenItems( "按钮4" ).BackColOr=RGB(0,255,0)
'设置对象的属性,如字体颜色
Dim txtT0
Set txtt0=ScreenItems("text2")
txtt0.ForeColor =RGB(0,255,0)
'***************
2:
Dim tag1
Dim tag0
Dim tag2
Set tag0=HMIRuntime.Tags("stat_byte_6")
tag0.Write 10
Set tag1=HMIRuntime.Tags("stat_byte_7")
tag1.Read
If tag1.Value >=210 Then
tag1.Value =0
End If
tag1.Value =tag1.Value +10
tag1.Write
Set tag2=HMIRuntime.Tags("stat_byte_5")
tag2.Value =tag1.Value
tag2.Write
' Dim vtag0
'Set vtag0=HMIRuntime.Visible("Visible")
'*********************
Dim rtag0
Dim rtag1
Set rtag0=HMIRuntime.Tags("stat_byte_4")
HMIRuntime.Trace"Value:" & rtag0.Read & vbCrLf
Set rtag1=HMIRuntime.Tags ("stat_byte_3")
'rtag1.Read
rtag1.Value =rtag0.Value
rtag1.Write 1
Dim arry(7)
Dim i
Dim j
For i=1 To 7
arry(i)=HMIRuntime.Tags("cheng1_3" & CStr(i)).Read
Next
For i = 1 To 7
For j=1 To 7
If i<>j Then
If arry(i)=arry(j) Then
MsgBox CStr(i) & " 号秤与 " & CStr(J) & " 号秤启动顺序重复,请重新设置!",48,"错误!"
Exit Sub
End If
End If
Next
Next
For i=1 To 7
HMIRuntime.Tags("cheng1_" & CStr(i+20)).write arry(i),1
Next
HWND hwnd=NULL;
int iRet;
hwnd=FindWindow(NULL,"WinCC-运行系统 - ");
iRet=MessageBox(hwnd,"是否退出系统?","对话框",MB_YESNOMB_ICONQUESTIONMB_SYSTEMMODAL);
printf("\r\n对话框例子\r\n");
if (iRet==IDYES)
{
printf("你选择YESbutton\r\n");
}
else
{
printf("你选择NObutton\r\n");
}
这句:hwnd=FindWindow(NULL,"WinCC-运行系统 - ");用于中文版的!
如果是英文版就用下面这句:
hwnd=FindWindow(NULL,","WinCC-Runtime - ");