gpt4 book ai didi

c# - DbDataReader 错误 : Invalid attempt to call Read when reader is closed

转载 作者:行者123 更新时间:2023-11-30 14:37:19 29 4
gpt4 key购买 nike

您好,我正在尝试从一个方法中返回 DataReader,但它返回一个关闭的 DbDataReader 对象。解决这个问题的任何想法。我愿意接受任何改进代码的建议。

谢谢

更新我不想让数据库连接保持打开状态。有没有办法在关闭连接后返回打开的 DataReader。

internal DbDataReader ExecuteReader(SqlCommand command, CommandBehavior behavior, string connectionString)
{
DbDataReader dataReader = null;
try
{
SqlConnection connection = GetConnection(connectionString);
Open(connection);
command.Connection = connection;
command.CommandTimeout = 60;
dataReader = command.ExecuteReader(behavior);
Close(connection);
}
catch
{
}
return dataReader;
}

最佳答案

它已关闭,因为您关闭了数据库连接。它无法从关闭的 SqlConnection 中读取数据。如果您想重用该连接,您可以将一个 OPEN 连接传递给该方法,并在您使用 DbDataReader 中的数据后关闭该连接。

关于c# - DbDataReader 错误 : Invalid attempt to call Read when reader is closed,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9381383/

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