关闭数据库连接的正确方法是什么? [英] The correct way to close a database connection?

查看:148
本文介绍了关闭数据库连接的正确方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找另一个开发人员代码,我现在对

关闭数据库连接并销毁对象的正确方法感到困惑。


我通常会这样做:


设置conn = Server.CreateObject(" adodb.connection")

conn.Execute(UPDATE表Blah Set Something =''Somevalue''WHERE

无论='''blah'')

conn.Close

设置conn = nothing


这个其他开发人员以这种方式关闭了连接:


conn = Server.CreateObject(" adodb.connection")

conn.Execute(UPDATE TABLE Blah Set Something =''Somevalue''WHERE

无论什么='''blah'')

conn.close( )

conn = null

1.这个sql更新编码的正确(或更好)方法是什么?

2。是conn = null吗?破坏''conn''对象的方法有效吗?


我继承的代码示例是:


db =" DRIVER = {Microsoft Access Driver(* .mdb)}; DBQ = QUOT; +

Server.Mappath(" thedb.mdb")

conn = Server.CreateObject(" adodb.connection")

conn 。打开(db)

SQLStmt =" INSERT INTO tblBlah(姓名,姓氏,职业)"

SQLStmt = SQLStmt +" VALUES(' '" + frmTest [0] +"''" +",''" + frmTest [1]

+"''" +",''" ; + frmTest [2] +"''"


RS = conn.Execute(SQLStmt)

RS = null

conn.close()

conn = null

我的编码方式如下:


Dim SQLStmt ,db,conn

SQLStmt =" INSERT INTO tblBlah(Name,Surname,Occupation)"& _

" VALUES(''"& frmTest [0]&"'',''"& frmTest [1]&"'',''"&

frmTest [2]&"' ')"


