csci 3327 visual basic chapter 13: databases and linq utpa – fall 2011

Post on 17-Jan-2016

214 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

CSCI 3327 Visual Basic CSCI 3327 Visual Basic Chapter 13: Databases and LINQChapter 13: Databases and LINQ

UTPA – Fall 2011

Objectives

• In this chapter, you will– Learn the relational database model– Get familiar with using LINQ to retrieve and

manipulate data from a database– Know how to add data sources to project, create

LINQ to SQL classes, and use data binding to move data between GUI controls and databases

– Learn how to create Master/Detail views to select a record and display its details

2

Example 12.20: TitleQueries.vb

• URL:– http://media.pearsoncmg.com/ph/esm/deitel/vb_ht

p_2010/codeexamples.html

• GUI in the next page

3

Dynamically Binding Query Results

4

Creating the Display Query Results

• Step 1: Creating the Project– Create a new Windows Forms Application

named DisplayQueryResult– Rename the source file to TitleQueries.vb.– Set the Form’s Text property to Display Query

Results

• Step 2: Creating the LINQ to SQL Classes– Add the Books database to the project– Generate the LINQ to SQL classes

5

Creating the Display Query Results (cont'd)

• Step 3: Creating a DataGridView to Display the Titles Table– Create the data source and the DataGridView.– Select the Title class (rather than the Author class)

as the data source– Drag the Title node from the Data Sources window

onto the form

6

Creating the Display Query Results (cont'd)

• Step 4: Adding a ComboBox to the Form– In Design view, add a ComboBox named

queriesComboBox below the DataGridView on the Form

– Open the ComboBox’s String Collection Editor by right clicking the ComboBox and selecting Edit Items

• All titles• Titles with 2008 copyright• Titles ending with "How to Program"

7

Coding the Display Query Results

8

Coding the Display Query Results (cont'd)

9

Retrieving Data from Multiple Tables with LINQ

• Join two tables with LINQ

• Example 12.22:– URL:

http://media.pearsoncmg.com/ph/esm/deitel/vb_htp_2010/codeexamples.html

10

Retrieving Data from Multiple Tables with LINQ (cont'd)

11

Other LINQ Queries

• Let query operator– Declare a new variable in an LINQ query

12

Create the Master/Detail GUI

13

Create the Master/Detail GUI (cont'd)

14

Steps to Create the Master/Detail GUI

• Step 1: Creating the Project– Create a new Windows Forms Application called

MasterDetail– Name the source file Details.vb and set the Form’s

Text property to Master/Detail

• Step 2: Creating LINQ to SQL Classes– Add the Books database– Create the LINQ to SQL classes to interact with

the database15

Steps to Create the Master/Detail GUI (cont'd)

• Step 3: Creating GUI Elements– Add two Labels and two ComboBoxes to the top

of the Form

16

Steps to Create the Master/Detail GUI (cont'd)

• Create a DataGridView, booksDataGridView, to hold the details that are displayed– Do not automatically create it by dragging a data

source from the Data Sources window

– Instead, drag the DataGridView from the Toolbox

– Add two BindingSources, namely titleBindingSource and authorBindingSource, from the Data section of the Toolbox, one for information from the Titles table and one for information from the Authors table

17

Master/Detail GUI

• Nested classes– Class AuthorBinding is intended for use only in

this example– So it is defined inside class Details—it’s a so-

called nested class

18

Master/Detail GUI (cont'd)

• DisplayMember = "Name"– Tell the ComboBox to use the Name property of

the objects in its DataSource to determine what text to display for each item

19

Master/Detail GUI (cont'd)

20

Exercises

• A table in a relational database consists of ____ and ____ in which values are stored.

• The _____uniquely identifies each row in a relational database table.

• A relational database can be manipulated in LINQ to SQL via a(n) ____ object, which contains properties for accessing each table in the database.

• Merging data from multiple relational database tables is called _____ the data.

• A(n) is a column (or groups of column) in a relational database table that matches the primary-key column (groups of column) in another table.

21

True / False Statements

• Providing the same value for a foreign key in multiple rows causes the DBMS to report an error.

• Providing a foreign-key value that does not appear as a primary-key value in another table is an error.

• The result of a query can be sorted in ascending or descending order.

• LINQ to SQL automatically saves changes made back to the database.

22

23

top related