Why should I close and destroy a recordset?

I read this article: http://www.utteraccess.com/wiki/Recordsets_for_Beginners , and he says that it is important for me to shut down and destroy RS as follows:
rs.close Set rs = Nothing
and if I do not, some errors may occur.

  • Which mistakes?
  • What does it mean rs.close? Does this mean that the database connection remains open until rsit closes?
+1
source share
2 answers

rs.closecleans up resources that are used internally, however, since ASP is the only process that does not have a GC, it Set rs = Nothinghelps in cleansing.

, . , ?

+2

T McKeown

ADODB.Recordset, , . Call rs.Close() - , .

, , , .

Dim conn, conn_string, rs

conn_string = "some connection string to your database"

'Memory allocated for ADODB.Connection object
Set conn = Server.CreateObject("ADODB.Connection")
'Connection opened to data source (SQL Server, Oracle, MySQL etc)
Call conn.Open()

Set rs = conn.Execute("SELECT * FROM [sometable]")
Do While Not rs.EOF
  'Long running page (do some processing)
  Call rs.MoveNext()
Loop

'Remove any locks on the tables and dis-associate connection
Call rs.Close()
'Deallocate ADODB.Recordset from memory
Set rs = Nothing

'Connection is still open and needs to be closed
Call conn.Close()
'Connection closed but still allocated in memory
Set conn = Nothing

, ADODB.

Dim conn, conn_string, rs, cmd, data

conn_string = "some connection string to your database"

'Memory allocated for ADODB.Command object
Set cmd = Server.CreateObject("ADODB.Command")
With cmd
  'Connection allocated when ADODB.Command is run and only
  'lasts for the life of ADODB.Command object.
  .ActiveConnection = conn_string
  .CommandType = adCmdText
  .CommandText = "SELECT * FROM [sometable]"
  'Allocate memory for ADODB.Recordset
  Set rs = .Execute()
  'Use .GetRows() to build a two dimensional array 
  'containing the ADODB.Recordset data.
  If Not rs.EOF Then data = rs.GetRows()
  Call rs.Close()
  'Deallocate memory for ADODB.Recordset
  Set rs = Nothing
End with
'Deallocate ADODB.Command which closes and de-allocates
'the associated  ADODB.Connection.
Set cmd = Nothing

'All ADODB objects have been deallocated rest of the page can run
'without them using the array to iterate through the data.
If IsArray(data) Then
  'Long running page (do some processing)
End If

. , .

+4

Source: https://habr.com/ru/post/1651338/


All Articles