我想我会一直走在测试的道路上,既然 我已选择了测试这条路 欢迎测试界的XDJM一起交流、共同进步

QTP那点事之VBS操作文件数据库篇

上一篇 / 下一篇  2009-05-01 16:31:31 / 个人分类:QTP

QTP那点事之VBS操作文件数据库

VBS对于文件和数据库的操作都是通过对象来进行的,也就是说在使用文件或数据库的之前,都必须先创建相应的对象,通过对象的操作来实现。

文本文件的读取

Option Explicit

Const ForReading = 1

Const ForWritting = 2

Const ForAppending = 8

Dim fso,file,msg

Set fso=CreateObject("Scripting.FileSystemObject")

Set file=fso.OpenTextFile("d:\a.txt",ForReading,True)

Do While Not file.AtEndOfStream

msg = file.ReadLine

MsgBox msg

Loop

file.Close

Set file=Nothing

Set fso=Nothing

第一步:首先创建两个对象,一个FileSystemObject对象fso,然后用fso对象的opentextfile方法去打开一个文件。并返回一个textStream对象给file

第二步:使用file对象对打开的文件进行操作,例子中是对文件的读取。读取通过file对象的一个方法ReadLine来实现。

第三步:读取完毕,关闭文件

第四步:释放file对象和fso对象

File还有一些其它的方法,如ReadReadAll

文本文件的写入

Option Explicit

Const ForReading = 1

Const ForWritting = 2

Const ForAppending = 8

Dim fso,file,msg

Set fso=CreateObject("Scripting.FileSystemObject")

Set file=fso.OpenTextFile("d:\a.txt",ForWritting,True)

File.WriteLine “hello world”

File.WriteLine “this is a writing file case”

file.Close

Set file=Nothing

Set fso=Nothing

文件写入的步骤跟读取是一样的,唯一的区别是它们打开文件的方式不同。一个是以读的方式打开,另一个是以写的方式,这通过OpenTextFile方法的参数实现。OpenTextFile方法的第二个参数是一个常量值:1281ForReading的,2ForWritting的,3ForAppending的追加方式。

Excel文件的读写

 

Option Explicit

Dim xlApp,xlFile,xlSheet,xlSheet2

Dim iRowCount,iLoop,jLoop,numAdd(4),iColumnCount,record

Set xlApp=CreateObject("Excel.Application")

Set xlFile=xlApp.Workbooks.Open("D:\data.xls")

Set xlSheet=xlFile.Sheets("Sheet1")

Set xlSheet2=xlFile.Sheets("Sheet2")

 

iRowCount=xlSheet.usedRange.Rows.Count

iColumnCount=xlSheet.usedRange.Columns.Count

 

For iLoop = 1 To iRowCount

      For jLoop = 1 To iColumnCount

             numAdd(jLoop) = xlSheet.Cells(iLoop,jLoop)

             'MsgBox numAdd(jLoop)

             xlSheet2.Cells(iLoop,jLoop)=numAdd(jLoop)

             

             'MsgBox xlSheet2.Cells(iLoop,jLoop)

      Next

      record = Join(numAdd," ")

      MsgBox record

Next

 

xlFile.Save

xlFile.Close

xlApp.Quit

Set xlSheet = Nothing

Set xlSheet2 = Nothing

Set xlFile = Nothing

Set xlApp = Nothing

第一步:Excel的三层概念:ApplicationExcelFileSheet,因此需要创建三个对象来实现对Excel文件的操作。首先创建一个Excel.Application对象xlApp,然后通过xlApp打开一个Excel文件,并返回一个对象给xlFile,最后通过xlFileSheets属性来操作某一个sheet

第二步:通过sheet对象的cells子对象来操作某一sheet中的区域,sheet对象可以通过usedRange属性来识别此文件已经使用的cells。通过其中的count方法来获取具体值。

第三步:操作完成,保存文件、关闭文件、退出程序

第四步:释放对象

数据库的读写

Dim Cnn, Rst, strCnn

Set Cnn = CreateObject( "ADODB.Connection“ )

Set Rst = CreateObject( "ADODB.Recordset“ )

strCnn= "Provider=Microsoft.Jet.OLEDB.4.0.1;Data Source=C:\qtp_file\cal.mdb; Persist Security Info=False"

Cnn.Open strCnn

Rst.Open "Select * from student", Cnn

Rst.MoveFirst

Do While Not Rst.EOF

MsgBox Trim(Rst.Fields("name"))

Rst.MoveNext

Loop

Rst.Close

Cnn.Close

Set Rst = Nothing

Set Cnn = Nothing

1) 创建两个对象Connection对象和RecordSet对象和一个字符串连接串

2) 使用Connection通过连接串连接数据库

3) 将数据库操作的结果集保存到RecordSet对象中

4) 对于结果集中的数据可以通过RecordSet对象的方法来进行操作,Move系列方法是移动游标到相应位置,Fields属性来获取相应的字段值。

5) 关闭结果集、关闭数据库连接

6) 释放对象

其它的数据库文件的操作都是类似的,只要通过相应的字符串连接串进行连接。

xml文件的读写

Dim xmlDoc, xmlRoot, ChildItem, msg

Set xmlDoc = CreateObject ("Microsoft.XMLDOM")

xmlDoc.Load "C:\qtp_file\addressbook.xml" '使用load方法来加载xml文档,建立dom树和xml文档之间的关联

 

Set xmlRoot = xmlDoc.documentElement '获取XML文档的根元素节点

 

For Each ChildItem In xmlRoot.childNodes 

      MsgBox "当前节点是:" & ChildItem.baseName '显示当前节点的名称

        Set node1=ChildItem.childNodes.item(0)

        Set node2=ChildItem.childNodes.item(1)

        Set node3=ChildItem.childNodes.item(2)

        MsgBox node1.baseName

        MsgBox node2.baseName

        MsgBox node3.baseName

        MsgBox node1.firstChild.nodeValue

        MsgBox node2.firstChild.nodeValue

        MsgBox node3.firstChild.nodeValue

Next

1) 同样通过创建XmlDom对象,使用对象通过load方法来关联xml文件

2) 创建xmlRoot对象来获取xml文件的根元素

3) 通过对xmlRoot的子节点来操作整个文件

4) 当然,最后结束操作要释放对象

 


TAG: vbs VBS

cherryqxh的个人空间 引用 删除 cherryqxh   /   2009-06-06 09:04:03
好文章,受教了
 

评分:0

我来说两句

我的栏目

日历

« 2024-04-12  
 123456
78910111213
14151617181920
21222324252627
282930    

数据统计

  • 访问量: 25313
  • 日志数: 42
  • 建立时间: 2008-12-06
  • 更新时间: 2009-06-15

RSS订阅

Open Toolbar