发布新日志

  • QTP链接数据库

    2009-03-27 17:51:31

    *********************************************************
    '连接数据库
    '*********************************************************
    Dim conn,res,strSql ,strSqll ,numbe '定义变量
    On error resume next
    set conn = CreateObject("ADODB.Connection")   '创建连接
    conn.ConnectionString="Provider=MSDAORA.1;Password=Password;Persist Security Info=True;User

    ID=id;Data Source=192.168.0.1:****/source;"   '设置连接字符串
    conn.open  '开启连接

    If conn.state=0 Then   '如果连接状态为0,表示连接失败,写入reporter对象中,否则表示连接成功
        Reporter.ReportEvent micFail,"testing","数据库连接失败"
        res.close                               '关闭结果集和连接,并且置为空
        Set res = nothing
        conn.close
        Set conn = nothing
    else
       Reporter.ReportEvent micPass,"testing","数据库连接成功"

  • 数据库中查到的记录输出到execl

    2009-03-27 17:44:29


    '*********************************************************
    '创建记录集对象,执行查询语句
    '*********************************************************
    Set res = CreateObject("ADODB.RecordSet")   '创建一个记录集对象
    Set resl= CreateObject("ADODB.RecordSet")   '创建一个记录集对象
    strSql="select  *  from zhang_students"  '设置查询语句
    strSqll="select  count(*)  from zhang_students"
    res.open strSql,conn    '执行查询语句
    resl.open strSqll,conn


     '*********************************************************
    '创建excel表格
    '*********************************************************
    Dim excelApp,excelSheet,fso
    Set excelApp = CreateObject("Excel.Application") '创建Excel Application对象
    Set excelSheet =  CreateObject("Excel.sheet") '创建Excel Sheet对象
    Set fso = CreateObject("scripting.FileSystemObject") '创建FileSystemObject对象
    filename = "E:\QTP\text.xls" '设定文件路径
    excelApp.Visible = true 'excel可见
    excelApp.WorkBooks.Add

    '*********************************************************
    '查询结果输出到excel中
    '*********************************************************
    numbe=CLng(resl("count(*)"))
    For  i=1  to  numbe
    'Do While Not res.EOF
    'MsgBox Trim(res.Fields("Test Results"))
    'Loop
    'msgbox res("name")
    excelApp.Cells(i,1).Value =res("name")
    excelApp.Cells(i,2).Value =res("sex")
    excelApp.Cells(i,3).Value =res("address")
    excelApp.Cells(i,4).Value =res("class")
    excelApp.Cells(i,5).Value =res("score")
    res.MoveNext
    Next
    excelApp.ActiveWorkBook.Saved
    excelApp.Quit

    '*********************************************************
    '关闭连接
    '*********************************************************
       res.close
       Set res = nothing
       conn.close
       Set conn = nothing
    End If

我的栏目

数据统计

  • 访问量: 849
  • 日志数: 2
  • 建立时间: 2008-12-10
  • 更新时间: 2009-03-27

RSS订阅

Open Toolbar