The biggest disadvantage of DataSet is speed because it is a high resource consuming process with one or more related database tables are in-memory. It carries considerable overhead because of the related tables, constraints, and relationships among the database tables.
I hope you get the difference between DataSet and DataReader in. NET by step by step guide provided by me. I would be happy to provide my feedback on it. Thank you! Save my name and email in this browser for the next time I comment. Home Blog Asp. Sign in. Forgot your password? Get help. Password recovery. The DataSet can also get its data from a data source via a managed provider, but the data source can also be loaded manually, even from an XML file on a hard drive. If the.
NET Framework does not provide a managed provider that is specifically designed for your database, it is certainly worth checking to see if the manufacturer or a third party has one available since they should perform better than the generic OLE DB and ODBC providers. In ASP. The following code demonstrates how to retrieve a list of products from the Northwind database using a SqlDataReader object:.
Both a SqlConnection and a SqlCommand object are created. At this point the connection to the database is still open and associated with the SqlDataReader. NET DataGrid. Alternatively, a DataReader could be used to retrieve the rows and then loop through them manually, one by one. It can support several resultsets as well. For example, a list of products and categories could be retrieved from a database. The following code retrieves a SqlDataReader and loops through its rows, writing the first column's value for each row to the console:.
The DataReader supports access to multiple resultsets, one at a time, in the order they are retrieved. This code is easily modified to handle multiple resultsets.
The following code retrieves a SqlDataReader and loops through its rows, again writing the first column's value for each row to the console:. Once all of the rows from the first resultset are traversed, the rowset from the second query is retrieved and its rows are traversed and written. This process can continue for several resultsets using a single SqlDataReader.
The Read method of the SqlDataReader loads the next record so that it can be accessed and moves the position of the cursor ahead. It returns a Boolean value indicating the existence of more records. This feature can help circumvent a common problem in classic ADO development: the endless loop. In classic ADO, when looping through a recordset object developers would often omit the MoveNext method and run the code only to remember a second too late that this would cause the recordset to loop infinitely on the same record.
NET is kind to developers as its DataReader object's Read method automatically moves the position to the next record so this situation can't occur. It also returns a Boolean value indicating if there are additional resultsets to traverse, like the Read method does. The DataReader in the previous code sample shows how to get the value for a column from the DataReader using its ordinal index position.
Can the DataReader be indexed by the column name or can the index of the column be retrieved? The answer to both of these questions is yes. The code in Figure 1 shows how a DataReader retrieves data and displays the CompanyName for each row in the diagnostics' output window. To demonstrate these techniques, this code displays the CompanyName value, column name, and index. The first line in the loop writes the CompanyName using the value representing the name of the CompanyName column.
This could also have been accomplished by passing the index of 1. I avoid this as it is less clear which column you are accessing, although using the string value is slower than using the index. The second line in the loop writes the name of the column at position 1 CompanyName using the GetName method. You might also notice that the CommandBehavior is set to CloseConnection, ensuring that the connection will be closed when the SqlDataReader is closed. These methods are simple but they make the SqlDataReader a power tool.
NET disconnected architecture. Unlike the DataReader, the DataSet is not connected directly to a database through a Connection object when you populate it. Instead, to fill a DataSet from a database you first create a DataAdapter object such as a SqlDataAdapter for the provider and associate it with a SqlConnection object.
You can think of the SqlDataAdapter as a bridge between the connected and disconnected objects. One of its purposes is to serve as the route for a rowset to get from the database to the DataSet. For example, when the SqlDataAdapter's Fill method is executed it opens its associated SqlConnection object if not already open and issues its associated SqlCommand object against the SqlConnection.
Behind the scenes, a SqlDataReader is created implicitly and the rowset is retrieved one row at a time in succession and sent to the DataSet. The following code shows how a DataSet can be filled from the Products table of the Northwind database.
Notice that there is no explicit SqlDataReader object in this code sample:. Unlike the DataReader, the DataSet is not read-only. A DataSet can be modified, and rows can be added or deleted. Changes to a DataSet can be sent to the database via a managed provider's objects. Its rows can be traversed forward or backward. The DataReader can be traversed forward only. In addition, a DataSet is highly flexible in that its DataTable objects can be filtered vertically or horizontally and they can be sorted or even searched.
Then a guy told me DataSet wasn't the best method to do.. A DataSet is the bucket here; it allows you to carry around a disconnected set of data and work with it - but you will incur the cost of carrying the bucket so best to keep it to a size you are comfortable with. And in the same way that you can fill a bucket with a hose, you can fill the DataSet with the data-reader.
I don't personally use DataSet very often - but some people love them. I do, however, make use of data-readers for BLOB access etc. It depends on your needs. One of the most important differences is that a DataReader will retain an open connection to your database until you're done with it while a DataSet will be an in-memory object.
If you bind a control to a DataReader then it's still open. In addition, a DataReader is a forward only approach to reading data that can't be manipulated. With a DataSet you can move back and forth and manipulate the data as you see fit. Some additional features: DataSets can be serialized and represented in XML and, therefore, easily passed around to other tiers. DataReaders can't be serialized. On the other hand if you have a large amount of rows to read from the database that you hand off to some process for a business rule a DataReader may make more sense rather than loading a DataSet with all the rows, taking up memory and possibly affecting scalability.
Here's a link that's a little dated but still useful: Contrasting the ADO. Further to Marc's point: you can use a DataSet with no database at all. You can fill it from an XML file, or just from a program. Fill it with rows from one database, then turn around and write it out to a different database. A DataSet is a totally in-memory representation of a relational schema.
Whether or not you ever use it with an actual relational database is up to you. As you said, dataset is most similar to VB6 Recordset.
That is, pull down the data you need, pass it around, do with it what you will. Oh, and then eventually get rid of it when you're done. Datareader is more limited, but it gives MUCH better performance when all you need is to read through the data once.
For instance, if you're filling a grid yourself - i. On the other hand, dont even try using datareader if you have any intention of updating the data To answer your second question - Yes, you should learn about DataReaders. If anything, so you understand how to use them. I think you're better of in this situation using DataSets - since you're doing data binding and all I'm thinking CPU cycles vs Human effort.
As to which one will give a better performance. It very much depends on your situation. For example, if you're editing the data you're binding and batching up the changes then you will be better off with DataSets.
0コメント