用wincc怎么实现连接SQL服务器的记录,将数据写入SQL,并在报表中输出SQL相应的数据?最好是提供VC和VB的两种解决方案~~
最佳答案
1 wincc怎么实现连接SQL服务器的记录, 是要WinCC 的归档数据,还是自己用脚本归档,若是自己的脚本归档,是用sql ole db,
若是使用WinCC 的归档数据, 使用provider ole ,db .
a 使用sql ole db, 读用户归档
Sub OnClick(ByVal Item)
Dim sDsn
Dim sSer
Dim sCon
Dim sSql
Dim conn
Dim oRs
Dim oCom
Dim sPro
Dim m,n,s
Dim a,b,c
Dim Listview1
Dim oItem
Dim xlsApp
Dim strDateTime
Dim iMS
Set Listview1 = ScreenItems("Control1")
sCon ="Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=CC_database_08_10_20_14_59_38R;Data Source=YSY\WINCC"
sSql = "Select * from UA#myua"
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
m = oRs.Fields.Count
Set xlsApp=CreateObject("Excel.Application")
xlsApp.Visible=True
xlsApp.Workbooks.Open"E:\sample.xls"
If (m > 0) Then
oRs.MoveFirst
n = 1
xlsApp.Cells(1,1).Value=oRs.Fields(0).Name
xlsApp.Cells(1,2).Value=oRs.Fields(1).Name
xlsApp.Cells(1,3).Value=oRs.Fields(2).Name
Do While Not oRs.EOF
n = n + 1
xlsApp.Cells(n,1).Value=oRs.Fields(0).Value
xlsApp.Cells(n,2).Value=oRs.Fields(1).Value
xlsApp.Cells(n,3).Value=oRs.Fields(2).Value
oRs.MoveNext
Loop
xlsApp.ActiveWorkBook.Save
xlsApp.Workbooks.Close
xlsApp.Quit
Set xlsApp=Nothing
End If
oRs.Close
Set oRs = Nothing
conn.Close
Set conn = Nothing
End Sub
b,使用provider ole ,db 读归档数据
Sub OnClick(ByVal Item)
Dim xlsApp
Dim sDsn
Dim sSer
Dim sCon
Dim sSql
Dim conn
Dim oRs
Dim oCom
Dim sPro
Dim m,n,s
Dim a,b,c
sPro = "Provider=WinCCOLEDBProvider.1;"
sDsn = "Catalog=CC_vbs_08_05_26_14_56_45R;" //该为自己的数据库名称
sSer = "Data Source=.\WinCC"
sCon = sPro + sDsn + sSer
// 该为自己的归档名称和变量名称
sSql = "Tag:R,('ProcessValueArchive\tag1';'ProcessValueArchive\tag2'),'2008-07-2 00:32:00.000','2008-07-2 17:34:00.000'"
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
m = oRs.Fields.Count
Set xlsApp=CreateObject("Excel.Application")
xlsApp.Visible=True
xlsApp.Workbooks.Open"E:\sample.xls"
xlsApp.Cells(10,2).Value=6
If (m > 0) Then
oRs.MoveFirst
n = 1
xlsApp.Cells(1,1).Value=oRs.Fields(0).Name
xlsApp.Cells(1,2).Value=oRs.Fields(1).Name
xlsApp.Cells(1,3).Value=oRs.Fields(2).Name
xlsApp.Cells(1,4).Value=oRs.Fields(3).Name
xlsApp.Cells(1,5).Value=oRs.Fields(4).Name
Do While Not oRs.EOF
n = n + 1
xlsApp.Cells(n,1).Value=oRs.Fields(0).Value
xlsApp.Cells(n,2).Value=oRs.Fields(1).Value
xlsApp.Cells(n,3).Value=FormatNumber(oRs.Fields(2).Value,2)
xlsApp.Cells(n,4).Value=Hex(oRs.Fields(3).Value)
xlsApp.Cells(n,5).Value=Hex(oRs.Fields(4).Value)
oRs.MoveNext
Loop
xlsApp.ActiveWorkBook.Save
xlsApp.Workbooks.Close
xlsApp.Quit
Set xlsApp=Nothing
End If
oRs.Close
Set oRs = Nothing
conn.Close
Set conn = Nothing
End Sub
提问者对于答案的评价:
可以
原创文章,作者:more0621,如若转载,请注明出处:https://www.zhaoplc.com/plc129902.html