A
Andyza
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.CreateObject("adodb.connection")
conn.Execute(UPDATE TABLE Blah Set Something = 'Somevalue' WHERE
whatever = 'blah')
conn.Close
Set conn = nothing
This other developer has closed the connection this way:
conn = Server.CreateObject("adodb.connection")
conn.Execute(UPDATE 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={Microsoft Access Driver (*.mdb)}; DBQ=" +
Server.Mappath("thedb.mdb")
conn=Server.CreateObject("adodb.connection")
conn.Open(db)
SQLStmt = "INSERT INTO tblBlah (Name,Surname,Occupation) "
SQLStmt = SQLStmt + "VALUES ('" + frmTest[0] + "'" + ",'" + frmTest[1]
+ "'" + ",'" + frmTest[2] + "'"
RS = conn.Execute(SQLStmt)
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,Occupation) " & _
"VALUES ('" & frmTest[0] & "', '" & frmTest[1] & "', '" &
frmTest[2] & "')"
Set conn = Server.CreateObject("adodb.connection")
conn.Open("DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" +
Server.Mappath("thedb.mdb"))
conn.Execute(SQLStmt)
conn.Close
Set conn = nothing
3. Assigning the conn.Execute(SQLStmt) 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.CreateObject("adodb.connection")
conn.Execute(UPDATE TABLE Blah Set Something = 'Somevalue' WHERE
whatever = 'blah')
conn.Close
Set conn = nothing
This other developer has closed the connection this way:
conn = Server.CreateObject("adodb.connection")
conn.Execute(UPDATE 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={Microsoft Access Driver (*.mdb)}; DBQ=" +
Server.Mappath("thedb.mdb")
conn=Server.CreateObject("adodb.connection")
conn.Open(db)
SQLStmt = "INSERT INTO tblBlah (Name,Surname,Occupation) "
SQLStmt = SQLStmt + "VALUES ('" + frmTest[0] + "'" + ",'" + frmTest[1]
+ "'" + ",'" + frmTest[2] + "'"
RS = conn.Execute(SQLStmt)
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,Occupation) " & _
"VALUES ('" & frmTest[0] & "', '" & frmTest[1] & "', '" &
frmTest[2] & "')"
Set conn = Server.CreateObject("adodb.connection")
conn.Open("DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" +
Server.Mappath("thedb.mdb"))
conn.Execute(SQLStmt)
conn.Close
Set conn = nothing
3. Assigning the conn.Execute(SQLStmt) 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.