开发者

Difference between Datareader, DataAdapter, Dataset, DataView

开发者 https://www.devze.com 2023-03-28 04:06 出处:网络
Can someone please explain the 开发者_StackOverflowdifference between a DataReader, a DataAdapter, a Dataset, and a DataView?Quickly,

Can someone please explain the 开发者_StackOverflowdifference between a DataReader, a DataAdapter, a Dataset, and a DataView?


Quickly,

  • a DataReader is a forward-only iterator over a set of results. It's usually the most efficient way to deal with records when you don't need random access (in other words you can't go backwards). It is "scalable" to any number of records, at least in terms of memory pressure, since it only loads one record at a time. One typical way to get a DataReader is by using the ExecuteReader method of a DbCommand.

  • a DataSet represents a set of DataTable objects. More often than not, it will just contain one table, but if you do a query with multiple SELECT statements, the DataSet will contain a table for each. Because this is an in-memory representation, you have to be careful about how much data you pull into a DataSet. You can "Fill" a DataSet using the Fill method of a DataAdapter.

  • a DataAdapter is a kind of "pipe" that funnels data from a DB engine into a DataSet. That's why you'll have one DataAdapter implementation for each DB provider type. One DataSet, many providers.

  • a DataView is like a virtual subset of a DataTable.


DataReader

DataReader is used to read the data from database and it is a read and forward only connection oriented architecture during fetch the data from database. DataReader is used to iterate through resultset that came from server and it will read one record at a time because of that memory consumption will be less and it will fetch the data very fast when compared with dataset. Generally we will use ExecuteReader object to bind data to datareader.

DataSet

DataSet is a disconnected orient architecture that means there is no need of active connections during work with datasets and it is a collection of DataTables and relations between tables. It is used to hold multiple tables with data. You can select data form tables, create views based on table and ask child rows over relations. Also DataSet provides you with rich features like saving data as XML and loading XML data.

DataAdapter

DataAdapter will acts as a Bridge between DataSet and database. This dataadapter object is used to read the data from database and bind that data to dataset. Dataadapter is a disconnected oriented architecture.


I suggest you read the MSDN documentation or maybe buy a book. Here's a short answer:

  • A dataset is an in-memory representation of a database-like structure. It can have one or more datatables and define relations between them, key field etc.
  • A datatable is an in-memory representation of a single database table. You can think of it as having columns and rows in the same way.
  • A dataview is a view on a datatable, a bit like a sql view. It allows you to filter and sort the rows - often for binding to a windows form control.


DataReader

DataReader is like a forward only recordset. It fetches one row at a time so very less network cost compare to DataSet(Fethces all the rows at a time). DataReader is readonly so we can't do any transaction on them. DataReader will be the best choice where we need to show the data to the user which requires no transaction. As DataReader is forward only so we can't fetch data randomly. .NET Data Providers optimizes the datareader to handle huge amount of data.

DataSet

DataSet is an in memory representation of a collection of Database objects including tables of a relational database schemas. DataSet is always a bulky object that requires a lot of memory space compare to DataReader. We can say that the DataSet is a small database because it stores the schema and data in the application memory area. DataSet fetches all data from the datasource at a time to its memory area. So we can traverse through the object to get the required data like querying database.

0

精彩评论

暂无评论...
验证码 换一张
取 消