我在模块中写了一个简单函数大致如下
public function getRs(byval strDatabasename as string,byval strSql as string) as adodb.recordset
//strDatabasename 为要连接的数据库的名字,strSql为要执行的sql语句
//函数返回一个记录集
dim strconn as string
dim conn as new adodb.connection
dim rs as new adodb.recordset
strconn="provider= ……" & strDatabasename & "……"
conn.open strconn //连接数据库
rs.open strSql , conn, 2,2 //得到记录集
set getRs=rs //函数返回一个记录集
End function
请问大家在模块里用的conn对象和rs对象什么时候释放掉啊???
如果我在函数末尾加上set conn=nothing 调用的时候程序就会出错,说数据库已经关闭,加上set rs=nothing也会出错,怎么办啊?
我要得到记录集并且还要对其操作,所以不知道什么时候释放,也不知道怎么释放模块里的连接对象,请大家帮忙???
public function getRs(byval strDatabasename as string,byval strSql as string) as adodb.recordset
//strDatabasename 为要连接的数据库的名字,strSql为要执行的sql语句
//函数返回一个记录集
dim strconn as string
dim conn as new adodb.connection
dim rs as new adodb.recordset
strconn="provider= ……" & strDatabasename & "……"
conn.open strconn //连接数据库
rs.open strSql , conn, 2,2 //得到记录集
set getRs=rs //函数返回一个记录集
End function
请问大家在模块里用的conn对象和rs对象什么时候释放掉啊???
如果我在函数末尾加上set conn=nothing 调用的时候程序就会出错,说数据库已经关闭,加上set rs=nothing也会出错,怎么办啊?
我要得到记录集并且还要对其操作,所以不知道什么时候释放,也不知道怎么释放模块里的连接对象,请大家帮忙???
该范例使用已经打开的 Recordset 和 Connection 对象的 Open 和 Close 方法。Public Sub OpenX() Dim cnn1 As ADODB.Connection
Dim rstEmployees As ADODB.Recordset
Dim strCnn As String
Dim varDate As Variant ' 打开连接。
strCnn = "Provider=sqloledb;" & _
"Data Source=srv;Initial Catalog=pubs;User Id=sa;Password=; "
Set cnn1 = New ADODB.Connection
cnn1.Open strCnn
' 打开雇员表。
Set rstEmployees = New ADODB.Recordset
rstEmployees.CursorType = adOpenKeyset
rstEmployees.LockType = adLockOptimistic
rstEmployees.Open "employee", cnn1, , , adCmdTable ' 将第一个雇员记录的受雇日期赋值给变量,然后更改受雇日期。
varDate = rstEmployees!hire_date
Debug.Print "Original data"
Debug.Print " Name - Hire Date"
Debug.Print " " & rstEmployees!fName & " " & _
rstEmployees!lName & " - " & rstEmployees!hire_date
rstEmployees!hire_date = #1/1/1900#
rstEmployees.Update
Debug.Print "Changed data"
Debug.Print " Name - Hire Date"
Debug.Print " " & rstEmployees!fName & " " & _
rstEmployees!lName & " - " & rstEmployees!hire_date ' 再查询 Recordset 并重置受雇日期。
rstEmployees.Requery
rstEmployees!hire_date = varDate
rstEmployees.Update
Debug.Print "Data after reset"
Debug.Print " Name - Hire Date"
Debug.Print " " & rstEmployees!fName & " " & _
rstEmployees!lName & " - " & rstEmployees!hire_date rstEmployees.Close
cnn1.CloseEnd Sub