VB连接WINCC数据库,运行提示无效的类别字符串,以下是代码,大家帮忙看看是哪里的问题:
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
''#为ADODB创建connection string
sPro = "Provider = WinCCOLEDBProvider.1;"
sDsn = "Catalog = CC_test_08_01_20_22_27_54R;"
sSer = "Data Source = .\WinCC"
sCon = sPro + sDsn + sSer
''#在sSql定义命令文本(相对时间)
sSql = "TAG:R,''PVArchive1\Tag1'',''0000-00-00 00:00:00.000'',''0000-00-00 00:00:00.000''"
''sSql = "TAG:R,1,''0000-00-00 00:00:00.000'',''0000-00-01 00:00:00.000''"
MsgBox "Open with:" & vbCr & sCon & vbCr & sSq1 & vbCr
''#建立连接
Set conn = CreateObject("ADODB.Connection")
conn.ConnectionString = sCon
conn.CursorLocation = 3
conn.Open
''#使用命令文本进行查询
Set oRs = CreateObject("ADODB.RecordSet")
Set oCom = CreateObject("ADODB.Command")
oCom.CommandType = 1
Set oCom.ActiveConnection = conn
oCom.CommandText = sSql
''#填充记录集
Set oRs = oCom.execute
m = oRs.Fields.Count
''#用记录集填充标准listview对象
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
问题补充:
============Provider============
Application : PDLRT
Picture : NewPdl0.pdl_Events
Function : Sub 按钮1
Line : 1
Error : 无效的类别字符串
============Provider============
谢谢城外之人,谢谢大家,代码中是单引号,但不知道为什么贴上来就变成双引号了,连注释部分都自动变化了
最佳答案
你这段代码应该是WINCC的一个按钮事件的VBS脚本吧。是看到你的问题补充得出的结论,这段代码是纯VB代码,而WINCC的vb是VBS,只是VBA的一个子集,他不支持Dim sPro As String这样的定义,应该写成这样Dim sPro
还有:
sDsn = "Catalog = CC_test_08_01_20_22_27_54R;"
这句话要和你的wincc的RT数据库一致
如果我写的话就不用OLEDB了直接这么写sDsn="PROVIDER=MSDASQL;dsn=CC_test_08_01_20_22_27_54R;uid=;pwd=;"
然后你只是处理了读数据的操作,直接用Connection的EXECUTE方法就可以了,不用再打开Command和RecordSet对象了,这样对系统的开销要小一点。
提问者对于答案的评价:
谢谢!!!
原创文章,作者:more0621,如若转载,请注明出处:https://www.zhaoplc.com/plc278089.html