我在WinCC中写了段程序,代码如下:
Sub OnClick(Byval Item)
Dim strcn
Dim cn
Dim is_SQL
Dim com
Dim com_SQL
Dim rds
Set cn = CreateObject ("ADODB.Connection")
strcn = "Provider = WINCCOLEDBProvider.1; Catalog = CC_TEST_08_05_21_15_03_52R; Data Source = .\WINCC"
cn.ConnectionString = strcn
cn.open
'com_SQL = "TAG:R,'ProcessValueArchive\AnalogTag','2008-07-08 01:00:00.000','0000-00-00 00:00:00.000'"
com_SQL = "TAG:R,1,'0000-00-00 01:00:00.000','0000-00-00 00:00:00.000'"
Set rds = CreateObject ("ADODB.Recordset") Set com = CreateObject ("ADODB.Command")
com.ActiveConnection = strcn
com.CommandText = com_SQL
Set rds = com.Execute
Dim ListView1
Dim Litem
Set ListView1 = ScreenItems ("ListView1")
ListView1.ListItems.Clear ()
ListView1.ColumnHeaders.Clear ()
ListView1.GridLines = True
ListView1.ColumnHeaders.Add , , CStr(rds.Fields(0).Name), 50 ListView1.ColumnHeaders.Add , , CStr(rds.Fields(1).Name), 50
ListView1.ColumnHeaders.Add , , CStr(rds.Fields(2).Name), 50
ListView1.ColumnHeaders.Add , , CStr(rds.Fields(3).Name), 50
ListView1.ColumnHeaders.Add , , CStr(rds.Fields(4).Name), 50
Set Litem = ListView1.ListItems.Add () 'object.Add(index, key, text, icon, smallIcon)
Litem.Text = CStr(rds.EOF)
Litem.SubItems (1) = CStr(rds.Fields(1).Value)
End Sub
运行后.EOF显示True,死活就是读不出那个过程值来,各位看看问题出在哪里?我一直没有找到问题。谢谢了!!
问题补充:
我能返回Name,但是返回不了Value。问题是,如果这个表存在,但是里面没有值的话,也是可以返回Name的。但是我又看不到那个表里面的实际内容,所以只能通过EOF来判断到底有没有数据被读上来。
最佳答案
你是不是没有设置数据库打开时的游标。导致指针到了最后一条记录上了。
参考一下手册上提供的例子
Example Script
Dim sPro As String
Dim sDsn As String
Dim sSer As String
Dim sCon As String
Dim sSql As String
Dim conn As Object
Dim oRs As Object
Dim oCom As Object
Dim oItem As ListItem
Dim m, n, s
'1.1 Make connection string for ADODB
sPro = "Provider=WinCCOLEDBProvider.1;"
sDsn = "Catalog=CC_OpenArch_03_05_27_14_11_46R;"
sSer = "Data Source=.\WinCC"
sCon = sPro + sDsn + sSer
' 1.2 Define command text in sSql (relative time)
sSql = "TAG:R,'PVArchive\Tag1','0000-00-00 00:10:00.000','0000-00-00
00:00:00.000'"
'sSql = "TAG:R,1,'0000-00-00 00:10:00.000','0000-00-00 00:00:00.000'"
MsgBox "Open with:" & vbCr & sCon & vbCr & sSql & vbCr
' 2.1 Make connection
Set conn = CreateObject("ADODB.Connection")
conn.ConnectionString = sCon
conn.CursorLocation = 3
conn.Open
' 2.2 Use command text for query
Set oRs = CreateObject("ADODB.Recordset")
Set oCom = CreateObject("ADODB.Command")
oCom.CommandType = 1
Set oCom.ActiveConnection = conn
oCom.CommandText = sSql
' 2.3 Fill the recordset
Set oRs = oCom.Execute
m = oRs.Fields.Count
' 3.0 Fill standard listview object with recordset
ListView1.ColumnHeaders.Clear
ListView1.ColumnHeaders.Add , , CStr(oRs.Fields(1).Name), 140
ListView1.ColumnHeaders.Add , , CStr(oRs.Fields(2).Name), 70
ListView1.ColumnHeaders.Add , , CStr(oRs.Fields(3).Name), 70
If (m > 0) Then
oRs.MoveFirst
n = 0
Do While Not oRs.EOF
n = n + 1
s = Left(CStr(oRs.Fields(1).Value), 23)
Set oItem = ListView1.ListItems.Add()
oItem.Text = Left(CStr(oRs.Fields(1).Value), 23)
oItem.SubItems(1) = FormatNumber(oRs.Fields(2).Value, 4)
oItem.SubItems(2) = Hex(oRs.Fields(3).Value)
If (n > 1000) Then Exit Do
oRs.MoveNext
Loop
oRs.Close
Else
End If
Set oRs = Nothing
conn.Close
Set conn = Nothing
提问者对于答案的评价:
说的不错,就是没有设置游标属性,谢谢回答!
原创文章,作者:more0621,如若转载,请注明出处:https://www.zhaoplc.com/plc276796.html