Download presentation
Presentation is loading. Please wait.
1
Chapter 10 Accessing Database Files
Programming In Visual Basic .NET
2
Visual Basic and Database Files
VB projects can display and update the data from database files VB .NET uses ADO.NET for database access ADO.NET is the next generation of database technology, based on Microsoft's previous version, ActiveX Data Objects (ADO) © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.
3
ADO.NET Data is stored and transferred in Extensible Markup Language (XML) Allows access to database data in many formats Basic types of providers OleDb, SQLClient for SQL Server, Odbc and Oracle © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.
4
Database Terminology Databases Tables Relational database
Records (rows) Fields (columns) Key field (unique to each record) Relational database Multiple tables Relationships between the tables © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.
5
XML Data Industry-standard format for storing and transferring data
Specifications at World Wide Web Consortium (W3C) The XML needed for accessing databases will be automatically generated for you in Visual Basic © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.
6
XML Data (continued) Data stored in XML are all text, identified by tags XML files can be edited by any text editor, such as Notepad Tags Used for identification, similar to HTML Not predefined like HTML tags Can identify database fields by name © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.
7
XML Data (continued) XML Data File XML Schema File (.xsd file)
Contains actual data XML Schema File (.xsd file) Contains descriptions for Fields Data Types Constraints, such as required fields View .xsd file in Solution Explorer © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.
8
Using ADO.NET and Visual Basic
Can display data from a database on Windows Form Web Form Add controls to form and bind data to them Label, TextBox Special controls designed just for data such as DataGrid, DataList © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.
9
Using ADO.NET and Visual Basic (continued)
Connection Establishes a link to a data source, a specific file and/or server Data Adapter Handles retrieving and updating the data Dataset Contains actual data, may come from multiple connections and/or multiple data adapters © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.
10
Steps to Display Data in Bound Controls on a Form
Source Connection Adapter Dataset Web Form Windows Specific file Connection Object Handles data transfer and provides data for dataset; uses SQL Actual data, can contain multiple tables and relationships Data display on the form in bound controls © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.
11
Connections Establishes a link from specific file or database to program ADO.NET provides four types OleDBConnection SqlConnection OdbcConnection OracleConnection © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.
12
Connections (continued)
Creating a new connection Data controls in the Toolbox Use Server Explorer to manage connections No matter which method above is used to begin the connection, the Data Link Properties dialog appears for you to complete the task © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.
13
Creating a Connection from Server Explorer
Click Connect to Database button OR Right-click Data Connections, select Add Connection © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.
14
Data Link Properties Dialog
Used to define Connection's OLE DB Provider Database Filename Can test if connection succeeded © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.
15
Connections in Server Explorer
Shows connections for all projects Same connection can be used for multiple projects Expand connection node to view tables and fields © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.
16
Data Adapters Does all the work of passing data back and forth between a data source and a program Data does not have to be from a database Data can be text file, object, or even an array Add data adapter Drag table name from Server Explorer to form Ctrl + click field names and drag as a group to the form © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.
17
Datasets Temporary set of data stored in memory
In ADO.NET datasets are disconnected; the copy of data kept in memory does not keep an active connection to the data source Dataset may contain multiple tables and relationships © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.
18
Datasets (continued) To define a dataset
Select data adapter component Right-click on the adapter and select Generate Dataset Name the dataset in the Generate Dataset dialog box Write code to fill the dataset (opens the connection to the data source and retrieves data into the dataset) Any controls bound to the dataset will automatically fill with data © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.
19
Data Passes from Data Source to Dataset to User Interface
Data Object (Class) Windows Forms Web Data Adapter Presentation Tier Business Tier Data Tier Dataset Source © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.
20
The Fill Method Execute Fill method of Data Adapter at run time to fill the dataset Usually coded in Form_Load (or Page_Load for Web projects) General Form Example DataAdapterName.Fill(DataSetName) booksDataAdapter.Fill(BooksDataset1) © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.
21
Binding Data to Controls
Set properties of the controls To bind data to a grid, set DataSource property Data Member property To bind individual fields to controls (such as labels and text boxes) Set DataBindings property © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.
22
Creating a Bound DataGrid Control (pages 391-394)
Steps Set up the Connection Add the Connection and Data Adapter to the Form Rename Connection and Data Adapter Generate a Dataset Add a Data Grid to the Form and set DataSource and DataMember properties © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.
23
Creating a Bound DataGrid Control (continued)
Write the Code Windows Form - Form_Load booksDataAdapter.Fill(BooksDataset1) Web Form - Page_Load booksDataAdapter.Fill(BooksDataset1) booksDataGrid.DataBind( ) Run the Project Modify the Grid Format © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.
24
Displaying a Data Preview
Allows you to see what your data looks like while you are designing the user interface Right-click DataAdapter control, select Preview Data In the dialog box verify that the correct DataAdapter displays in the list Click Fill Dataset, resize columns and scroll to see all data © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.
25
Displaying a Data Preview (continued)
Click to Display the data © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.
26
Binding Data Complex Data Binding Simple Data Binding
Binds more than one data element to a single control Used for DataGrids, and List or Combo Boxes Set DataSource and DataMember properties (may need additional) Simple Data Binding Connects one control to one data element through the Data Bindings property Use for TextBoxes and Labels (or any other control that has a Data Bindings property) © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.
27
Selecting Records from a Combo Box
Allows user to select records to view or update Windows Forms Use ListBox or ComboBox Web Forms Use ListBox or DropDownList © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.
28
Filling a List - Windows Forms
Set ListBox or ComboBox Properties DataSource to DataSet name DisplayMember to table name.field name Use Fill method to populate list with data ' Fill the dataset for the list box. BooksDataAdapter.Fill(BooksDataset1) © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.
29
Binding a Single Data Field
To bind data to a single control set the DataBindings property Expand DataBindings property and choose the Text property Expand the Dataset to reveal a list of fields Set the DataBindings Text property to the correct field © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.
30
Binding a Single Data Field (continued)
Bind a Label control’s Text property to a single field in a dataset © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.
31
Sorting the List Data Sort records using the data adapter’s SQL SELECT statement Select the data adapter, choose Data/Configure Data Adapter Navigate through the Wizard to the SQL statement page Choose “Ascending” in the Sort Type cell of the Query Builder Right-click the data adapter, select Generate Dataset and select the existing dataset name © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.
32
Sorting the List Data (continued)
© 2005 by The McGraw-Hill Companies, Inc. All rights reserved.
33
Selecting Records Using Web Forms
Set up connection, data adapter and dataset same as for Windows applications Need to modify program logic for the Web due to security and because web pages are Stateless Stateless means that each time a page displays it is a new fresh page Page_Load event occurs for every round trip to the server © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.
34
Filling a List – Web Forms
Postback, round trip to the server, occurs each time user makes a selection After Postback, web page redisplays, Page_Load event occurs Modify logic in Page_Load to use selection made by user Use Me.DataBind( ) statement to bind all controls on page at once © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.
35
Setting the List Properties
Properties to set for the Web version of DropDownList control DataSource property to the Dataset DataMember to the table name DataTextfield to name of field to display in list AutoPostBack property set to True to respond when a user selects from the list © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.
36
Retrieving the Selected Record
In a Web application, the dataset is created on the server, only one pageful of data is sent to the client When a user makes a selection from a list, it makes sense to retrieve only the selected record, not the entire table Use a parameterized query to create a dataset with only the selected record © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.
37
Retrieving the Selected Record (continued)
A Web selection application will have two data adapters One selects data to fill a drop-down list One selects a single record to display An SQL SELECT statement to select a particular record requires a WHERE clause Use a question mark in place of an actual value and supply the value as a parameter in code This type of query is called a parameterized query © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.
38
Query Builder - Parameterized Query
Where (Title = ?) © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.
39
Displaying the Data for a Selected Item
Supply value to be used for a Parameterized Query at run time TextBox Check for an empty dataset after the Fill method List box selection Write code in the SelectedIndexChanged Event Specify the parameter value using the Parameters collection of the data adapter’s SelectCommand property © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.
40
Displaying the Data for a Selected Item – Code Example
Private Sub titleDropDownList_SelectedIndexChanged(ByVal _ sender As System.Object, ByVal e As System.EventArgs) _ Handles titleDropDownList.SelectedIndexChanged ' Retrieve the record matching the selection. If titleDropDownList.SelectedIndex <> -1 Then BooksDataset1.Clear( ) booksDataAdapter.SelectCommand.Parameters("Title").Value _ = titleDropDownList.SelectedItem.Text booksDataAdapter.Fill(BooksDataset1) ISBNLabel.DataBind( ) authorLabel.DataBind( ) End If End Sub © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.
41
Making a Database Project Portable
You must move database AND Create Connection on new computer Configure DataAdapter For Web projects, also create virtual directory Less problems if the database is stored in the bin folder of the project Do not include path in the data adapter’s ConnectionString property © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.