大家有什么意见,欢迎拍砖!

vbs中操作数据库、读写文件、XML

上一篇 / 下一篇  2013-04-26 15:58:22 / 个人分类:个人学习

'数据库连接
'Option explicit
'Dim cnn,rst,strCnn,str,i
'Dim ParamCount,ParamIndex,ParamName,ParamValue
'strCnn="Provider=SQLOLEDB.1;Password=Atlas2012;Persist Security Info=True;User ID=sa;Initial Catalog=aaa;Data Source=127.0.0.1"
'Set cnn=CreateObject("ADODB.Connection")
'cnn.Open strCnn
'Set rst=CreateObject("ADODB.RecordSet")
'rst.Open "select * from groupinfo ",cnn
'rst.MoveFirst
' i=1
'Do while not rst.EOF
' ' str=str&rst.Fields("Tag_patient_name")&vbcrlf
' datatable.SetCurrentRow(i)
' str=rst.Fields("GroupID") &"     "&rst.Fields("GroupName")
'     datatable.Value(1,"Action1")=str
' rst.MoveNext
' i=i+1
'Loop
''msgbox str
'rst.Close
'cnn.Close
'Set rst=nothing
'Set cnn=nothing
 
'--文本文件读写’
'Option explicit
'Dim fso,file,i,msg
'Set fso=createObject("Scripting.FileSystemObject")
'If fso.FileExists("c:\test.txt") then
'   fso.DeleteFile("c:\test.txt")
'   msgbox("删除成功!")
'else
'  Set file=fso.CreateTextFile("c:\test.txt",2)
'  msgbox("创建成功!")
'end if
'For i=1 to 5
'    file.Write(i) 
' 'writeline()后面要加换行符
'Next
'msgbox("写文件成功!")
'
'Set file=fso.OpenTextFile("C:\test.txt",1)
''while(not File.AtEndOfStream)
'' msg=msg&file.ReadLine&vbcrlf
''wend
'msg=file.ReadAll
'msgbox("读文件:"&msg)
'file.Close
'Set file=nothing
'Set fso=nothing
'Option explicit
'Dim xlApp,xlWorkBook,xlSheet,row,col
'Set xlApp=CreateObject("Excel.Application")
'xlApp.Visible=true
'Set xlWorkBook=xlApp.Workbooks.Open("c:\data.xlsx")
'Set xlSheet=xlWorkBook.Sheets("Sheet1")
'For row=1 to 10
'  For  col=1 to 3
'   xlSheet.Cells(row,col)=row+col
'  Next 
'Next
'xlWorkBook.Save
'xlWorkBook.Close
'xlApp.Quit
'Set xlSheet=nothing
'Set xlWorkBook=nothing
'Set xlApp=nothing
 
'播放音乐
'Dim wmp
'Set wmp=createobject("WMPlayer.ocx")
'wmp.openPlayer("c:\滴答.mp3")
'Do  until wmp.playState=1
'    WScript.Sleep 1000
'Loop
 
'xml文件读写
Option explicit
Dim xmldoc,xmlroot,nodelist,i
Set xmldoc=CreateObject("Microsoft.XMLDOM") '创建xml dom对象
xmldoc.async=false   '控制加载模式为同步模式(xml树加载完毕后再执行后续代码)
xmldoc.load  "c:\aa.xml"  '加载文档
If   xmldoc.parseError.errorCode<>0  Then
 msgbox "xml load fail"
End If
Set xmlroot=xmldoc.documentElement  '获取节点
' MsgBox xmlroot.text
If  xmlroot.hasChildNodes Then
 For each nodelist  in xmlroot.ChildNodes
    msgbox nodelist.getAttribute("Sex") '读属性值
    'MsgBox nodelist.text '读所有元素值
    For i=0 to (nodelist.childNodes.length-1) '读每个元素
        msgbox(nodelist.childNodes(i).nodeName&": "&nodelist.childNodes(i).text)
    Next  
 Next
End If
 
对应XML文件:
<?xml version="1.0" encoding="UTF-8"?>
<Contacts>    
<Person Sex="男">        
 <Name>小陈</Name>        
 <Address>福建</Address>       
 <Phone>110</Phone>       
 <Email>小陈@gmail.com</Email>    
</Person>   
<Person Sex="女">        
 <Name>小林</Name>        
 <Address>广州</Address>        
 <Phone>112</Phone>        
 <Email>小林@gmail.com</Email>    
</Person>
</Contacts>
 
自己练习的,可以直接运行,保存下来。

TAG: vbs VBS XML xml

 

评分:0

我来说两句

我的栏目

日历

« 2024-03-29  
     12
3456789
10111213141516
17181920212223
24252627282930
31      

数据统计

  • 访问量: 13793
  • 日志数: 15
  • 建立时间: 2011-06-01
  • 更新时间: 2013-05-29

RSS订阅

Open Toolbar