我按照WinCC帮助中的示例访问归档数据时,出现错误,错误提示:“多步OLE DB操作产生错误。如果可能,请检查每个OLE DB状态值”。我在网上搜索了一下,对于VB,这样的错误一般都是数据库的字段类型不匹配或者字符个数超出了数据库的字段的最大限制个数。这种解释不适合于我使用的代码。
代码如下,请各位高手指点:
Const NMAX = 100
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, nRec
Dim strDateTime As String
Dim iMS As Long
'1.1 Make connection string for ADODB
sPro = "Provider=WinCCOLEDBProvider.1;"
sDsn = "Catalog=CC_ExcelFro_08_03_30_18_25_08R;"
sSer = "Data Source=.\WinCC;Integrated Security=SSPI"
'Persist Security Info=False;
sCon = sPro + sDsn + sSer
' 1.2 Define command text in sSql (relative time)
sSql = "TAG:R,'TagTestArchive\TagTest','0000-00-00 00:10:00.000','0000-00-00 00:00:00.000'"
' 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
nRec = NMAX
' 3.0 Fill standard listview object with recordset
ListView1.ColumnHeaders.Clear
ListView1.ColumnHeaders.Add , , CStr(oRs.Fields(1).Name), 2500 ' DateTime
ListView1.ColumnHeaders.Add , , "MS", 1500 ' Milisecond
ListView1.ColumnHeaders.Add , , CStr(oRs.Fields(0).Name), 1500 ' VarID
ListView1.ColumnHeaders.Add , , CStr(oRs.Fields(2).Name), 1500 ' RealValue
If (m > 0) Then
oRs.MoveFirst
n = 0
Do While (Not oRs.EOF And n < nRec)
n = n + 1
SplitDateTimeAndMs oRs.Fields(1).Value, strDateTime, iMS 'Split Milisecond from DateTime
s = FormatDateTime(strDateTime, 2) & " " & FormatDateTime(strDateTime, 3)
Set oItem = ListView1.ListItems.Add()
oItem.Text = s
oItem.SubItems(1) = iMS
oItem.SubItems(2) = oRs.Fields(0).Value
oItem.SubItems(3) = FormatNumber(oRs.Fields(2).Value, 4)
oRs.MoveNext
Loop
oRs.Close
Else
End If
Set oRs = Nothing
conn.Close
Set conn = Nothing