I'm looking at another developers code and I'm now confused about the
correct way to close a database connection and destroy the object.
I would normally do it this way:
Set conn = Server.CreateOb ject("adodb.con nection")
conn.Execute(UP DATE TABLE Blah Set Something = 'Somevalue' WHERE
whatever = 'blah')
conn.Close
Set conn = nothing
This other developer has closed the connection this way:
conn = Server.CreateOb ject("adodb.con nection")
conn.Execute(UP DATE TABLE Blah Set Something = 'Somevalue' WHERE
whatever whatever = 'blah')
conn.close()
conn = null
1. Which is correct (or better) way of coding this sql update?
2. Is the "conn = null" method of destroying the 'conn' object valid?
An example of the code that I've inherited is:
db="DRIVER={Mic rosoft Access Driver (*.mdb)}; DBQ=" +
Server.Mappath( "thedb.mdb" )
conn=Server.Cre ateObject("adod b.connection")
conn.Open(db)
SQLStmt = "INSERT INTO tblBlah (Name,Surname,O ccupation) "
SQLStmt = SQLStmt + "VALUES ('" + frmTest[0] + "'" + ",'" + frmTest[1]
+ "'" + ",'" + frmTest[2] + "'"
RS = conn.Execute(SQ LStmt)
RS = null
conn.close()
conn = null
My way of coding it would be like this:
Dim SQLStmt, db, conn
SQLStmt = "INSERT INTO tblBlah (Name,Surname,O ccupation) " & _
"VALUES ('" & frmTest[0] & "', '" & frmTest[1] & "', '" &
frmTest[2] & "')"
Set conn = Server.CreateOb ject("adodb.con nection")
conn.Open("DRIV ER={Microsoft Access Driver (*.mdb)}; DBQ=" +
Server.Mappath( "thedb.mdb" ))
conn.Execute(SQ LStmt)
conn.Close
Set conn = nothing
3. Assigning the conn.Execute(SQ LStmt) to the RS variable in the
original code is not necessary. Am I correct?
4. Is the "conn = null" method of destroying the 'conn' object valid?
Yes, I know, putting the sql code into a stored procedure would be the
best solution, but I'd like to know which of the above methods is
better.
Thanks.
correct way to close a database connection and destroy the object.
I would normally do it this way:
Set conn = Server.CreateOb ject("adodb.con nection")
conn.Execute(UP DATE TABLE Blah Set Something = 'Somevalue' WHERE
whatever = 'blah')
conn.Close
Set conn = nothing
This other developer has closed the connection this way:
conn = Server.CreateOb ject("adodb.con nection")
conn.Execute(UP DATE TABLE Blah Set Something = 'Somevalue' WHERE
whatever whatever = 'blah')
conn.close()
conn = null
1. Which is correct (or better) way of coding this sql update?
2. Is the "conn = null" method of destroying the 'conn' object valid?
An example of the code that I've inherited is:
db="DRIVER={Mic rosoft Access Driver (*.mdb)}; DBQ=" +
Server.Mappath( "thedb.mdb" )
conn=Server.Cre ateObject("adod b.connection")
conn.Open(db)
SQLStmt = "INSERT INTO tblBlah (Name,Surname,O ccupation) "
SQLStmt = SQLStmt + "VALUES ('" + frmTest[0] + "'" + ",'" + frmTest[1]
+ "'" + ",'" + frmTest[2] + "'"
RS = conn.Execute(SQ LStmt)
RS = null
conn.close()
conn = null
My way of coding it would be like this:
Dim SQLStmt, db, conn
SQLStmt = "INSERT INTO tblBlah (Name,Surname,O ccupation) " & _
"VALUES ('" & frmTest[0] & "', '" & frmTest[1] & "', '" &
frmTest[2] & "')"
Set conn = Server.CreateOb ject("adodb.con nection")
conn.Open("DRIV ER={Microsoft Access Driver (*.mdb)}; DBQ=" +
Server.Mappath( "thedb.mdb" ))
conn.Execute(SQ LStmt)
conn.Close
Set conn = nothing
3. Assigning the conn.Execute(SQ LStmt) to the RS variable in the
original code is not necessary. Am I correct?
4. Is the "conn = null" method of destroying the 'conn' object valid?
Yes, I know, putting the sql code into a stored procedure would be the
best solution, but I'd like to know which of the above methods is
better.
Thanks.
Comment