当DataReader变空时?

发布于 2024-08-19 08:29:59 字数 367 浏览 1 评论 0原文

我有下面的代码:

if (reader.HasRows)
{
    while (reader.Read())
    {
        DataRow searchedDR = ds.Tables[dr["name"].ToString()].Rows.Find(reader["ID"].ToString());
        if (searchedDR != null)
            ds.Tables[dr["name"].ToString()].Rows.Remove(searchedDR);
    }
}

当这段代码成功完成时,数据读取器(读取器)变空? 原因是什么? 之后我需要与这位读者一起工作。解决办法是什么?

I have the below code :

if (reader.HasRows)
{
    while (reader.Read())
    {
        DataRow searchedDR = ds.Tables[dr["name"].ToString()].Rows.Find(reader["ID"].ToString());
        if (searchedDR != null)
            ds.Tables[dr["name"].ToString()].Rows.Remove(searchedDR);
    }
}

When this block of code done successfully the data reader (reader) become empty?
What is the reason?
I need to work with this reader afterward. What is the solution?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

陈甜 2024-08-26 08:29:59

数据读取器是一束数据。当您消耗它正在读取的所有数据时,它会变得“空”(或者更好的比喻:耗尽)。

它不是存储设备(它不是桶)。如果您之后需要数据,请将其存储在某个地方 - 例如,DataTable 或类模型。

A data-reader is a hose of data. It becomes "empty" (or a better analogy: runs dry) when you consume all the data it is reading.

It is not a storage device (it isn't a bucket). If you want the data afterwards, store it somewhere - a DataTable or a class model, for example.

2024-08-26 08:29:59

IDataReader 定义只读、仅转发的数据提供者。一旦调用 Read() 方法,就无法返回到上一行。由于该接口没有提供 Reset() 方法,因此重新开始的唯一方法是再次执行数据库命令。将读取器视为数据流 - 您可以请求下一行,但一旦读取,它就消失了。

如果需要多次遍历数据库查询的结果,则需要将结果缓存在应用程序内存中。您可以手动执行此操作,对读取器进行初始传递,但使用 IDataAdapter 代替。

The IDataReader defines a readonly, forward only data provider. Once you call the Read() method, there is no way to go back to the previous row. As the interface provides no Reset() method, the only way to start over, is to execute the database command again. Think of the reader as a stream of data - you can ask for the next row, but once read, it's gone.

If you need to traverse the results of a database query several times, you will need to cache the results in application memory. You could do this manually doing the initial pass on the reader, but it would be a lot easier to use an IDataAdapter instead.

我喜欢麦丽素 2024-08-26 08:29:59

DataReader 只能前进,不能后退一步。它旨在用于快速、单向的数据读取操作。

如果之后需要使用数据,则必须使用已将数据复制到的数据表,或重新初始化它。

The DataReader is forward-only, you cannot take a step back. It is intended for quick, one-directional datareading operations.

If you need to work with the data afterwards, you will have to use the DataTable you have copied the data into, or reinitliaize it.

夜还是长夜 2024-08-26 08:29:59

取决于将产生多少数据。如果数据不多,您可以使用读取器将数据读入临时数据结构,然后迭代两个操作的数据结构。如果数据太多而无法有效地将其全部保存在内存中,那么您别无选择,只能创建一个新的数据读取器并重新执行命令。

Depends on how much data is going to be produced. If it's not much you could use the reader to read the data into a temporary data structure then iterate over the data structure for both of your operations. If there is too much data to keep it all in memory efficiently, then you've no choice but to create a new data reader and re-execute the command.

离不开的别离 2024-08-26 08:29:59

DataReader 是从数据库查询返回的数据流。执行查询时,第一行通过流返回到 DataReader。然后,流保持与数据库的连接,准备检索下一条记录。 DataReader 一次从数据库中读取一行,并且一次只能向前移动一条记录。当 DataReader 从数据库中读取行时,可以读取和评估每行中的列的值,但无法编辑它们。

通常我们使用 datareader ado.net 控件,我们不需要像绑定下拉列表或数据重复器控件那样转到上一行。为了保持轻量级,微软在数据读取器控制中支持仅转发功能。

要稍后使用它,请使用 DataTable 或创建一个保留内容的自定义类。

来源: http://www .techbaba.com/q/2758-why+datareader+forward+only.aspx

A DataReader is a stream of data that is returned from a database query. When the query is executed, the first row is returned to the DataReader via the stream. The stream then remains connected to the database, poised to retrieve the next record. The DataReader reads one row at a time from the database and can only move forward, one record at a time. As the DataReader reads the rows from the database, the values of the columns in each row can be read and evaluated, but they cannot be edited.

Generally we use datareader ado.net control where we don't need go to previous row like binding a dropdown or data repeater control. To keep it light weighted microsoft support forward only functionality in datareader control.

To use it afterward either use DataTable or make a custom class that persist the contents.

Source: http://www.techbaba.com/q/2758-why+datareader+forward+only.aspx

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文