Wiley 978-0-7645-5708-8 Datasheet

Browse online or download Datasheet for Software manuals Wiley 978-0-7645-5708-8. Wiley Beginning ASP.NET 1.1 with Visual C# .NET 2003 User Manual

  • Download
  • Add to my manuals
  • Print
  • Page
    / 36
  • Table of contents
  • BOOKMARKS
  • Rated. / 5. Based on customer reviews
Page view 0
8
Reading from Databases
So far, you've learnt a lot about programming, and seen those techniques in use in a variety of Web
pages. Now it's time to turn our attention to one of the most important topics of building
Web sites – data. Whatever the type of site you aim to build, data plays an important part. From a
personal site (perhaps a vacation diary or a photo album), to a corporate e-commerce site,
data is key!
There are numerous ways to store data, but most sites use a database. In this chapter, we're going to
look at data stored in databases, and show how easily it can be used on Web pages. For this we are
going to use ADO.NET, which is the data access technology that comes as part of the .NET
Framework.
If the thought of databases sounds complex and scary, don't worry. We're going to show you just
how easy this can be. In particular, we'll be looking at:
Basics of databases and how they work
How to create simple data pages using Web Matrix
Different ADO.NET classes used for fetching data
Basics of ADO.NET and how it fetches data
How to use Web Matrix to simplify developing data access pages
Let's develop some basic understanding of databases first.
Understanding Databases
Understanding some basics about databases is crucial to using data in your pages. You don't need
to be a database expert, but there are certain things you will need to know in order to work with
data in .NET. For a start, you need to understand how data is stored. All types of data on a
computer are stored in files of some sort. Text files, for example, are simple files and just contain
57084_08.qxp 30/01/2004 8:02 PM Page 247
Page view 0
1 2 3 4 5 6 ... 35 36

Summary of Contents

Page 1 - Reading from Databases

8Reading from DatabasesSo far, you've learnt a lot about programming, and seen those techniques in use in a variety of Webpages. Now it's ti

Page 2

❑ PagerStyle: Defines the style of the pager section. In our grid, this is the last row showing thepage numbers, but it appears before the footer if a

Page 3 - Normalization

Try It Out Creating a Data Page1.Create a new page using the Data Pages templates. Pick the Data Report with Paging and Sorting,and call it SortPage.

Page 4

Figure 8-6This isn't much different from the drag and drop approach we used in the first example, but it uses the.NET data classes and a DataGrid

Page 5 - The Web Matrix Data Explorer

❑ AllowSorting: Allows the grid to sort the rows on the basis of column selections. Setting thisto true enables links on the column headings.❑OnSortCo

Page 6

one attached to. Well, since it hasn't got a named object, ASP.NET takes this as being a property of thecurrent Page. By default, a Page doesn&ap

Page 7 - Creating Data Pages

if (SortField == String.Empty)CommandText = "select * from Suppliers order by CompanyName";elseCommandText = "select * from Suppliers o

Page 8

Displaying Data Using the Code WizardsThere are times where both the drag and drop from the Data Explorer and the template pages cannotprovide you wit

Page 9

4. The drop-down list shows configured data sources (from the Data Explorer) as well as an optionto create a new connection. Pick the existing connect

Page 10 - Chapter 8

Figure 8-118. Click OK and you'll see the WHERE clause part of the window is filled in as shown in Figure 8-12:Figure 8-129. Press the Next butto

Page 11

You can see just the required columns in Figure 8-13.10. Press Next.11. From the Name Method window, change the name textbox to GetProductsDataSet. Ma

Page 12

plain text. Spreadsheets, on the other hand, are complex files containing not only the entered text andnumbers, but also details about the data, such

Page 13

This is where (pun intended) we add the WHERE part of the SQL statement, and this is what filters therows and joins tables together. We've select

Page 14

Now when you look at the WHERE clause section you see two tables joined together as in Figure 8-17:Figure 8-17The WHERE Clause Builder can also be use

Page 15

Next, we have the connection string that simply points to our existing database:string connectionString = "Provider=Microsoft.Jet.OLEDB.4.0; &quo

Page 16

We mentioned earlier that the data adapter is the link between our page and the data. As part of thislink, the adapter provides not only data fetching

Page 17

❑ System.Data.OleDb: Contains the objects used to communicate to databases via OLEDB.OLEDB provides a common set of features to connect to multiple da

Page 18

On the left we have the database and the connection, in the middle we have four Command objects, andon the right a DataAdapter and a DataSet. Notice t

Page 19

The three main methods of the command you'll use are the execute methods:In the examples so far, we haven't used these methods as the execut

Page 20

of products, with a drop-down list showing the product categories. The user could select a category sothat only those categories are shown.The Paramet

Page 21

6. Double-click the Fetch button to switch to the Click event procedure. Add the following code:void Button1_Click(object sender, EventArgs e) {DataGr

Page 22

13. Save the file and run it.14. Select a category and then click Fetch to see only the products for that category shown in Figure8-22:Figure 8-22What

Page 23

This is the sort of thing you'd see in a spreadsheet, but there are a couple of big problems with this. For astart, we have repeated information.

Page 24

lstCategory.DataValueField = "CategoryID";lstCategory.DataTextField = "CategoryName";lstCategory.DataBind();}}When the Fetch butto

Page 25 - The OleDbCommand Object

Once the query string is set, we define our command to run the query, as follows:System.Data.IDbCommand dbCommand = new System.Data.OleDb.OleDbCommand

Page 26

string queryString = "SELECT [Products].[ProductName], ""[Products].[QuantityPerUnit], [Products].[UnitPrice]," +"[Products].

Page 27

Many times, when fetching data we simply want to display it as it is, perhaps by binding it to a grid. TheDataSet provides a local store of the data,

Page 28

"[Products].[UnitsInStock] FROM [Products]";System.Data.IDbCommand dbCommand = new System.Data.OleDb.OleDbCommand();dbCommand.CommandText =

Page 29

System.Data.OleDbDataReader dataReader = new _dbCommand.ExecuteReader(System.Data.CommandBehavior.CloseConnection);if (!dataReader.HasRows)Response.Wr

Page 30

57084_08.qxp 30/01/2004 8:03 PM Page 282

Page 31

The OrderDetails table is as follows:We now have three tables that can be linked together by their ID fields as shown in Figure 8-1:Figure 8-1We now h

Page 32 - The DataReader Object

foreign key is simply a column that is the primary key in another table. Because the values of theprimary key and the foreign key will be the same, we

Page 33

Figure 8-22. Select Access Database from the window that appears and press OK.3. Enter the following into the Data File text area (use a central locat

Page 34

You can double-click on these to open the table, and see and change the data. One thing you mightnotice is that you don't see any queries – that&

Page 35 - Exercises

3. Save the page and run it as shown in Figure 8-5:Figure 8-5Amazing! A sortable grid full of data and you didn't have to write even a single lin

Page 36

some database specific features. You don't need to know about these specifically (they are fullydocumented in the .NET help files); just copy the

Comments to this Manuals

No comments