如何将 DataTable 转换为 IDatareader?

发布于 07-12 04:16 字数 692 浏览 16 评论 0原文

我们都知道 DataReader 比 DataTable 更快,因为 DataReader 用于构建 DataTable。

因此,鉴于我已经有一个 DataTable...为什么我要将其转换为 DataReader?

那么我正在创建一个名为 IDataProvider 的内部接口。 该接口旨在本地实现并作为 WebService 实现。 该接口将有一个方法“Getdata”,它接受一些条件信息并返回一些数据。

由于 DataReader 是最快的数据检索机制,因此我希望将其用作“GetData”方法的结果类型。 但是我们也知道 DataReader 不可序列化,因此无法通过 Web 服务在 Web 上传输...

在 Web 的情况下,我会让本地代理类请求数据作为 DataTable,然后将其在本地转换为数据读取器。

通过这种方式,本地应用程序不需要知道(或关心)它是在本地还是远程访问数据。

然而,为了做到这一点,我需要知道... 如何将 DataReader 包装在预先存在的 DataTable 周围?

更新:我的业务逻辑不会保存在 Web 服务中,因为使用的 DataProvider Web 服务可以切换为不支持的 Web 服务。 因此,业务逻辑将保存在客户端应用程序中。

FWIW我正在使用.Net 3.5 SP1

We all know that DataReaders are quicker than DataTables since the a DataReader is used in the construction of a DataTable.

Therefore given that I already have a DataTable.... Why would I want to convert it to a DataReader?

Well I am creating an internal interface called IDataProvider. This interface is intended to be implemented both locally and as a WebService. The interface will have a method "Getdata" which takes some criteria information and returns some data.

Since a DataReader is the quickest data retrieval mechanism, I will want to use this as the result type of the "GetData" method.
However we also know that the DataReader is not serializable and therefore cannot be transferred across the web via a web service...

In the case of the web I would have the local proxy class request the data as a DataTable and then transform it locally into a DataReader.

In this way the Local application need not know (or care) that if it is accessing the data locally or remotely.

However in order to do this I need to know... How do I wrap a DataReader around a preexisting DataTable?

Update: My business logic is not going to be held in the webservice since the DataProvider that uses the Webservice is switchable for one which does not.
The businessLogic will therefore be held in the client app.

FWIW I am using .Net 3.5 SP1

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

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

发布评论

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

评论(4

清引2024-07-19 04:16:35

只需在 DataTable 上调用 CreateDataReader()

Just call CreateDataReader() on your DataTable

爱她像谁2024-07-19 04:16:35

DataReader 是读取数据存储的最快方式,但前提是满足某些条件:

  1. 数据将以只进方式读取。
  2. 数据是只读的。

即使您的场景满足这些条件,DataReader 也代表一个连接的数据存储,这意味着您需要在 DataReader 通过网络传递的整个过程中保持连接打开,直到另一端的被调用方法返回一些值。某种回应。

因此,我认为活动的 DataReader 永远不应该通过各个层和应用程序传递。 我宁愿先将数据提取到另一个数据存储或集合中,然后立即处理 DataReader。

A DataReader is the fastest way to read a datastore, but only if certain conditions are met:

  1. The data is to be read in a forward-only manner.
  2. The data is to be read-only.

Even if these conditions are met by your scenario, the DataReader represents a Connected Datastore, which means that you would need to keep your connection open throughout the time that the DataReader is passed over the network and until the called method at the other end returns some sort of response.

Therefore, it is my opinion that an active DataReader should never be passed around through various layers and applications. I would much rather extract the data into another data store or collection first and dispose of the DataReader immediately.

落墨2024-07-19 04:16:35

没有现有的类可以为您执行此操作。 但编写一个可序列化的类来实现 IDataReader 并作为现有 DataTable 的包装器对您来说应该不难。

编辑:
您可能会发现从 DbDataReader 继承更容易(我认为,在对象资源管理器中检查 SqlDataReader 的基类)。 它为您提供了一些接口实现。 但是,是的,它仍然是很多乏味的代码。

There is no existing class that will do this for you. But it shouldn't be hard for you to write a serializable class that implements IDataReader and is a wrapper round your existing DataTable.

EDIT:
You might find it easier to inherit from DbDataReader (I think, check the base class of SqlDataReader in object explorer). It provides some of the interface implentation for you. But yes, it is still quite a lot of dull code.

暖树树初阳…2024-07-19 04:16:35

你不能。 DataReader 和DataTable 是两个不同的东西。

由于 DataReader 使您能够以流的形式读取数据,因此我真的不明白为什么要在客户端执行此操作。

DataReader 通常用于从数据库读取数据并添加逻辑来填充对象列表或 DataTable。 因此,最好在 Web 服务上执行与构建 DataTable 有关的大部分业务逻辑,将其作为 Web 服务传递给客户端,并与其他 ADO.Net 函数一起使用以获得更多业务逻辑。

也许您可以更具体地说明为什么您真正需要 DataReader?

You can't. DataReader and DataTable are two different things.

Since a DataReader enables you to read data as a stream, I don't really see why you want to do this client-side.

A DataReader is typically used to read data from the database and add logic to fill a list of objects or a DataTable. So it's best to do most business logic which has to do with the building of the DataTable on the webservice, pass it to the client as a webservice and work with the other ADO.Net functions there for more business logic.

Maybe you can be more specific on why you really want a DataReader?

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