我应该Dispose()一个SqlCommand对象吗? [英] Should I Dispose() an SqlCommand object?

查看:66
本文介绍了我应该Dispose()一个SqlCommand对象吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好!


在我使用完SqlCommand类的实例后,我是否应该在实例上调用Dispose()。我想是这样的,因为有一个Dispose

方法,但它实际发布了什么?


我基本上更喜欢跳过调用Dispose()这个将确定何时使用情况已经完成后,我将免费获得



祝你好运,


Henrik Dahl

解决方案

Henrik,


在我看来,这是一个糟糕的设计。一般来说,对于DB操作,至少使用.NET中的模型(断开连接的记录集),您应该打开连接,执行操作并退出。您可以重新创建该命令。另外,你真的想把你的数据库中的命令传递给外面可能以不正当方式使用它的人吗?


所以,鉴于这一切,是的,你应该总是调用Dispose。它所处理的并不重要,IDispose的实现(比如快10倍)的含义是,类管理的资源应该及时处理,而不是等待GC 。


希望这会有所帮助。


-

- Nicholas Paldino [.NET / C#MVP]

- 尼克(d0t)paldino = At-exisconsulting''dot | com


2003年10月6日星期一12:57:18 +0200,Henrik Dahl <钍********************** @ inet.uni2.dk>写道:

你好!

在我使用完SqlCommand类的实例之后,我是否应该调用Dispose()实例。我想是这样,因为有一个Dispose
方法,但它实际上是什么释放?

我基本上更喜欢跳过调用Dispose(),因为这将释放我
确定用法何时完成。

祝你好运,

Henrik Dahl



< blockquote>嗨Henrik,


我不太确定你需要在SqlCommand上调用Dispose(),

SqlCom的Dispose()继承自Component(根据MSDN)

因此它不执行任何与DB相关的操作。

您应该定义关闭连接,关于SqlCommand这个

是ADO.NET指南所说的:


"虽然你可以重复使用相同的SqlCommand对象来执行

相同的命令多个次,不要重复使用相同的SqlCommand对象来执行不同的命令。

希望得到这个帮助,


-

Ignacio Machin,

ignacio.machin AT dot.state.fl.us

佛罗里达州交通局


" Henrik Dahl" <钍********************** @ inet.uni2.dk>在消息中写道

news:ev ************* @ TK2MSFTNGP11.phx.gbl ...

你好!

在使用完SqlCommand类的实例之后,我应该在
上调用实例上的Dispose()。我想是这样,因为有一个Dispose
方法,但它实际上是什么释放?

我基本上更喜欢跳过调用Dispose(),因为这将释放我
确定用法何时完成。

祝你好运,

Henrik Dahl



< blockquote> Hello Ignacio,


有趣,也因为在SqlCommand概述页面上它显示:

"你可以重置CommandText属性并重用SqlCommand对象。

但是,你必须关闭SqlDataReader才能执行新的或

之前的命令。。

祝你好运,


Henrik Dahl


Ignacio Machin(.NET / C#MVP)" < ignacio.machin AT dot.state.fl.us>在消息新闻中写了

:uw ************** @ TK2MSFTNGP12.phx.gbl ...

嗨Henrik,

我不太确定你需要在SqlCommand上调用Dispose(),SqlCommand的Dispose()是从Component继承的(根据MSDN)
因此它不执行任何与DB相关的操作。
你应该定义关闭连接,关于SqlCommand这是ADO.NET指南所说的:

虽然你可以重复使用相同的SqlCommand对象多次执行相同的命令,不要重复使用相同的SqlCommand对象来执行不同的命令。

希望这个帮助,

-
Ignacio Machin,
ignacio.machin at dot.state.fl.us
佛罗里达州交通局

Henrik Dahl <钍********************** @ inet.uni2.dk>在消息中写道
新闻:ev ************* @ TK2MSFTNGP11.phx.gbl ...

你好!

在我使用完SqlCommand类的实例之后,我应该


然后

在实例上调用Dispose()。我想是这样,因为有一个Dispose
方法,但它实际上是什么释放?

我基本上更喜欢跳过调用Dispose(),因为这将释放我
确定用法何时实际完成。

祝你好运,

Henrik Dahl




Hello!

After I''ve finished using an instance of the SqlCommand class, should I then
invoke Dispose() on the instance. I suppose so, as there is a Dispose
method, but what does it actually release?

I would basically prefer to skip invoking Dispose() as this will free me
from determining when the usage actually has finished.
Best regards,

Henrik Dahl

解决方案

Henrik,

This is a bad design, in my opinion. Generally speaking, with DB operations, at least with the model in .NET (disconnected recordsets), you should open your connection, perform your operation, and get out. You can recreate the command. Also, do you really want to be passing around commands to your database to someone on the outside that might use it in an improper manner?

So, given all that, yes, you should always call Dispose. It doesn''t really matter what it disposes, the implication through the implementation (say that 10 times fast) of IDispose is that there is a resource the class manages which should be disposed of in a timely manner as opposed to waiting for a GC.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- nick(d0t)paldino=At-exisconsulting''dot|com

On Mon, 6 Oct 2003 12:57:18 +0200, Henrik Dahl <Th**********************@inet.uni2.dk> wrote:

Hello!

After I''ve finished using an instance of the SqlCommand class, should I then
invoke Dispose() on the instance. I suppose so, as there is a Dispose
method, but what does it actually release?

I would basically prefer to skip invoking Dispose() as this will free me
from determining when the usage actually has finished.
Best regards,

Henrik Dahl



Hi Henrik,

I''m not very sure that you need to call Dispose() on a SqlCommand , the
Dispose() of SqlCommand is inherited from Component ( according to MSDN )
therefore it does not perform any DB related op.
You should definely close the connection , regarding the SqlCommand this
is what the ADO.NET guide says:

"Although you can repeatedly use the same SqlCommand object to execute the
same command multiple times, do not reuse the same SqlCommand object to
execute different commands. "
Hope this help,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Henrik Dahl" <Th**********************@inet.uni2.dk> wrote in message
news:ev*************@TK2MSFTNGP11.phx.gbl...

Hello!

After I''ve finished using an instance of the SqlCommand class, should I then invoke Dispose() on the instance. I suppose so, as there is a Dispose
method, but what does it actually release?

I would basically prefer to skip invoking Dispose() as this will free me
from determining when the usage actually has finished.
Best regards,

Henrik Dahl



Hello Ignacio,

Interesting, also because on the SqlCommand overview page it reads:
"You can reset the CommandText property and reuse the SqlCommand object.
However, you must close the SqlDataReader before you can execute a new or
previous command.".
Best regards,

Henrik Dahl

"Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin AT dot.state.fl.us> wrote
in message news:uw**************@TK2MSFTNGP12.phx.gbl...

Hi Henrik,

I''m not very sure that you need to call Dispose() on a SqlCommand , the
Dispose() of SqlCommand is inherited from Component ( according to MSDN )
therefore it does not perform any DB related op.
You should definely close the connection , regarding the SqlCommand this
is what the ADO.NET guide says:

"Although you can repeatedly use the same SqlCommand object to execute the
same command multiple times, do not reuse the same SqlCommand object to
execute different commands. "
Hope this help,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Henrik Dahl" <Th**********************@inet.uni2.dk> wrote in message
news:ev*************@TK2MSFTNGP11.phx.gbl...

Hello!

After I''ve finished using an instance of the SqlCommand class, should I


then

invoke Dispose() on the instance. I suppose so, as there is a Dispose
method, but what does it actually release?

I would basically prefer to skip invoking Dispose() as this will free me
from determining when the usage actually has finished.
Best regards,

Henrik Dahl




这篇关于我应该Dispose()一个SqlCommand对象吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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