==============================================================
dim cn
dim rs
dim row
dim col
dim viewdata '继承返回的查询结果 将从这里获取查询的返回内容
dim mdbpath '这个是mdb数据库文件路径
dim SQLcmd '这里是SQL语句
dim STRsql '返回的查询结果存放在这个变量里
set cn=CreateObject("ADODB.Connection")
set rs=CreateObject("ADODB.Recordset")
mdbpath="d:\pass.mdb"
cn.Open "DSN=dsnmdbUid=Pwd="
SQLcmd="select * from Pathlist"
rs.Open SQLcmd,cn,,,adCmdText
if rs.bof=false then '如果查询结果有返回的则
viewdata=rs.GetRows
STRsql=""
for row=0 to UBound(viewdata,2) '获取二维数组的长度
for col=0 to UBound(viewdata,1)
strsql=strsql &viewdata(col,row) &" "
next
strsql=strsql &vbcrlf
next
end if
rs.close
cn.close
msgbox strsql
如何使用VBScript访问ORACLE数据库并查询一张表不知道你用的什么数据库,权且当作SQL Server
private void showdata()
{
try
{
string selectsql = @"select * from [user]"//select语句,修改下,应该不难吧
SqlCommand cmd = new SqlCommand(selectsql, conn)//conn就是创建的SqlConnection实例,你如果用到数据库,应该有创建
cmd.CommandType = CommandType.Text
string info = String.Empty//表结果
SqlDataReader odr = cmd.ExecuteReader()
while(odr.Read())
{
info = odr[0].ToString() + " " + odr[1].ToString() + "\n"
//具体每行有几列数据,就添加到n-1,0是第一列,你应该也能修改,可以在个数据之间加个空格,以及行末加个回车,自己改。。。
}
infoTxt.Text = info//将info显示到你说的文本框中,infoTxt为文本框的名称,你应该也可以看懂
}
catch (Exception ex)
{
Console.WriteLine(ex.Message)
}
}
//将函数体部分放入到你的按钮click事件中就可以了
读取变量归档的查询语句是:TAG:R, <ValueID or ValueName>,<TimeBegin>,<TimeEnd>[,<SQL_clause>][,<TimeStep>]
其中TimeStep指的是时间间隔
Sub OnLButtonDown(ByVal Item, ByVal Flags, ByVal x, ByVal y)
Dim sPro
Dim sDsn
Dim sSer
Dim sCon
Dim sSql
Dim conn
Dim oRs
Dim oCom
Dim oItem
'Dim m,n,s
'connection string
sPro = "Provider=WinCCOLEDBProvider.1"
sDsn = "Catalog=CC_XXXXX_12_03_10_08_18_49R" '数据库名
sSer = "Data Source=.\WinCC"
sCon = sPro + sDsn + sSer
'sCon="Provider=WinCCOLEDBProvider.1Integrated Security=SSPIPersist Security Info=FalseInitial Catalog=SSEJRecordData Source=.\WINCC"
'MsgBox "set sCon"
'sSQL为查询命令
sSql = "TAG:R,'ProcessValueArchive\AnalogTag','0000-00-00 00:00:20.000','0000-00-00 00:00:00.000'"
'sSql = "ALARMVIEW:SELECT * from AlgViewChs"
'MsgBox "Set SQL"
'建立连接
Set conn = CreateObject("ADODB.Connection")
conn.ConnectionString = sCon
conn.CursorLocation = 3
conn.Open
'MsgBox "conn Open"
'进行查询
Set oRs = CreateObject("ADODB.Recordset")
Set oCom = CreateObject("ADODB.Command")
oCom.CommandType = 1
Set oCom.ActiveConnection = conn
oCom.CommandText = sSql
'MsgBox "set command"
'填充记录集
Set oRs = oCom.Execute
'm = oRs.Fields.Count
MsgBox oRs.EOF
If(oRs.EOF) Then
oRs.Close
Else
oRs.MoveFirst
Do While Not oRs.EOF
HMIRuntime.Tags("AlarmText1").Write CStr(oRs.Fields(17).Value)
oRs.MoveNext
Loop
oRs.Close
End If
Set oRs = Nothing
conn.Close
Set conn = Nothing
'MsgBox "end"
End Sub
下面是WINCC读写EXCEL的脚本
写:
Sub OnOpen()
Dim objComboBox
Set objComboBox = ScreenItems("控件1")
objComboBox.Clear()
Dim objExcelApp,oWorkBook
Set objExcelApp = CreateObject("Excel.Application")
objExcelApp.Visible = False
Set oWorkBook = objExcelApp.Workbooks.Open("c:\1.xls")
objComboBox.AddItem objExcelApp.Cells(1, 1).Value
objComboBox.AddItem objExcelApp.Cells(1, 2).Value
objComboBox.AddItem objExcelApp.Cells(1, 3).Value
objComboBox.SelText = "选项1"
objExcelApp.Workbooks.Close
objExcelApp.Quit
Set objExcelApp = nothing
End Sub
读:
Sub OnLButtonDown(Byval Item, Byval Flags, Byval x, Byval y)
Dim objExcelApp,oWorkBook
Set objExcelApp = CreateObject("Excel.Application")
objExcelApp.Visible = False
Set oWorkBook = objExcelApp.Workbooks.Open("c:\1.xls")
Dim iBlankLine
iBlankLine = oWorkBook.ActiveSheet.Columns(1).Find("").Row '查找第一列的空行
'MsgBox iBlankLine
objExcelApp.Cells(iBlankLine, 1).Value = "tag1"
objExcelApp.Cells(iBlankLine, 2).Value = "tag2"
objExcelApp.Cells(iBlankLine, 3).Value = "tag3"
oWorkBook.Save
objExcelApp.Workbooks.Close
objExcelApp.Quit
Set objExcelApp = nothing
'Set oWorkBook =objExcelApp.Workbooks.Add
'oWorkBook.SaveAs "D:\ExcelExample.xls"
End Sub
以上都是我写的 现在还在用 这两部分合起来就能完成你的功能了
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)