|
Function OpenConnection()
Set Cnn = CreateObject("ADODB.Connection")
' Cnn.ConnectionString = "rovider=MSDASQL.1ersist Security Info=true;User Id=saassword=command;Data Source = LocalServer"
' Cnn.Open
StrCon ="rovider=MSDASQL;Driver={SQL Server};Server=10.3.50.32;Database=ViewBase;Uid=sawd=command"
Cnn.Open StrCon
If Cnn.state=0 Then
conn.close
Set conn = Nothing
Reporter.ReportEvent micFail,"数据库连接失败!","连接语句为:"&StrCon
logPrint("数据库连接失败!连接语句为:"&StrCon)
Else
Reporter.ReportEvent micPass,"数据库连接成功!","连接语句为:"&StrCon
logPrint("数据库连接成功!连接语句为:"&StrCon)
End If
Set OpenConnection = Cnn
End Function
Sub ExecuteSql(strSql)
Dim strValue,iCount
Cnn = OpenConnection()
Set rs = CreateObject("ADODB.RecordSet")
strSql = LCase(LTrim(strSql))
If Left(strSql,6)="select" Then
rs.Open strSql,Cnn,1,1
If rs.EOF Then
Reporter.ReportEvent micFail,"查询语句查询结果为空!","查询语句为:"&strSql
logPrint("查询语句查询结果为空!查询语句为:"&strSql)
Else
iCount = rs.Fields.Count
rs.MoveFirst '将记录集游标指到记录集的最开始
Do
Dim strR
For i = 0 To iCount - 1
strValue = rs.Fields(i).Value
strR=strR&strValue
Next
Reporter.ReportEvent micPass,"查询成功","查询结果为:"&strR
logPrint("查询结果为:"&strR)
strR=""
rs.MoveNext
Loop until rs.EOF = true
End If
Else
rs.Open strSql,Cnn,1,3
If rs.EOF Then
Reporter.ReportEvent micFail,"执行语句失败!","执行语句为:"&strSql
logPrint("执行语句失败!"&strSql)
Else
Reporter.ReportEvent micPass,"执行语句成功!","执行语句为:"&strSql
logPrint("执行语句成功!"&strSql)
End If
End If
rs.close
Set rs = nothing
End Sub
Public Function logPrint(ByVal logMessage)
Dim fso, logFile
Set fso = CreateObject("Scripting.FileSystemObject")
Set logFile = fso.OpenTextFile("D:\我的文档\QTP学习\BCTTest\runtime.log", 8, True) 'Open a file and write to the end of the file and open as Unicode
logFile.WriteLine(date() & " " & hour(now) & ":" & minute(now) & ":" & second(now) & ": " & logMessage)
logFile.Close
End Function |
|