像Microsoft文档中的示例那样不处理SQLCommand是正确的吗?
sqlcommand类给出以下不处理SQLCommand的示例代码。
SQLCommand从实现Idisposable实现的DBCommand继承。
常识表明,建议处置SQLCommand,但是示例不是。
这个例子是提供最佳实践吗?
private static void ReadOrderData(string connectionString)
{
string queryString =
"SELECT OrderID, CustomerID FROM dbo.Orders;";
using (SqlConnection connection = new SqlConnection(
connectionString))
{
SqlCommand command = new SqlCommand(
queryString, connection);
connection.Open();
using(SqlDataReader reader = command.ExecuteReader())
{
while (reader.Read())
{
Console.WriteLine(String.Format("{0}, {1}",
reader[0], reader[1]));
}
}
}
}
The Microsoft Documentation for the SqlCommand class gives the following example code which does not dispose of the SqlCommand.
SqlCommand inherits from DbCommand which implements IDisposable.
Common knowledge would suggest it's advisable to dispose of the SqlCommand, however, the example does not.
Is the example providing a best practice?
private static void ReadOrderData(string connectionString)
{
string queryString =
"SELECT OrderID, CustomerID FROM dbo.Orders;";
using (SqlConnection connection = new SqlConnection(
connectionString))
{
SqlCommand command = new SqlCommand(
queryString, connection);
connection.Open();
using(SqlDataReader reader = command.ExecuteReader())
{
while (reader.Read())
{
Console.WriteLine(String.Format("{0}, {1}",
reader[0], reader[1]));
}
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在您的代码中查看我的评论
命令
在Connection 的范围中声明了,即try/catch/the/catch/fine
命令不一定持有连接之外的任何资源。您处置了连接,该连接已在读者处置时已经关闭。 (请参阅
commandberhavior
)。因此,最终,命令
是对处置资源引用的对象。更了解,请使用一些反射工具,然后看内部。
那么,这是什么元数据?可能是一些参数信息。但是,如果您不添加参数,那么您可能无需处理。 Microsoft知道这一点,并发现不必包括
在
。这是为了回答,为什么Microsoft不使用为命令
上删除命令
包括。看一下构造函数,
看上去一开始没有任何可供处理。所有这些“缓存物品”将正常收集。我在任何地方没有发现任何IO或其他不受管理的资源。
但是,要记住的一件事是,实现可能会因版本而变化,而您不想更改代码。因此,添加另一层以命令
这将有可能节省一些喧嚣。
有趣的事实:使用
mySQL
提供商在sqlclient
上实现相同的接口和基类的提供程序时,mysqlcommand.dispose < /代码>总是关闭读者。使用
使用的同一代码将与SQLCLIENT和ORACLE ODP一起使用,而不是MySQL。我必须包装
mySqlCommand
,并在提供者 - 不合理的代码中使用我的自定义类(想想 decorator 模式)来覆盖行为。See my comments in your code first
command
was declared in the scope of connectionusing
, i.e.try/catch/finally
Command is not necessarily holding any resources outside of connection. And you disposing connection, which already closed when reader is disposed. (see
CommandBerhavior
). So, in the end,command
is the object that holds references to disposed resources.To know better, use some reflection tool and look inside.
So, what is this metadata? Probably some Parameter info. But if you don't add parameters then you might have nothing to dispose. Microsoft knows this and found unnecessary to include
dispose
oncommand
. This is to answer, why Microsoft did not includeusing
forcommand
.Look at the constructor
Looks like there is nothing to dispose in the first place. And all those "cached items" will be garbage collected normally. I did not find there any IO or other unmanaged resources anywhere there.
One thing however, to remember, is that implementation might change from version to version and you don't want to change the code. So, add another layer to command
This will potentially save some hustle.
Fun fact: When working with
MySql
provider which implements same interfaces and base classes asSqlClient
, there was a bug (still is probably) in theMySqlCommand.Dispose
which closed the reader always. And this same code withusing
would work with SqlClient and Oracle ODP but not MySql. I had to wrapMySqlCommand
and use my custom class (think of Decorator pattern) in the provider-agnostic code to overwrite the behavior.