gpt4 book ai didi

c# - 在 ExecuteReader() 中使用 CommandBehavior.CloseConnection 的用途/优势是什么

转载 作者:可可西里 更新时间:2023-11-01 03:07:08 24 4
gpt4 key购买 nike

谁能告诉我什么是 CommandBehavior.CloseConnection 以及在 com.ExecuteReader(CommandBehavior.CloseConnection) 中将其作为参数传递的用途/好处是什么?

最佳答案

您在读取数据读取器时需要打开连接,并且希望尽快关闭连接。通过指定 CommandBehavior.CloseConnection打电话时 ExecuteReader ,您确保您的代码在关闭数据读取器时关闭连接。

但是您应该已经立即处理您的连接(而不是仅仅关闭它们),在这种情况下,这样做最多只能带来边际(几乎肯定无法衡量)的好处.

例如,此代码会立即关闭其连接( 执行处理它所需的任何其他工作),而无需指定命令行为:

using (SqlConnection connection = new SqlConnection(connectionString))
using (SqlCommand command = new SqlCommand(commandText, connection))
{
connection.Open();
using (SqlDataReader reader = command.ExecuteReader())
{
while (reader.Read())
// Do something with the rows
}
}

关于c# - 在 ExecuteReader() 中使用 CommandBehavior.CloseConnection 的用途/优势是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5689263/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com