Retrieving Data from a Database

Chapter 1 - Displaying Data on the Web
byJohn Kauffman, Fabio Claudio Ferracchiatiet al.?
Wrox Press ?2002

Regardless of the data store involved, there are three steps to using it that will be common to almost every web application you write. You need to 'connect' to the data source; you need to read the data (and possibly convert it or otherwise perform operations upon it); and you need to display the results. Before we begin to delve into the way that .NET deals with handling data, we'll elaborate on these three topics in a more general way, as a quick heads-up on how data-driven sites function.

The diagram below lays out the three steps mentioned above, and places them in context with the code that you'll need to write, and the data store itself.

click to expand

Reading it from left to right, this diagram shows that there are three clearly separated aspects to the system: the application that requests the data, the code that communicates with the database and operates on the data, and the database itself. The details of this three-part structure and how it can be used to best effect are given later on in this chapter; for now, we'll continue on our top-to-bottom route.

  • Connecting to the data source. Before we can issue any commands to the database or retrieve any data from it, we must create a connection to it. This provides a conduit through which we can send and retrieve data. To establish a connection, we need to specify such things as the type of database we are opening, where it is located, and any necessary security permissions. Once this has been done, and the connection has been opened, we can start to send instructions to the database.

  • Reading/converting the data. Through the connection, we can tell the database to add, delete, and update records, to return information to us, and so on. As you can see from the diagram, there is more involved here than in the other steps. This is because the database expects commands to be in a different language from that of the application, and the application expects data to be returned in a different format from that stored in the database. Once information has been sent or retrieved, however, the connection to the database can usually be terminated. In special cases, an open connection is maintained, allowing data to be returned and displayed a little at a time, rather than all at once.

  • Displaying the data. Once the data has been retrieved and converted into the correct format, it is usually transformed in some way to a format that's viewable by the user, such as an HTML table. Although there are far more operations during the reading and converting of the data, these largely happen behind the scenes, and in web applications it's often the case that presenting the information well takes the most time to implement. As we'll see later, however, ASP.NET offers us some help in this regard.

That's a lot of information, and lot of the things we'll be talking about in the first few chapters of this book where we will cover these issues more slowly, and in much greater depth. It also sounds like a lot of work - in fact, it is a lot of work - but mercifully we don't have to do all of it ourselves. Help is at hand in the form of ADO.NET, and that's our subject for the next section.



Beginning ASP. NET 2.0 and Databases
Beginning ASP.NET 2.0 and Databases (Wrox Beginning Guides)
ISBN: 0471781347
EAN: 2147483647
Year: 2004
Pages: 263

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net