How do I convert a DataTable to an IDatareader?

放肆的年华 提交于 2019-12-03 16:39:34

问题


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


回答1:


Just call CreateDataReader on your DataTable




回答2:


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.




回答3:


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.




回答4:


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?



来源:https://stackoverflow.com/questions/479532/how-do-i-convert-a-datatable-to-an-idatareader

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!