关于'使用'和资源的问题 [英] Question about 'Using' and resources

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

问题描述

我正在从VB6迁移并有一个关于使用''使用''和

最好的方式来使用它的问题。


这里是一小段代码的例子:


dbConx(" open")

使用CN

Dim CMD As新的OleDbCommand(sSQL,CN)

Dim DR As OleDbDataReader = CMD.ExecuteReader()


如果DR.HasRows = True那么

Dim vCID As New Collection()

而DR.Read()

vCID.Add(DR.GetInt32(0))

结束时

DR.Close()

CMD.Dispose()

dbConx(" close")

Else

DR.Close()

CMD.Dispose()

dbConx(" close")

返回False

结束如果

结束使用


dbConx创建OleDbConnection CN并打开/关闭它。这个目的

是返回0或更多记录号以供以后使用。如果返回零

记录,我只退出该函数,返回false。

如果找到记录,将它们收集到一个集合中并继续,关闭

使用的所有资源。


在VB6中,我使用了set x = nothing。清理。


- 在使用区块内清理什么?

- 我的CMD和DR对象是否被清理干净CN(所以不需要

需要所有.close和.dispose行吗?

- 有什么更干净的方法吗?


谢谢!

解决方案

Mg,


只要您使用表格或这样的事情,而不是很多关于清理的事情。

(处置是其中的组成部分)

>
VB.Net是托管代码组中的一种编程语言。管理

代码是特殊的,可以克服所有清理问题。只有非托管

*资源*没有被托管代码清理。


在你显示的system.data类中,没有一块非托管的

资源,因此没有任何内容其中必须手工处理。


使用是安静的标准,它正在使用最后处理。

这可能是必需的,但正如我写的不是system.data类。


Cor


" mg" < bi ***** @ gmail.comschreef in bericht

news:11 ********************** @ 74g2000cwt。 googlegro ups.com ...


我正在从VB6迁移并对使用''使用'和
$ b $有疑问b使用它的最佳方式。


以下是一小段代码示例:


dbConx(" open")

使用CN

将CMD视为新的OleDbCommand(sSQL,CN)

Dim DR作为OleDbDataReader = CMD.ExecuteReader()


如果DR.HasRows = True则

Dim vCID As New Collection()

而DR.Read()

vCID.Add(DR.GetInt32(0))

结束时

DR.Close()

CMD.Dispose()

dbConx(" close")

Else

DR.Close()

CMD.Dispose()

dbConx(" close")

返回False

结束如果

结束使用


dbConx创建一个OleDbConnection CN并打开/关闭它。这个目的

是返回0或更多记录号以供以后使用。如果返回零

记录,我只退出该函数,返回false。

如果找到记录,将它们收集到一个集合中并继续,关闭

使用的所有资源。


在VB6中,我使用了set x = nothing。清理。


- 在使用区块内清理什么?

- 我的CMD和DR对象是否被清理干净CN(所以不需要

需要所有.close和.dispose行吗?

- 有什么更干净的方法吗?


谢谢!



见内联。


mg写道:


我正在从VB6迁移,并且有一个关于使用''使用''和

最佳使用方式的问题。


以下是一小段代码示例:


dbConx(打开)

使用CN

Dim CMD As New OleDbCommand(sSQL,CN)

Dim DR As OleDbDataReader = CMD.ExecuteReader()


如果DR.HasRows = True然后

Dim vCID As New Collection()

而DR.Read()

vCID.Add(DR.GetInt32(0))

结束时

DR.Clo se()

CMD.Dispose()

dbConx(" close")

否则

DR。关闭()

CMD.Dispose()

dbConx(" close")

返回False

结束如果

结束使用


dbConx创建OleDbConnection CN并打开/关闭它。这个目的

是返回0或更多记录号以供以后使用。如果返回零

记录,我只退出该函数,返回false。

如果找到记录,将它们收集到一个集合中并继续,关闭

使用的所有资源。


在VB6中,我使用了set x = nothing。清理。


- 在使用块中清理哪些内容?



''使用''关键字将在块结束时自动生成对

的Dispose调用。你可以肯定的唯一一点就是

它会影响''Using''表达式中指定的对象。但是,在你的

的情况下,它可能会处理任何与OleDbConnection相关的OleDbCommand和

OleDbDataReader对象。

但是,''这是OleDbConnection的一个实现细节。


- 我的CMD和DR对象是否与CN一起被清理(所以不需要
需要全部.close和.dispose行?



我可能会说,但是用''使用'来包装它们绝不会伤害

关键字。


- 有什么更干净的方法吗?



好​​吧,它我不清楚dbConx(open)做了什么以及CN在哪里创建了
。我通常用

'内联创建OleDbConnection对象''使用''构造那样,它的范围仅限于那个区块。


>

谢谢!


Cor,


查看我的评论内联。


Brian


Cor Ligthert [MVP]写道:


Mg,


只要您使用某种形式或类似的东西那不仅仅是为了清理而不是很多。

(处置是其中的组成部分)


VB.Net是托管代码组中的一种编程语言。管理

代码是特殊的,以克服所有清洁问题。托管代码不会清除非托管的

*资源*。


在您显示的system.data类中,没有一块unmanaged

资源,因此不需要手工处理。



我们都知道GC最终会为我们做

时终结器自动运行。但是,问题在于何时发生b
。而且我会说这些对象中的某些地方可能存在非托管资源

。他们实现

IDisposable的事实是一个非常好的线索。


使用是安静的标准,因此它最终使用dispose 。

这可能是需要的,但是我写的不是system.data类。



调用Dispose(或使用''对于System.Data中的类,建议使用''keyword)绝对是
。如果你不是那么一个

数据库连接可以保持打开,直到GC运行,这可能需要一段时间。


Cor


I''m migrating from VB6 and have a question about using ''Using'' and the
best way to use it.

Here is a example of a small bit of code:

dbConx("open")
Using CN
Dim CMD As New OleDbCommand(sSQL, CN)
Dim DR As OleDbDataReader = CMD.ExecuteReader()

If DR.HasRows = True Then
Dim vCID As New Collection()
While DR.Read()
vCID.Add(DR.GetInt32(0))
End While
DR.Close()
CMD.Dispose()
dbConx("close")
Else
DR.Close()
CMD.Dispose()
dbConx("close")
Return False
End If
End Using

dbConx creates an OleDbConnection CN and opens/closes it. The purpose
of this is to return 0 or more record numbers for later use. If zero
records are returned I just exit the function with a return of false.
If records found, gather them into a collection and carry on, closing
all resources used.

In VB6 I used "set x=nothing" to clean-up.

-What gets cleaned up within a ''Using'' block?
-Does my CMD and DR objects get cleaned up along with CN (so not
needing all the .close and .dispose lines?
-Any cleaner way to do this?

Thanks!

解决方案

Mg,

As long as you use a form or things like that, than don''t botter to much
about cleaning up.
(The dispose is in the component part of that)

VB.Net is a programming language inside the managed code group. That managed
code is special to overcome all cleaning troubles. Only unmanaged
*resources* are not cleaned up by the managed code.

In the system.data class that you show, there is not one piece of unmanaged
resource and therefore nothing of that has to be disposed by hand.

The using is quiet standard, it is using dispose consequently at the end.
That can be needed, but as I wrote not for the system.data classes.

Cor

"mg" <bi*****@gmail.comschreef in bericht
news:11**********************@74g2000cwt.googlegro ups.com...

I''m migrating from VB6 and have a question about using ''Using'' and the
best way to use it.

Here is a example of a small bit of code:

dbConx("open")
Using CN
Dim CMD As New OleDbCommand(sSQL, CN)
Dim DR As OleDbDataReader = CMD.ExecuteReader()

If DR.HasRows = True Then
Dim vCID As New Collection()
While DR.Read()
vCID.Add(DR.GetInt32(0))
End While
DR.Close()
CMD.Dispose()
dbConx("close")
Else
DR.Close()
CMD.Dispose()
dbConx("close")
Return False
End If
End Using

dbConx creates an OleDbConnection CN and opens/closes it. The purpose
of this is to return 0 or more record numbers for later use. If zero
records are returned I just exit the function with a return of false.
If records found, gather them into a collection and carry on, closing
all resources used.

In VB6 I used "set x=nothing" to clean-up.

-What gets cleaned up within a ''Using'' block?
-Does my CMD and DR objects get cleaned up along with CN (so not
needing all the .close and .dispose lines?
-Any cleaner way to do this?

Thanks!



See inline.

mg wrote:

I''m migrating from VB6 and have a question about using ''Using'' and the
best way to use it.

Here is a example of a small bit of code:

dbConx("open")
Using CN
Dim CMD As New OleDbCommand(sSQL, CN)
Dim DR As OleDbDataReader = CMD.ExecuteReader()

If DR.HasRows = True Then
Dim vCID As New Collection()
While DR.Read()
vCID.Add(DR.GetInt32(0))
End While
DR.Close()
CMD.Dispose()
dbConx("close")
Else
DR.Close()
CMD.Dispose()
dbConx("close")
Return False
End If
End Using

dbConx creates an OleDbConnection CN and opens/closes it. The purpose
of this is to return 0 or more record numbers for later use. If zero
records are returned I just exit the function with a return of false.
If records found, gather them into a collection and carry on, closing
all resources used.

In VB6 I used "set x=nothing" to clean-up.

-What gets cleaned up within a ''Using'' block?

The ''Using'' keyword will automatically generate the call to Dispose at
the end of the block. The only thing you can say for certain is that
it affects the object specified in the ''Using'' expression. In your
case, however, it probably disposes any OleDbCommand and
OleDbDataReader objects associated with the OleDbConnection as well.
But, that''s really an implementation detail of OleDbConnection.

-Does my CMD and DR objects get cleaned up along with CN (so not
needing all the .close and .dispose lines?

I''d say probably, but it never hurts to wrap those with the ''Using''
keyword as well.

-Any cleaner way to do this?

Well, it''s not clear to me what dbConx("open") does and where CN is
created. I usually create the OleDbConnection object inline with the
''Using'' construct that way its scope is limited to just that block.

>
Thanks!


Cor,

See my comments inline.

Brian

Cor Ligthert [MVP] wrote:

Mg,

As long as you use a form or things like that, than don''t botter to much
about cleaning up.
(The dispose is in the component part of that)

VB.Net is a programming language inside the managed code group. That managed
code is special to overcome all cleaning troubles. Only unmanaged
*resources* are not cleaned up by the managed code.

In the system.data class that you show, there is not one piece of unmanaged
resource and therefore nothing of that has to be disposed by hand.

We all know that the GC will eventually do it for us automatically when
the finalizer runs. But, the issue is with the timing of when that
happens. And I''d say it''s likely that there are unmanaged resources
involved somewhere in those objects. The fact that they implement
IDisposable is a pretty good clue.

The using is quiet standard, it is using dispose consequently at the end.
That can be needed, but as I wrote not for the system.data classes.

Calling Dispose (or using the ''Using'' keyword) is definitely
recommended for the classes in System.Data. If you don''t then a
database connection may be held open until the GC runs and that may
take awhile.

Cor


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

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