Introduction to ADO.NET: A Simple Address Book • Chapter 7 |
323 |
In our sample, we commented out the lines responsible for creating the OleDbConnection object.That and the different connection string are all it takes to switch database connections.
Browsing a Database: Exercise
Now that you are connected to the database, you can retrieve some records. Data retrieval is the most intensive thing that you will do to your database. Online Transaction Processing, or OLTP, applications are designed for inserting and updating data quickly.They are not designed for fast and efficient retrieval of multi-dimensional data. Modern Relational Database technology does a good job of satisfying most needs, but many people often find themselves needing faster access to the data than they are currently getting. Faster is better, right? One of the benefits of ASP.NET are the caching and state management features.They enable you to connect to a database, return some results, and then cache this for a specific period of time.This caching can improve performance dramatically, while reducing the amount of load on the database. It is a true win-win situation.
Our example uses two methods that return data reader objects to the calling procedure. A Data Reader is a read-only, forward-only cursor.You can bind it to a DataGrid, a DataList, a DataRepeater, etc.You can only use it once due to its for- ward-only nature.This is the workhorse for ADO.NET, and especially for data access in ASP.NET.
Another object for browsing data is the DataSet. You can think of the DataSet as an in-memory database.You can add DataTables, which are synonymous with database tables; you can create DataViews, DataRelations, and constraints.The DataSet is very useful when you are going to access the same data more than twice in a page hit or session.The thing to keep in mind is that it is not connected to the database. Once you fill the DataTable, it is disconnected from the data source.The DataTable doesn’t know anything about the database. As far as ASP.NET goes, DataTables are useful for populating drop-downs with data that doesn’t change very often, but is used many times in a single session. If you place a DataSet in a session, beware that the memory is taken up until the session times out, not just when the user leaves the site. Our example doesn’t use the DataSet, but a DataSet can be bound to the DataList in the same manner as the
DataReaders are.
Our example uses stored procedures extensively for SQL Server, and raw SQL for Access. Not all Relational databases create and consume stored procedures the same way; therefore their implementation is specific to the database. In this example,T-SQL is used to create the stored procedures in SQL Server.