设置conn = Server.CreateObject(" adodb.connection& ;)

conn.Open(" DRIVER = {Microsoft Access Driver(* .mdb)}; DBQ = QUOT; +

Server.Mappath(" thedb.mdb"))

conn.Execute(SQLStmt)

conn.Close

设置conn = nothing

3.不需要将

原始代码中的conn.Execute(SQLStmt)分配给RS变量。我是否正确?

4.是conn = null破坏''conn''对象的方法有效吗?

是的,我知道,将sql代码放入存储过程将是

最佳解决方案,但我想知道上面哪种方法更好。$ / b

谢谢。

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.

推荐答案

你的更好。


Bob Lehmann


" Andyza" < an **** @ webmail.co.zawrote in message

news:11 ********************* @ v45g2000cwv。 googlegro ups.com ...
Yours is better.

Bob Lehmann

"Andyza" <an****@webmail.co.zawrote in message
news:11*********************@v45g2000cwv.googlegro ups.com...

我正在查看另一个开发人员代码,我现在对

正确感到困惑关闭数据库连接并销毁对象的方法。


我通常会这样做:


设置conn = Server.CreateObject( " adodb.connection")

conn.Execute(UPDATE TABLE Blah Set Something =''Somevalue''WHERE

whatever =''blah'')

conn.Close

设置conn =无


其他开发人员已通过这种方式关闭连接:


conn = Server.CreateObject(" adodb.connection")

conn.Execute(UPDATE TABLE Blah Set Something =''Somevalue''WHERE

无论='''等等')

conn.close()

conn = null


1.哪个是正确的(或b etter)编码这个sql更新的方法?

2.是conn = null吗?破坏''conn''对象的方法有效吗?


我继承的代码示例是:


db =" DRIVER = {Microsoft Access Driver(* .mdb)}; DBQ = QUOT; +

Server.Mappath(" thedb.mdb")

conn = Server.CreateObject(" adodb.connection")

conn 。打开(db)

SQLStmt =" INSERT INTO tblBlah(姓名,姓氏,职业)"

SQLStmt = SQLStmt +" VALUES(' '" + frmTest [0] +"''" +",''" + frmTest [1]

+"''" +",''" ; + frmTest [2] +"''"


RS = conn.Execute(SQLStmt)

RS = null

conn.close()

conn = null


我的编码方式如下:


Dim SQLStmt,db,conn

SQLStmt =" INSERT INTO tblBlah(Name,Surname,Occupation)"& _

" VALUES(''" ;& frmTest [0]&"'',''"& frmTest [1]&"'',''"&

frmTest [2]& ;"''')"


设置conn = Server.CreateObject(" adodb .connection")

conn.Open(" DRIVER = {Microsoft Access Driver(* .mdb)}; DBQ = QUOT; +

Server.Mappath(" thedb.mdb"))

conn.Execute(SQLStmt)

conn.Close

设置conn = nothing


3.不需要将

原始代码中的conn.Execute(SQLStmt)分配给RS变量。我是否正确?

4.是conn = null破坏''conn''对象的方法有效吗?

是的,我知道,将sql代码放入存储过程将是

最佳解决方案,但我想知道上面哪种方法更好。$ / b

谢谢。
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.


Andyza写道:
Andyza wrote:

我正在寻找另一个开发人员代码而我现在对此感到困惑br />
关闭数据库连接并销毁对象的正确方法。


我通常会这样做:


设置conn = Server.CreateObject(" adodb.connection")

conn.Execute(UPDATE TABLE Blah Set Something =''Somevalue''WHERE

whatever =' 'blah'')

conn.Close

设置conn =无


这个其他开发者已经通过这种方式关闭了连接:


conn = Server.CreateObject(" adodb.connection")

conn.Execute(UPDATE TABLE Blah Set Something =''Somevalue''WHERE

无论什么='''blah'')

conn .close()

conn = null

1.哪种编码sql更新的方法正确(或更好)?
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?



两者都不:-)

如上所述,上述语句不会运行 - 试试吧。 :-)

好​​的,我接受sql语句周围的引号被遗漏了

无意中。但是,仍然存在问题:

1.您应该将sql语句分配给变量并将

变量传递给Execute方法。它使调试变得更容易。

2.你应该总是指定commandtype参数而不是使用
ADO猜测。通常它会正确猜测命令类型是

Text,但在极少数情况下,它可能会猜错,导致调试时间:

const adCmdText = 1
dim sql

sql =" UPDATE TABLE Blah Set Something =''Somevalue''" &安培; _

" WHERE whatever =''blah''"

conn.Execute sql ,, adCmdText

conn.close:set conn = no


更好的是,您应该使用参数而不是动态sql:
http://groups-beta.google.com/group/...e36562fee7804e

Neither :-)
As written, the above statements will not run - try it. :-)
OK, I accept that the quotes around the sql statements were left out
inadvertantly. However, there are still issues:
1. You should assign your sql statement to a variable and pass the
variable to the Execute method. It makes debugging easier.
2. You should always specify the commandtype argument instead of making
ADO guess. Usually it will guess correctly that the command type is
Text, but in rare cases, it can guess wrong, leading hours of debugging:
const adCmdText = 1
dim sql
sql="UPDATE TABLE Blah Set Something = ''Somevalue'' " & _
"WHERE whatever = ''blah''"
conn.Execute sql,,adCmdText
conn.close: set conn=nothing

Better yet, you should use parameters instead of dynamic sql:
http://groups-beta.google.com/group/...e36562fee7804e


2.是conn = null吗?破坏''conn''对象的方法有效吗?
2. Is the "conn = null" method of destroying the ''conn'' object valid?



不在vbscript中。在jscript中,是的。

Not in vbscript. In jscript, yes.


>


我继承的代码示例如下:

db =" DRIVER = {Microsoft Access Driver(* .mdb)}; DBQ = QUOT; +

Server.Mappath(" thedb.mdb")

conn = Server.CreateObject(" adodb.connection")

conn 。打开(db)

SQLStmt =" INSERT INTO tblBlah(姓名,姓氏,职业)"

SQLStmt = SQLStmt +" VALUES(' '" + frmTest [0] +"''" +",''" + frmTest [1]

+"''" +",''" ; + frmTest [2] +"''"


RS = conn.Execute(SQLStmt)

RS = null

conn.close()

conn = null


我的编码方式如下:


Dim SQLStmt,db,conn

SQLStmt =" INSERT INTO tblBlah(Name,Surname,Occupation)"& _

" VALUES(''" ;& frmTest [0]&"'',''"& frmTest [1]&"'',''"&

frmTest [2]& ;"''')"


设置conn = Server.CreateObject(" adodb .connection")

conn.Open(" DRIVER = {Microsoft Access Driver(* .mdb)}; DBQ = QUOT; +

Server.Mappath(" thedb.mdb"))

conn.Execute(SQLStmt)

conn.Close

设置conn = nothing


3.不需要将

原始代码中的conn.Execute(SQLStmt)分配给RS变量。我是否正确?

4.是conn = null破坏''conn''对象的方法有效吗?

是的,我知道,将sql代码放入存储过程将是

最佳解决方案,但我想知道上面哪种方法更好。


谢谢。
>

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.



-

Microsoft MVP - ASP / ASP.NET

请回复新闻组。我的From

标题中列出的电子邮件帐户是我的垃圾邮件陷阱,因此我不经常检查它。通过发布到新闻组,您将获得更快的回复。

--
Microsoft MVP -- ASP/ASP.NET
Please reply to the newsgroup. The email account listed in my From
header is my spam trap, so I don''t check it very often. You will get a
quicker response by posting to the newsgroup.


2月13日下午3:37,Bob Barrows [MVP] ]" < reb01 ... @ NOyahoo.SPAMcom>

写道:
On Feb 13, 3:37 pm, "Bob Barrows [MVP]" <reb01...@NOyahoo.SPAMcom>
wrote:

1.您应该将sql语句分配给变量并传递执行方法的

变量。它使调试更容易。
1. You should assign your sql statement to a variable and pass the
variable to the Execute method. It makes debugging easier.



这不是我用conn.Execute(SQLStmt)

行所做的事情吗?

将sql语句分配给''SQLStmt''变量然后

conn.Execute(SQLStmt)执行SQLStmt变量中的值,即:


SQLStmt =" INSERT INTO tblBlah(Name,Surname,Occupation)" &安培; _

" VALUES(''"& frmTest [0]&"'',''"& frmTest [1]&"'',''" ;&

frmTest [2]&"'')"


< snip>


conn.Execute(SQLStmt)


这不是你说的我应该做的吗?

Isn''t that what I''ve already done with the conn.Execute(SQLStmt)
line?
The sql statement is assigned to the ''SQLStmt'' variable and then
conn.Execute(SQLStmt) executes the value in the SQLStmt variable, ie:

SQLStmt = "INSERT INTO tblBlah (Name,Surname,Occupation) " & _
"VALUES (''" & frmTest[0] & "'', ''" & frmTest[1] & "'', ''" &
frmTest[2] & "'')"

<snip>

conn.Execute(SQLStmt)

Isn''t this what you were saying I should do?


这篇关于关闭数据库连接的正确方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