Handling ADODB Connections in Classic ASP

I am an ASP.NET C # guy who needs to get back to classic ASP, and he needs help.

First, consider two functions (I know that in VBScript comments are declared ', not //, but the syntax shortcut here has messed up with ').

First version:

Function DoThing

    Dim Connection, Command, Recordset

    Set Connection = Server.CreateObject("ADODB.Connection")
    Set Command = Server.CreateObject("ADODB.Command")
    Set Recordset = Server.CreateObject("ADODB.Recordset")

    Connection.Open "blah blah blah"
    Command.CommandText = "blah blah blah"
    Recordset.Open Command, Connection

    If Recordset.EOF = False Then

        While Recordset.EOF = False Then

            // Do stuff.

            Recordset.MoveNext

        WEnd

    Else

        // Handle error.

    End If

    Recordset.Close
    Connection.Close

    Set Recordset = Nothing
    Set Command = Nothing
    Set Connection = Nothing

End Function

Second version:

Function DoAnotherThing

    Dim Connection, Command, Recordset

    Set Connection = Server.CreateObject("ADODB.Connection")
    Set Command = Server.CreateObject("ADODB.Command")

    Connection.Open "blah blah blah"

    Command.ActiveConnection = Connection
    Command.CommandText = "blah blah blah"

    Set Recordset = Command.Execute

    If Recordset.EOF = False Then

        While Recordset.EOF = False Then

            // Do stuff.

            Recordset.MoveNext

        WEnd

    Else

        // Handle error.

    End If

    Recordset.Close
    Connection.Close

    Set Recordset = Nothing
    Set Command = Nothing
    Set Connection = Nothing

End Function

Now let's start with the questions:

Question number 1:

What's the best

Connection.Open "blah blah blah"
Command.CommandText = "blah blah blah"
Recordset.Open Command, Connection

or

Connection.Open "blah blah blah"

Command.ActiveConnection = Connection
Command.CommandText = "blah blah blah"

Set Recordset = Command.Execute

?

Question number 2:

When performing SELECT, use disabled recordsets, for example

Set Recordset = Command.Execute

Command.ActiveConnection = Nothing

Connection.Close

Set Connection = Nothing

?

Question number 3:

I need to call

Recordset.Close
Connection.Close

although i do

Set Recordset = Nothing
Set Command = Nothing
Set Connection = Nothing

on the right (i.e. garbage collector when calling them Set Stuff= Nothingalso .Closebefore destruction)?

Question number 4:

Do I need to include

Recordset.Close
Connection.Close

Set Recordset = Nothing
Set Command = Nothing
Set Connection = Nothing

even if the function ends there, and these objects are out of scope, and the garbage collector (should) take care of them?

Question number 5:

ASP , , ?


, .

+3
1

№ 1.

.
, , Recordset.Open , .

№ 2.

, . - , , , .

№ 3.

VBScript , .

, .
- , Nothing , ( Nothing , ).

, , , , Nothing ( ) .

- Close, , , Nothing. , .

№ 4.

. 3.

№ 5.

- Connection, , , .

+10

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


All Articles