ADO.NET问题:何时使用DataReader、DataAdapter

发布于 2024-09-18 09:25:07 字数 219 浏览 6 评论 0原文

我只是想知道,在使用 DataReader 和 DataAdapter 从数据库中获取数据时我必须考虑哪些事情,这两者之间有什么区别,datareader 需要打开连接而 datadapter 不需要...在我们的项目中,我们使用 DataReader我们所有的 DAL,我们从不使用数据适配器。所以我想知道在什么情况下使用 DataAdapter + Datatable 组合比使用 DataReader 更好。提前致谢。

i just wondering, what things i have to consider when using DataReader and DataAdapter in fetching data from the database and whats the difference between this two other the datareader needs open connection and the datadapter does not... In our projects, were using DataReader in ALL our DAL, we never use dataadapter. So I wondering what scenario would it been better to use DataAdapter + Datatable combo than using DataReader. Thanks in advance.

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

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

发布评论

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

评论(7

归途 2024-09-25 09:25:07

DataReader :当您只想以只读模式获取数据、填充业务实体并关闭读取器时,最好使用此方法。这真的很快。

假设,您有一个客户类,并且您希望拥有完全初始化的对象,其中填充了所有客户属性(名称、地址等..)。

您将在此处使用 DataReader 并仅填充实体并关闭阅读器。

您无法使用 datareader 进行更新。

DataAdapter :您可以使用 dataadapters 读取/更新数据,但读取数据时速度不如 Datareader 快。

您可以使用 DataAdapter 更新数据,但使用 reader 则不能

DataReader : This is best used when you just want to fetch data in readony mode , populate your business entity and close the reader. This is really fast.

Say suppose , you are having a customer class and you want to have fully initilized object with all your customer properties filled like( Name,Address etc..)

You will use DataReader here and just populate the entity and close reader.

You cannot do update with datareader.

DataAdapter : You can Read/Update the data with dataadapters but it is less faster when reading the data then Datareader.

You can update the data with DataAdapter but with reader you won't

梦在深巷 2024-09-25 09:25:07

在做 ADO.NET 工作时,我几乎总是喜欢 DataReader;原因是,它不会强迫您在客户端上存储数据的时间超过您必须的时间。

这也是何时对 DataSet/Table 使用 DataAdapter 的答案;当您想将数据存储在客户端上时,也许要以某种方式使用它 - 来回迭代它,或者将其作为一个集合进行操作,而不是简单地将值输出到网格中,在网格中,阅读器,IMO,是一个更好的选择。

I almost always favor the DataReader when doing ADO.NET stuff as well; the reason being, it does not force you to store the data on the client any longer than you must.

That's also somewhat the answer to when to use a DataAdapter to a DataSet/Table; when you want to store the data on the client, perhaps to work with it somehow - iterating back and forth through it, or operating on it as a set, as opposed to simply outputting the values into a grid, where the Reader, IMO, is a better option.

痴者 2024-09-25 09:25:07

DataReader 允许您处理每条记录并将其丢弃,当您想要处理大量彼此不相关的数据记录时,这非常有用。例如,当您想要从数据库中的每条记录计算一些复杂的统计值,或者将大量数据记录保存到本地文件中时,您可以使用DataReader

DataAdapter 是另外一回事,它能够让你在内存中保存数据记录。它允许您使用 GUI 来浏览数据、编辑数据等。它更通用,但不适用于大型数据集。

DataReader allow you to process each record and throw it away, which is good when you want to process a lot of data records with no relation to each other. For example, you might use DataReader when you want to calculate some complex statistic value from every records in the database, or to save a lot of data records into a local file.

DataAdapter is something else, it is capable to let you have data records in the memory. That allows you to make the GUI to browse data, editing data, etc.. It is more general but will not work well with large data set.

め可乐爱微笑 2024-09-25 09:25:07

当您使用 DataSet 时,您只想使用 DataAdapter。
适配器有 2 个主要方法 Fill()Updater() 来读取数据集并将其写入数据库。

请注意,Fill() 将打开一个 Connnection,使用 DataReader 获取所有记录,然后关闭该 Connnection。

如果没有数据集和数据表,您就无法使用数据适配器。

所以真正的问题是:您想在 DAL 中使用哪种存储类?数据集可行且简单,但它是一项老化的技术(不再改进)。

也许您应该寻找 ORM(对象关系映射)库。但这将用更复杂的选择取代您的 DataReader/Adapter 问题。

You only want to use DataAdapters when you use DataSets.
An Adapter has the 2 main methods Fill() and Updater() to read a Dataset from and write it to the Database.

Note that Fill() will open a Connnection, use a DataReader to get all records and then close the Connetion.

Without Datasets and DataTables you don't have a use for DataAdapters.

So the real question is: What kind of storage classes do you want to use in your DAL? DataSets are viable and simple but it's an aging technology (no longer improved).

Maybe you should look around for an ORM (Object Relational Mapping) library. But that will replace your DataReader/Adapter question with a much more complicated choice.

假扮的天使 2024-09-25 09:25:07

从不使用DataReader。

由于我对应用程序进行了强烈分层,我的 DAL 负责与数据库通信,而我的 BLL 负责构建对象,因此 BLL 无法在完成后关闭 DataReader。相反,BLL 向 DAL 请求 DataSet/DataTable,DAL 会满足该要求。它通过执行 Fill 来实现这一点(按照 TomTom 的观点,> 查看堆栈跟踪,是的,您将在其中看到一个 DataReader)。然后 BLL 对结果集执行它喜欢的操作。

I never use DataReader.

Since I strongly layer my application, my DAL is responsible for talking to the database and my BLL is responsible for building objects, there's no way for the BLL to close the DataReader when it's done. Instead the BLL requests a DataSet/DataTable from the DAL, which the DAL fulfills. It does this by performing a Fill (to TomTom's point > look at the stack trace and yes, you will see a DataReader in there). The BLL then does what it likes with the result set.

不乱于心 2024-09-25 09:25:07

什么时候我必须考虑什么
使用DataReader和DataAdapter

DataReader:良好的低级接口。几乎是唯一的接口 - 如果将数据加载到更高的结构中,则实际加载始终使用 DataReader 完成。

DataAdapter / DataSet:那些喜欢结构化程序和漂亮代码并且不只是碰巧编写报告应用程序的人不使用的东西。请改用 ORM - NHipernate(好)、Linq2SQL(坏)、实体框架(坏)或其他更好的抽象之一。

what things i have to consider when
using DataReader and DataAdapter

DataReader: Good low level interface. PRetty much the ONLY interface - if you load data into higher up structures, the actual load is always done using a DataReader.

DataAdapter / DataSet: stuff not used by people who like structured p rograms and nice code and do not just happen to write a reporting applcation. Use an ORM instead - NHipernate (good), Linq2SQL (bad), Entity Framework (bad) or one of the other better abstractions.

始终不够 2024-09-25 09:25:07

我想这个问题只是讨论过程和缺点,并且偏离代码

*Data Reader 在获取数据方面比 DataAdapter 快得多,但您必须知道到底什么是断开连接模式

*正在使用 DataReader 或连接模式和 DataAdapter 断开连接模式在相同的场景中,但有时,如果您是数据的一种方式,断开连接模式会更好

*但是断开连接模式提供了丰富的 API,如 DataAdapter 、DataView 、DataTable 和 DataSet 。强大的功能是,您只需为 DataAdapter 提供 SELECT、INSERT、UPDATE、DELETE 命令,通过一行代码 Adapter.Fill(DataTable) 或 Adapter.Fill(DataSet) 附加来自单个表或多个表的数据,以及与更新数据适配器相同的方式。更新(DataTable)

*在断开连接模式下更新分层数据比在连接模式下工作要好得多,连接模式必须使用额外的代码和额外的逻辑来维护,在断开连接模式下,您可以更新仅插入的数据更新操作旁边的行或更新的行或删除的行包含在 Dot Net Transaction 中
Adapter.Update(DataTable.Select("","",DataViewRowState.Added))

*在断开连接模式下,您可以获得数据中每一行的版本,此外您还可以更改数据 DataTable.GetChanges ()

*断开模式为您提供strongTypedDataSet,因此您可以获得数据定义架构和关系,您可以获得父行和子行

*断开模式提供通过PrimaryKey获取行的方法,还可以获取具有特定条件的行 DataTable.Select("FilterExpression ","SortOrder",DataRowViewState)

*您可以通过 DataTable 进行计算,并且不会通过像 select ProductID,ProductName,Price,Quantity,price*quantity as Total 这样的计算来打扰您的服务器,您可以轻松添加具有特定条件的列(价格*数量)

*您可以进行聚合或抢夺的DataTable,DataTable.Compute(“Sum(price)”,“price> 250”)

*在断开连接模式下,您有CommandBuilder,它为您创建sql命令,但它只能与单桌

I guess this question just to talk about proc and cons ,and being off side the code

*Data Reader is much faster than DataAdapter in fetching Data but you have to know what's exactly Disconnected mode

*DataReader or Connected mode and DataAdapter Disconnected mode are being used in the same scenarios but some times Disconnected mode is better in case of you're a way of your data

*But the Disconnectiod mode is provided with rich APIs like DataAdapter ,DataView ,DataTable and DataSet. The powerful thing is you simply provide your DataAdapter with SELECT,INSERT,UPDATE,DELETE Command ,Attach you Data from single table or multiple tables ,with one line of code Adapter.Fill(DataTable) or Adapter.Fill(DataSet) ,and the same way with Updating Data Adapter.Update(DataTable)

*Updating Hierarchical Data in Disconnected mode is far better than working in connected mode which has to use extra code and extra logic to maintain ,in Disconnected mode you have the the ability to update Only Inserted Rows or Updated Rows Or Deleted Rows beside updating operation is wrapped inside Dot Net Transaction
Adapter.Update(DataTable.Select("","",DataViewRowState.Added))

*in disconnected mode you got the ability get the versions of every single row in your data ,beside that you can the Changes to your Data DataTable.GetChanges()

*Disconnected mode provide you with stronglyTypedDataSet ,so you get your data definition schema and the relations ,you can get parent and child rows

*Disconnected mode provide method for Getting Rows by PrimaryKey also getting rows with specific criteria DataTable.Select("FilterExpression","SortOrder",DataRowViewState)

*you can make calculation over DataTable and don't disturb your server with calculations like select productID,ProductName,Price,Quantity,price*quantity as Total, you can easily add column with specific criteria (price*quantity)

*you can make aggregations or your snatched DataTable ,DataTable.Compute("Sum(price)","price>250")

*in Disconnected mode you have CommandBuilder which it creates the sqlcommands for you,but its only working with single table

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