龍羽君 发表于 2010-5-19 13:58:55

用update时老是提示对象关闭时,不允许操作

Function OpenConnection()
        Set Cnn = CreateObject("ADODB.Connection")
'        Cnn.ConnectionString = "Provider=MSDASQL.1;Persist Security Info=true;User Id=sa;Password=command;Data Source = LocalServer"
'        Cnn.Open
        StrCon ="Provider=MSDASQL;Driver={SQL Server};Server=10.3.50.32;Database=ViewBase;Uid=sa;Pwd=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

TIB 发表于 2010-5-19 15:37:00

不要用rs.Open strSql,Cnn,1,3

Cnn.Execute strSql

yahuu 发表于 2010-5-19 15:45:19

如果有返回值就不能只Execute吧?

TIB 发表于 2010-5-19 22:02:01

原帖由 yahuu 于 2010-5-19 15:45 发表 http://bbs.51testing.com/images/common/back.gif
如果有返回值就不能只Execute吧?
可以找份ADO的文档具体看看
页: [1]
查看完整版本: 用update时老是提示对象关闭时,不允许操作