<asp:AccessDataSource> ID="MySourcName" Runat="server" DataFile="MyMDBName.mdb" SelectCommand="SELECT MyField1, MyField2 FROM MyTable"> </asp:AccessDataSource> |
<html> <head> <title>TIO ch02-1 Display MDB Data</title> <head> <body> <h3>ch02 TIO 2 Demonstration of connection to an MDB source </h3> <body> </html> |
<%@ page language="VB" %> <html> <head><title>ch02 TIO 2 Display MDB Data</title></head> <body> <h3>ch02 TIO 2 Demonstration of Connection to an MDB Source </h3> <form runat="server"> <asp:accessdatasource id="NorthwindProductsAccDataSource" runat="server" selectcommand="Select * From Products" datafile="~/App_Data/Northwind.mdb"> </asp:accessdatasource> <asp:gridview id="NorthwindProductsGridView" runat="server" datasourceid="NorthwindProductsAccDataSource"> </asp:gridview> </form></body></html> |
<asp:accessdatasource id="NorthwindProductsAccDataSource" runat="server" selectcommand="Selcet * From [Sales by Category] " Datafile="~/App_Data/Northwind.mdb"> </asp:accessdatasource> |
(14) Open the browser and view the page.
Example Note #2 - Connect to MDB and display data with GridView
You have two controls on the page. The first, AccessDataSource, performs all the work of connecting to the ADO.NET objects that connect to the JET engine that interacts with the MDB file. The second is GridView, which is used to obtain data and convert its format into HTML format that can be displayed on the page. Please note that it is very important to give each control a meaningful name (ID). You must then make sure that the data-bound control (GridView) uses its DataSourceID property to reference the AccessDataSource's ID in order to use the AccessDataSource as its data source.
Displaying data from a query is not very difficult; in Access you can use query names instead of table names. There are two instructions here. First, if there are spaces in the name of a table or query, the entire name must be enclosed in square brackets. Second, queries that require user input (for example, "sales in a given year" require knowing which year) involve techniques that are beyond the scope of this book.
After completing the previous steps, you can see the data obtained from the AccessDataSource control displayed on the page. Later chapters will cover GridView in detail, but this chapter will continue to focus on data source controls.
Select variables in statements
When creating a data source in VWD, the wizard will ask you to specify the columns that need to be displayed or to create a custom SQL statement. In the previous "Try It Away", you simply selected a few columns. More complex SQL statements can be specified in several different ways:
● Use interactive dialog boxes
● Enter customized SQL statements in the AccessDataSource wizard (called by the Configure Data Source convenience task in the control)
● Enter statements in the property grid
● Enter statements directly in the markup of the Source view
Selecting columns in the Access DataSource wizard is a preferred technique for simple queries that return one or more columns from a separate table because it reduces typography and syntax errors (see Figure 2-3 ). In this wizard, you can select the name of the table or query from the Name drop-down list. You can then select the required columns in the table or query by selecting all columns (*) or any group of columns. If you click the Order By button, you can sort by any column in the data source. If there is a constraint on the first column, the field selected in Then By will be used. As you select options from the wizard, note that the actual SQL syntax for the SelectCommand is also displayed in a read-only text box.
Figure 2-3
You can use the WHERE button in the wizard to create a SQL statement with parameters. This part will be introduced in Chapter 9. We will skip this selection for now, but it is important that before deployment you use these parameters which will be discussed later in this book. In a deployed site, avoid directly connecting user input to SQL statements. Ignoring parameter issues will expose your site to SQL injection attacks. This attack technique uses false characters from user input to invalidate the original SQL statement and then replaces it with a destructive statement. You can use parameter collections to send user input to ADO.NET parameter collections, which can use the functionality of collections to reduce SQL injection problems.
Although the Configure Data Source wizard has many options to help quickly create SQL statements, sometimes you still need to directly enter (or modify) the SQL statement in the marked SelectCommand. The AccessDataSource wizard allows these operations. In the wizard page for selecting tables, queries, and columns, you can select the radio button for "Specify a custom SQL statement or stored procedure." After selecting this option in the wizard page, click the Next button to enter another page where you can directly enter custom statements in the text area. You can also use Visual Studio QueryBuilder to visually create custom statements, using tools that are very similar to Access QueryBuilder.
If you do not want to use the wizard, you can choose to enter a custom SQL statement in the AccessDataSource control's property grid, or you can switch to Source view and enter the statement in the SelectCommand property of the AccessDataSource control tag.
There are many books about SQL (the same type as this one is Beginning SQL Programming , ISBN 1-861001-80-0), and this book provides a brief introduction in the appendix. If you plan to learn SQL syntax, you can start by learning the commands for returning part of a record (TOP and DISTINCT), the syntax for renaming a field (AS), and the technique for returning fields from two related tables (JOIN). The following exercises will explore some variables in SQL statements.
Try #3 - Changing the Select Statement in AccessDataSource In this exercise, you will display specific columns and specific records from Northwind's Products table. A page will also be created to display the data obtained from the query. Please note that in this exercise, the selection rules are already in the source code. Currently, there is no user input as an option.
(1) Create a file named ch02_TIO_3_AlternateSelect-Commands.aspx in C:WebsitesBegAspNet2Dbch02.
(2) Add an AccessDataSource control with Northwind as the ID and set the MDB to App_DataNorthwind.mdb. In a series of dialog boxes, set up the Select command to retrieve all fields from the Products table ("name"=Products). Take a look at the Source view and notice the statement you created, as shown below:
SELECT * FROM Products |
<%@ page language="VB" %> <html> <head runat="server"> <title>ch02 TIO 3 Alternate Select Conimands</title> </head> <body> <h3>ch02 TIO 3 Alternate Select Coinmands</h3> <form runat="server"> <asp:accessdatasource id="NorthwindAccDataSource" runat="server" selectcommand="SELECT * FROM Products" datafile=" ~/App_Data/Northwind.mdb" > </asp:accessdatasource> <asp:gridview id="GridViewl" runat="server" datasourceid="NorthwindAccDataSource"> </asp:gridview> </form> </body> </html> |
SELECT * FROM [Products] WHERE (CategroyID=3) |
<asp:accessdatasource id="NorthwindAccDataSource" runat="server" selectcommand="SELECT * FROM [Products] WHERE (CategoryID = 3) " Datafile="~App_Data/Northwind.mdb"> </asp:accessdatasource> |
SELECT * FROM Products WHERE ProductID = 12 SELECT * FROM Products WHERE ProductName = 'Northwoods Cranberry Sauce' SELECT * FROM Products WHERE ProductID <11 SELECT * FROM Products WHERE ProductID <11 ORDER BY ProductName Ascending SELECT * FROM Products WHERE SupplierID = 6 OR SupplierID = 8 SELECT * FROM Products WHERE SupplierID = 24 AND UnitPrice > 10 |
Variables in MDB file location
MDB files are stored in various physical locations on the hard disk: the same folder as the Web page, a subfolder under the Web page, or other folders on the machine, etc. In Visual Web Developer, these files can be browsed frequently in the designer, and the correct path to the MDB file has been entered. However, if you plan to enter your own code, you will need to follow the syntax described in this section.
The DataFile property of the AccessDataSource control contains a page path that can be fully qualified (for example, starting with a drive letter) or specified relative to the location of the page that contains the AccessDataSource. Paths can also be relative to the application, i.e. using URL syntax to reference the path. This syntax replaces the symbol (~) with the application root directory, for example: ~App_Dataproduces.mdb. Using application-relative paths makes it easier to move pages from one location to another without breaking references to the database, so it is recommended to use application-relative paths whenever possible.
First, let's look at the syntax of a fully qualified path, which contains the entire path to an MDB file, starting with the root of the computer drive where the file is located:
<asp:accessdatasource . . . datafile= "C:WebSitesWebApplicationApp_DataMyMdb.mdb"> |
<asp:accessdatasource . . . datafile="MyMdb.mdb"> |
<asp:accessdatasource . . . datafile="MyDaughterFolder/MyMdb.mdb"> |
<asp:accessdatasource . . . datafile="../MyMdb.mdb"> |
<asp:accessdatasource . . . datafile="~App_Data/MyMdb.mdb> |