关于Dispose的问题 [英] Question about Dispose

查看:146
本文介绍了关于Dispose的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我的理解是Dispose()不应该用于销毁

连接对象,而且首选Close()。


但是,在MS的快速启动应用程序之一中,我看到这个被使用....


这可以在SqlHelper中找到class -


Dim cn As New SqlConnection(connectionString)

试试

cn.Open()

''调用连接代替连接取代连接

string

返回ExecuteScalar(cn,commandType,commandText,commandParameters)

最后

cn.Dispose()

结束尝试


这是正确的方法吗?


Bob Lehmann

解决方案



" Bob Lehmann" <无>在消息中写道

新闻:好的************** @ TK2MSFTNGP10.phx.gbl ...


我的理解是Dispose()不应该用于破坏
连接对象,而且首选Close()。

然而,在其中一个MS'' s快速启动应用程序我看到这个被使用....

这是在SqlHelper类中找到的 -

Dim cn As New SqlConnection(connectionString)
试试
cn.Open()
''调用连接取代连接的重载
字符串
返回ExecuteScalar(cn,commandType,commandText,commandParameters)
最后
cn.Dispose()
结束尝试

这是正确的方法吗?

Bob Lehmann



这样做是因为Open方法在Try块内,在Finally

块中你不知道连接是否成功打开。什么

如果连接打开但是连接本身有错误?如果你打电话给

关闭你可能会收到错误,或者任何其他方法可能会抛出异常。


Dispose确保连接正确关闭之前对象是被毁坏的
。这是一种适当的技术,用于确保

连接是否已打开,它将在Try结束之前关闭...最后

块已被关闭。


有意义吗?


Mythran


<" Bob莱曼" <无>>写道:

我的理解是Dispose()不应该用于销毁
连接对象,而且首选Close()。

然而,在MS的Quickstart应用程序中的一个我看到这个被使用....

这可以在SqlHelper类中找到 -

Dim cn As New SqlConnection(connectionString)<尝试
cn.Open()
''调用连接取代连接的重载
字符串
返回ExecuteScalar(cn,commandType,commandText,commandParameters )
最后
cn.Dispose()
结束尝试

这是正确的方法吗?




我相信这是正确的。我不认为

调用Close()调用Dispose()有什么好处。


-

Jon Skeet - < sk *** @ pobox.com>
http://www.pobox.com/~skeet

如果回复小组,请不要给我发邮件


嗨Bob Lehmann,


你是对的。


Dispose()方法释放与连接相关的所有资源

对象(包括从连接池中删除它)。不是一个好的做法

调用Dispose(),除非你想从

连接池中删除连接。


问候

Ashish M Bhonkiya


" Bob Lehmann" <无>在消息中写道

新闻:好的************** @ TK2MSFTNGP10.phx.gbl ...


我的理解是Dispose()不应该用于破坏
连接对象,而且首选Close()。

然而,在其中一个MS'' s快速启动应用程序我看到这个被使用....

这是在SqlHelper类中找到的 -

Dim cn As New SqlConnection(connectionString)
试试
cn.Open()
''调用连接取代连接的重载
字符串
返回ExecuteScalar(cn,commandType,commandText,commandParameters)
最后
cn.Dispose()
结束尝试

这是正确的方法吗?

Bob Lehmann


Hi,

My understanding is that Dispose() should not be used for destroying a
connection object, and that Close() is preferred.

However, in one of MS''s Quickstart Apps I see this being used....

This is found in the SqlHelper class -

Dim cn As New SqlConnection(connectionString)
Try
cn.Open()
''call the overload that takes a connection in place of the connection
string
Return ExecuteScalar(cn, commandType, commandText, commandParameters)
Finally
cn.Dispose()
End Try

Is this the correct way to do this?

Bob Lehmann

解决方案


"Bob Lehmann" <none> wrote in message
news:OK**************@TK2MSFTNGP10.phx.gbl...

Hi,

My understanding is that Dispose() should not be used for destroying a
connection object, and that Close() is preferred.

However, in one of MS''s Quickstart Apps I see this being used....

This is found in the SqlHelper class -

Dim cn As New SqlConnection(connectionString)
Try
cn.Open()
''call the overload that takes a connection in place of the connection
string
Return ExecuteScalar(cn, commandType, commandText, commandParameters)
Finally
cn.Dispose()
End Try

Is this the correct way to do this?

Bob Lehmann



This is done because the Open method is inside the Try block and in the Finally
block you have no idea whether or not the connection opened successfully. What
if the connection opened but there''s a bug in the connection itself? If you call
close you may get an error, or any other method may throw an exception.

Dispose makes sure the connection is properly closed prior to the object being
destroyed. This is a proper technique used to ensure that whether or not the
connection was opened, it will be closed before the end of the Try...Finally
block has been closed.

Make sense?

Mythran


<"Bob Lehmann" <none>> wrote:

My understanding is that Dispose() should not be used for destroying a
connection object, and that Close() is preferred.

However, in one of MS''s Quickstart Apps I see this being used....

This is found in the SqlHelper class -

Dim cn As New SqlConnection(connectionString)
Try
cn.Open()
''call the overload that takes a connection in place of the connection
string
Return ExecuteScalar(cn, commandType, commandText, commandParameters)
Finally
cn.Dispose()
End Try

Is this the correct way to do this?



I believe this is correct. I don''t think there''s any advantage in
calling Close() over calling Dispose().

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


Hi Bob Lehmann,

You are correct.

The Dispose() method releases all resources associated to the connection
object (including removing it from the connection pool). Not a good practise
to call Dispose() unless you want connection to be removed from the
connection pool.

Regards
Ashish M Bhonkiya

"Bob Lehmann" <none> wrote in message
news:OK**************@TK2MSFTNGP10.phx.gbl...

Hi,

My understanding is that Dispose() should not be used for destroying a
connection object, and that Close() is preferred.

However, in one of MS''s Quickstart Apps I see this being used....

This is found in the SqlHelper class -

Dim cn As New SqlConnection(connectionString)
Try
cn.Open()
''call the overload that takes a connection in place of the connection
string
Return ExecuteScalar(cn, commandType, commandText, commandParameters)
Finally
cn.Dispose()
End Try

Is this the correct way to do this?

Bob Lehmann



这篇关于关于Dispose的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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