2种关闭连接的方法 [英] 2 ways to close a connection

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

问题描述

在我的Finally块中,我使用了cn.close(其中cn是一个ADO.NET

连接对象,确切地说是SQLConnection)然后我遇到了

跟随一些微软代码。


如果不是cn什么都没有那么

CType(cn,IDisposable).Dispose()

结束如果


我不得不承认,我不知道这里发生了什么。有人会向我解释这个

代码行(中间的那个,而不是if语句LOL)吗?

[希望我不会因此而受到抨击: - )]


Eric

In my Finally block, I was using cn.close (where cn is an ADO.NET
connection object, SQLConnection to be exact) and then I came across the
following in some microsoft code.

If Not cn Is Nothing Then
CType(cn, IDisposable).Dispose()
End If

I have to admit, I''m not sure what happens here. Will someone explain this
line of code (the middle one, not the if statement LOL) to me please?
[hopefully I won''t get flamed for this :-) ]

Eric

推荐答案

Eric,

将连接转换为IDisposable类型,然后调用Dispose

方法以释放非托管资源。当你调用Dispose()时,

当呼叫返回时完成所有必要的清理,而不是等待GC上的
,当它认为有必要时不确定点

将来。


Raymond Lewallen


" Eric Sabine" < mopar41 @___ ho_y_tmail.ScPoAmM>在消息中写道

新闻:#8 ************* @tk2msftngp13.phx.gbl ...
Eric,

The connection is converted to a type IDisposable and then the Dispose
method is called to release unmanaged resources. When you call Dispose(),
all the necessary cleanup is done when the call returns instead of waiting
on the GC to do it when it feels it is necessary at an indeterminate point
in the future.

Raymond Lewallen

"Eric Sabine" <mopar41@___ho_y_tmail.ScPoAmM> wrote in message
news:#8*************@tk2msftngp13.phx.gbl...
在我的Finally块中,我正在使用cn.close(其中cn是一个ADO.NET
连接对象,确切地说是SQLConnection)然后我在一些微软代码中看到了
以下内容。

如果没有cn什么也没有那么
CType(cn,IDisposable).Dispose()
结束如果

我不得不承认,我不知道这里发生了什么。有人会向我解释
这行代码(中间的那个,而不是if语句LOL)吗?
[希望我不会因此而受到抨击:-)]

Eric
In my Finally block, I was using cn.close (where cn is an ADO.NET
connection object, SQLConnection to be exact) and then I came across the
following in some microsoft code.

If Not cn Is Nothing Then
CType(cn, IDisposable).Dispose()
End If

I have to admit, I''m not sure what happens here. Will someone explain this line of code (the middle one, not the if statement LOL) to me please?
[hopefully I won''t get flamed for this :-) ]

Eric



谢谢。你说的是有道理的。如果我使用过cn.close,我只需要垃圾收集器的支配就可以在需要的时候清理它。这里

是2个问题。


1)让GC管理资源不是更好,或者我应该覆盖

it?

2)我注意到SQLConnection对象有自己的Dispose方法。为什么

你认为它不会在这里使用吗?


Eric


Raymond Lewallen <太阳神****************** @ nospam.faa.gov>在消息中写道

新闻:%2 **************** @ TK2MSFTNGP12.phx.gbl ...
Thank you. What you said makes sense. If I had used cn.close, I am simply
at the mercy of the garbage collector to clean it up when it wants. Here
are 2 questions back to you.

1) is it not better to let the GC manage the resouces, or should I override
it?
2) I notice that the SQLConnection object has its own Dispose method. Why
do you suppose it''s not used here?

Eric

"Raymond Lewallen" <Ra******************@nospam.faa.gov> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
Eric,

连接转换为IDisposable类型,然后调用Dispose
方法释放非托管资源。当你调用Dispose()时,
当呼叫返回时完成所有必要的清理,而不是在GC上等待它,当它认为有必要在一个不确定的点时
未来。

Raymond Lewallen

Eric Sabine < mopar41 @___ ho_y_tmail.ScPoAmM>在消息中写道
新闻:#8 ************* @tk2msftngp13.phx.gbl ...
Eric,

The connection is converted to a type IDisposable and then the Dispose
method is called to release unmanaged resources. When you call Dispose(),
all the necessary cleanup is done when the call returns instead of waiting
on the GC to do it when it feels it is necessary at an indeterminate point
in the future.

Raymond Lewallen

"Eric Sabine" <mopar41@___ho_y_tmail.ScPoAmM> wrote in message
news:#8*************@tk2msftngp13.phx.gbl...
在我的Finally块中,我使用的是cn .close(其中cn是一个ADO.NET
连接对象,确切地说是SQLConnection)然后我在一些微软代码中看到了
以下内容。

如果不是cn是什么都没有
CType(cn,IDisposable).Dispose()
结束如果

我不得不承认,我不知道这里发生了什么。有人会解释
In my Finally block, I was using cn.close (where cn is an ADO.NET
connection object, SQLConnection to be exact) and then I came across the
following in some microsoft code.

If Not cn Is Nothing Then
CType(cn, IDisposable).Dispose()
End If

I have to admit, I''m not sure what happens here. Will someone explain


这个

代码行(中间的那个,而不是if语句LOL)请给我?
[希望我不会得到为此:-)]

Eric
line of code (the middle one, not the if statement LOL) to me please?
[hopefully I won''t get flamed for this :-) ]

Eric




CType(cn ,IDisposable).Dispose()


Treat(一个对象,作为IDisposable接口)。并在

结果上调用Dispose方法。


如果cn是一个指向ADO.NET连接对象的变量,下面的代码

将起作用,因为ADO.NET连接对象实现了IDisposable

interface'的Dispose方法作为你可以直接调用的公共方法。


cn.Dispose()


-

Mike McIntyre

Visual Basic MVP
www .getdotnetcode.com


" Eric Sabine" < mopar41 @___ ho_y_tmail.ScPoAmM>在消息中写道

新闻:%2 *************** @ tk2msftngp13.phx.gbl ...
CType(cn, IDisposable).Dispose()

Treat(an object, as an IDisposable interface).and call Dispose method on the
result.

If cn is a variable that points to an ADO.NET connection object the code
below will work because ADO.NET connection objects implement the IDisposable
interface''s Dispose method as a public method you can call directly.

cn.Dispose()

--
Mike McIntyre
Visual Basic MVP
www.getdotnetcode.com

"Eric Sabine" <mopar41@___ho_y_tmail.ScPoAmM> wrote in message
news:%2***************@tk2msftngp13.phx.gbl...
在我的最后块,我正在使用cn.close(其中cn是一个ADO.NET
连接对象,确切地说是SQLConnection)然后我在一些微软代码中看到了
以下内容。
CType(cn,IDisposable).Dispose()
结束如果

我不得不承认,我不知道这里发生了什么。有人会向我解释
这行代码(中间的那个,而不是if语句LOL)吗?
[希望我不会因此而受到抨击:-)]

Eric
In my Finally block, I was using cn.close (where cn is an ADO.NET
connection object, SQLConnection to be exact) and then I came across the
following in some microsoft code.

If Not cn Is Nothing Then
CType(cn, IDisposable).Dispose()
End If

I have to admit, I''m not sure what happens here. Will someone explain this line of code (the middle one, not the if statement LOL) to me please?
[hopefully I won''t get flamed for this :-) ]

Eric



这篇关于2种关闭连接的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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