- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
您好,我正在尝试从一个方法中返回 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/
有谁知道 DbDataReaders 是如何工作的。我们可以以SqlDataReader为例。 当您执行以下操作时 cmd.CommandText = "SELECT * FROM Customers
有什么巧妙的方法可以简化这种下流话吗?它有效,但不可读... int? myVal = r.IsDBNull(r.GetOrdinal("colName")) ? (int?)null : r.Get
根据 MSDN , System.Data.SqlClient支持从 DataTable 填充表值参数, DbDataReader或 IEnumerable对象。 我编写了以下代码,使用 IEnume
这个问题是我的 previous 的延续一。无需过多介绍,我将使用 2 个相关的一对多表填充数据集。所以,我现在的问题是 - 为什么这段代码运行良好 public DataAgencyR_DataSe
如何让dr.Read();在满足条件的情况下重新从头开始阅读? 类似于: SqlDataReader dr = command.ExecuteReader(); for(int i=0; dr.Rea
在下面的代码中,command是一个已经设置好的DbCommand: using( var dataReader = command.ExecuteReader() /*The actual exec
考虑下面的 FetchData 方法。它旨在避免每次您想从数据库中获取一些数据时重复数据库查询代码。 List myData = new List { myData.Add(new
我在许多示例中看到了 IDataReader 而不是 DbDataReader 的用法(以及其他 ADO.NET 接口(interface)而不是其相应类的用法)。 我知道 IDataReader
我有很多使用原始 ADO.NET(DbConnection、DbDataReader 等)的现有代码。我想过渡到对新代码使用 LINQ to SQL,但现在将现有代码和新代码都放在一组统一的 Repo
我试图让 Entity Framework 返回自定义查询的多个结果,然后将其映射回 Entity Framework 代码优先模型。是否可以使用Entity Framework的内部映射引擎? 我可
如何转换 List personList至 DBDataReader ? 在我下面给出的代码中,我试图批量插入 personList .我有大约 50 万条记录,方法 WriteToServer需要一
我目前正在实现一个可以从不同数据库读取数据的小软件。以下是代码: interface Fetch { Dictionary GetData(); } abstract class Conn :
偶然发现了一段相对常用的代码,起初看起来效率很低。 (我知道有时优化可能是邪恶的,但我想知道) 介绍部分 - 相当简单的 SP 执行 + 读取返回的数据: try { await connec
我有一个简单的应用程序,需要执行某些查询来获取数据库架构信息。我写了一个简单的方法来执行查询并返回一个阅读器,就像这样 - public static DbDataReader ExecuteQuer
我有以下函数,它允许我传入一个对象并用返回数据(如果有)填充该对象。 我修改了该函数,使其可以异步调用。 public static async Task MySQLReturnReader(stri
我的属性绑定(bind)数据访问类有一个小问题(更像是一个烦恼)。问题是当类中相应属性的读取器中不存在列时映射失败。 代码 这是映射器类: // Map our datareader object t
我在 MS reference 查看 DbDataReader(也称为 DbCommand)的代码并且无法弄清楚 ReadAsync() 方法中的异步是什么。 virtual public T
您好,我正在尝试从一个方法中返回 DataReader,但它返回一个关闭的 DbDataReader 对象。解决这个问题的任何想法。我愿意接受任何改进代码的建议。 谢谢 更新我不想让数据库连接保持打开
我通过以下方式调用 EF Core 2.0 中的存储过程。 private async Task> InvokeStoredProcedureAsync(string entityName) {
我是一名优秀的程序员,十分优秀!