请问哪位大侠有wincc对SQLserver数据库操作的 已做好例子,请发我邮箱谢谢wzw2008_80@126.com
最佳答案
给你一段代码供你参考,代码参考自深入浅出西门子wincc和技术论坛,简单修改就可i在wincc里用,vb测试通过.
Dim sPro
Dim sDsn
Dim sSer
Dim sCon
Dim sSql
Dim oRs
Dim conn
Dim oCom
Dim oItem
Dim m, n, s
Dim hourdate
Dim secdate
Dim k
''定义变量,VBS使用变体类型,无需指定数据类型,否则编译出错
''Set listview1 = ScreenItems("listview1") ''使用Listview控件
ListView1.View = 3
ListView1.ListItems.Clear
''//定义控件属性
sPro = "Provider=WinCCOLEDBProvider.1;" ''//定义服务提供者
sDsn = "Catalog=CC_lyf_07_12_30_09_17_21R;"
sSer = "Data Source=.\WinCC"
sCon = sPro + sDsn + sSer
''//定义连接字符串
sSql = "TAG:R,''ProcessvalueArchive\NewTag'',''2008-1-9 5:19:20.000'',''2008-1-9 5:22:00.999''"
''//访问压缩归档的SQL语句,非压缩归档使用SELECT语句,用法见SQL
''MsgBox "Open with:" & vbCr & sCon & vbCr & sSql & 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
''//标准的ADO访问,下面是输出到Listview的代码
m = oRs.RecordCount
Text1.Text = m
Text2.Text = oRs.Fields(3).ActualSize & "-" & oRs.Fields(3)
With ListView1
.ColumnHeaders.Clear
.ColumnHeaders.Add , , CStr(oRs.Fields(1).Name), 780
.ColumnHeaders.Add , , CStr(oRs.Fields(2).Name), 900
.ColumnHeaders.Add , , CStr(oRs.Fields(3).Name), 1050
.ColumnHeaders.Add , , CStr(oRs.Fields(4).Name), 1200
End With
If (m > 0) Then
oRs.MoveFirst
n = 0
Do While Not oRs.EOF
n = n + 1
''s = Left(CStr(oRs.Fields(1).value), 23)
s = CDate(oRs.Fields(1).Value)
k = DateAdd("h", 8, s)
Set oItem = ListView1.ListItems.Add()
oItem.Text = k
oItem.SubItems(1) = FormatNumber(oRs.Fields(2).Value, 1)
oItem.SubItems(2) = Hex(oRs.Fields(3).Value)
oItem.SubItems(3) = FormatNumber(oRs.Fields(4).Value, 1)
oRs.MoveNext
Loop
oRs.Close
Else
End If
Set oRs = Nothing
conn.Close
Set conn = Nothing
提问者对于答案的评价:
厉害
原创文章,作者:more0621,如若转载,请注明出处:https://www.zhaoplc.com/plc277961.html