简体   繁体   English

处置后使用SqlDataReader记录

[英]Use of SqlDataReader Records After Disposal

I would like to retrieve all System.Data.IDataRecord s from a SqlDataReader . 我想从SqlDataReader检索所有System.Data.IDataRecord With LINQ, this is simple, but I'd like to be able to dispose of all SQL objects and keep the data intact. 使用LINQ,这很简单,但我希望能够处理所有SQL对象并保持数据完好无损。 Below is what I have: 以下是我所拥有的:

private static List<System.Data.IDataRecord> getRecords()
{
    List<System.Data.IDataRecord> records;
    //Make a SqlConnection and SqlCommand.
    using (SqlDataReader reader = command.ExecuteReader())
    {
        records = reader.Cast<System.Data.IDataRecord>().ToList();
    }
    //Dispose of SqlCommand and SqlConnection.
    return records;
}

But here is my question: When getRecords() is finished, I have my data, and I have asked for all SQL objects to be disposed. 但这是我的问题:当getRecords()完成后,我有我的数据,并且我已经要求处理所有SQL对象。 But am I somehow hanging onto data which will prevent proper resource disposal? 但我是否会以某种方式挂在数据上,以防止适当的资源处置? Or will my data eventually become invalid once the garbage collector does its work? 或者,一旦垃圾收集器完成其工作,我的数据最终会变得无效吗? I think the answer is no, but I am trying to find something definitive. 我认为答案是否定的,但我试图找到确定的东西。 I believe Dispose() will only remove unmanaged resources, and since I placed the data in my List, I would think it is safe. 我相信Dispose()只会删除非托管资源,因为我把数据放在我的列表中,我认为它是安全的。 Thanks. 谢谢。

This will work fine. 这样可以正常工作。

IDataRecord instances are not affected by SqlDataReader.Dispose in any way. IDataRecord实例不受SqlDataReader.Dispose的任何影响。 They contain their own full copy of the data and schema info, completely separate from the reader. 它们包含自己的数据和架构信息的完整副本,与读者完全分开。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM