我们都知道, wincc的数据库中, 想获取数据库的用户名, 必须要知道数据库的名称, 譬如CC_XXX_XXX_23_09_27_08_22_13R 类似, 那么接下来,根据这个思路, 我们就可以通过vbs脚本连接到wincc自身的数据库
首先在wincc界面中创建一个按钮以及拖出一个组合框
把组合框更名为:cmoUserList
右击按钮,并选择VBS动作
写入以下代码
Dim fullname,dataBaseName,conn,sqlconnectionString,sql,oRs,userCount,i, cmb
fullname = HMIRuntime.Tags("@DatasourceNameRT").Read
dataBaseName = Mid(fullname, 1, Len(fullname) - 1)
sqlconnectionString = "Provider=SQLOLEDB;Data Source=.\WINCC;Initial Catalog=" & dataBaseName & ";Integrated Security=SSPI;"
Set conn = CreateObject("ADODB.Connection")
conn.ConnectionString = sqlconnectionString
conn.CursorLocation = 3
conn.Open
MsgBox "数据库连接成功"
sql = "select name from PW_USER where GRPID > 0"
Set oRs = CreateObject("ADODB.Recordset")
oRs.Open sql, conn, 1, 3
userCount = oRs.RecordCount
Set cmb = ScreenItems("cmoUserList")
cmb.NumberLines = userCount
If userCount > 0 Then
For i = 1 To userCount
cmb.SelIndex = i
cmb.SelText = Trim(oRs.Fields("name").Value)
oRs.MoveNext
Next
End If
cmb.SelIndex = 1
oRs.Close
conn.Close
Set conn = Nothing
Set oRs = Nothing
Set cmb = Nothing
准备工作已经完成, 点击运行,再点击按钮
这时就可以看到,数据全部被抓取出来了