+ All Categories
Home > Documents > Advanced Visual Basic 2005 4 th Edition Chapter 3: Using SQL Server Databases (c) 2007 Pearson...

Advanced Visual Basic 2005 4 th Edition Chapter 3: Using SQL Server Databases (c) 2007 Pearson...

Date post: 16-Jan-2016
Category:
Upload: elwin-turner
View: 213 times
Download: 0 times
Share this document with a friend
63
Advanced Visual Basic 2005 Advanced Visual Basic 2005 4 4 th th Edition Edition Chapter 3: Using SQL Server Databases (c) 2007 Pearson Education Inc. All rights reserved. You may modify and copy this slide show for your personal use, or for use in the classroom, as long as this copyright statement, the author's names and title are not changed. Slides prepared by the authors Revision date: Jan 18, 2007 by Kip Irvine and Tony Gaddis
Transcript
Page 1: Advanced Visual Basic 2005 4 th Edition Chapter 3: Using SQL Server Databases (c) 2007 Pearson Education Inc. All rights reserved. You may modify and copy.

Advanced Visual Basic 2005 Advanced Visual Basic 2005 44thth Edition Edition

Chapter 3: Using SQL Server Databases

(c) 2007 Pearson Education Inc. All rights reserved. You may modify and copy this slide show for your personal use, or for use in the classroom, as long as this copyright statement, the author's names and title are not changed.

Slides prepared by the authors

Revision date: Jan 18, 2007

by Kip Irvine and Tony Gaddis

Page 2: Advanced Visual Basic 2005 4 th Edition Chapter 3: Using SQL Server Databases (c) 2007 Pearson Education Inc. All rights reserved. You may modify and copy.

Advanced Visual Basic 2005, 4th Edition. © Pearson Education Inc. 2

OverviewOverview

• Database Basics• SQL SELECT Statement• Using the DataGridView• Selecting DataSet Rows• Data-Bound Controls• Karate School Manager Application

Page 3: Advanced Visual Basic 2005 4 th Edition Chapter 3: Using SQL Server Databases (c) 2007 Pearson Education Inc. All rights reserved. You may modify and copy.

Advanced Visual Basic 2005, 4th Edition. © Pearson Education Inc. 3

Database BasicsDatabase Basics

Page 4: Advanced Visual Basic 2005 4 th Edition Chapter 3: Using SQL Server Databases (c) 2007 Pearson Education Inc. All rights reserved. You may modify and copy.

Advanced Visual Basic 2005, 4th Edition. © Pearson Education Inc. 4

IntroductionIntroduction

• Database holds collection of tables• Table is logical grouping of related information

• rows – each row holds a logical unit

• columns – similar to attributes

• Example:

Page 5: Advanced Visual Basic 2005 4 th Edition Chapter 3: Using SQL Server Databases (c) 2007 Pearson Education Inc. All rights reserved. You may modify and copy.

Advanced Visual Basic 2005, 4th Edition. © Pearson Education Inc. 5

Primary KeyPrimary Key

• Column or columns that uniquely identify each row• Can be any data type• Can be auto-generated (identity)• Departments table:

• dept_id column is the primary key

Page 6: Advanced Visual Basic 2005 4 th Edition Chapter 3: Using SQL Server Databases (c) 2007 Pearson Education Inc. All rights reserved. You may modify and copy.

Advanced Visual Basic 2005, 4th Edition. © Pearson Education Inc. 6

SQL Server Data TypesSQL Server Data Types

• Microsoft SQL Server• .NET uses SqlDbType to identify data types

Page 7: Advanced Visual Basic 2005 4 th Edition Chapter 3: Using SQL Server Databases (c) 2007 Pearson Education Inc. All rights reserved. You may modify and copy.

Advanced Visual Basic 2005, 4th Edition. © Pearson Education Inc. 7

Designing Database TablesDesigning Database Tables

• Database schema• design of tables, columns, and relationships

• Choose descriptive column names• Use data types that match the data values• Sample Members table:

Page 8: Advanced Visual Basic 2005 4 th Edition Chapter 3: Using SQL Server Databases (c) 2007 Pearson Education Inc. All rights reserved. You may modify and copy.

Advanced Visual Basic 2005, 4th Edition. © Pearson Education Inc. 8

Sample Column TypesSample Column Types

• Primary key – Int type• Person's last name: VarChar type• Salary: Decimal type• Ratio: Float type• Boolean (True/False): Bit type• Birth date: DateTime or SmallDateTime type• Picture: Image type

Page 9: Advanced Visual Basic 2005 4 th Edition Chapter 3: Using SQL Server Databases (c) 2007 Pearson Education Inc. All rights reserved. You may modify and copy.

Advanced Visual Basic 2005, 4th Edition. © Pearson Education Inc. 9

One-To-Many RelationshipOne-To-Many Relationship

• Relational model• relations between tables

• minimizes duplication of data within table rows

• Primary Key referenced by Foreign Key• Example:

Page 10: Advanced Visual Basic 2005 4 th Edition Chapter 3: Using SQL Server Databases (c) 2007 Pearson Education Inc. All rights reserved. You may modify and copy.

Advanced Visual Basic 2005, 4th Edition. © Pearson Education Inc. 10

CheckpointCheckpoint

1. How is a table different from a database?

2. In a table of employees, what column would make a good primary key?

3. Which Visual Basic data type is equivalent to the Bit column type in SQL Server?

4. Why would we not want to spell out the name of each person’s department name in a table of employees?

5. How is a foreign key different from a primary key?

Page 11: Advanced Visual Basic 2005 4 th Edition Chapter 3: Using SQL Server Databases (c) 2007 Pearson Education Inc. All rights reserved. You may modify and copy.

Advanced Visual Basic 2005, 4th Edition. © Pearson Education Inc. 11

SQL SELECT StatementSQL SELECT Statement

Page 12: Advanced Visual Basic 2005 4 th Edition Chapter 3: Using SQL Server Databases (c) 2007 Pearson Education Inc. All rights reserved. You may modify and copy.

Advanced Visual Basic 2005, 4th Edition. © Pearson Education Inc. 12

SELECT StatementSELECT Statement

• SQL – Structured Query Language• Standardized by ANSI (American National Standards

Institute)

• SELECT returns rows from one or more tables• Example 1:

SELECT ID, Salary

FROM SalesStaff

• Example 2:SELECT [Last Name], [First Name]

FROM Employees

• Example 3:SELECT *

FROM SalesStaff

Page 13: Advanced Visual Basic 2005 4 th Edition Chapter 3: Using SQL Server Databases (c) 2007 Pearson Education Inc. All rights reserved. You may modify and copy.

Advanced Visual Basic 2005, 4th Edition. © Pearson Education Inc. 13

Setting the Row Order with ORDER BYSetting the Row Order with ORDER BY

• Format:ORDER BY columnName [ASC | DESC]

• Examples:ORDER BY Last_Name ASC

ORDER BY Last_Name

ORDER BY Last_Name DESC

ORDER BY Last_Name, First_Name

• Complete example:SELECT ID, Salary

FROM SalesStaff

ORDER BY ID

Page 14: Advanced Visual Basic 2005 4 th Edition Chapter 3: Using SQL Server Databases (c) 2007 Pearson Education Inc. All rights reserved. You may modify and copy.

Advanced Visual Basic 2005, 4th Edition. © Pearson Education Inc. 14

Selecting Rows with the WHERE ClauseSelecting Rows with the WHERE Clause

• WHERE clause filters (selects) rows returned by the SELECT statement

• Simplest formatWHERE columnName = value

• Example:SELECT First_Name, Last_Name, Salary

FROM SalesStaff

WHERE Last_Name = 'Gomez'

Page 15: Advanced Visual Basic 2005 4 th Edition Chapter 3: Using SQL Server Databases (c) 2007 Pearson Education Inc. All rights reserved. You may modify and copy.

Advanced Visual Basic 2005, 4th Edition. © Pearson Education Inc. 15

Relational OperatorsRelational Operators

• Operators permitted in WHERE clause expressions:

Page 16: Advanced Visual Basic 2005 4 th Edition Chapter 3: Using SQL Server Databases (c) 2007 Pearson Education Inc. All rights reserved. You may modify and copy.

Advanced Visual Basic 2005, 4th Edition. © Pearson Education Inc. 16

Numeric and Date ValuesNumeric and Date Values

• Numeric literals have no delimiters or commas• Parentheses are optional

WHERE Salary > 30000

WHERE (Salary > 30000)

• DateTime values are delimited by single quotesWHERE (Hire_Date > '12/31/1999')

Page 17: Advanced Visual Basic 2005 4 th Edition Chapter 3: Using SQL Server Databases (c) 2007 Pearson Education Inc. All rights reserved. You may modify and copy.

Advanced Visual Basic 2005, 4th Edition. © Pearson Education Inc. 17

CheckpointCheckpoint

1. Write a SELECT statement that retrieves the pay_rate, employee_id, and hours_worked columns from a table named Payroll, and sorts the rows in descending order by hours_worked.

2. Write a SELECT statement that creates an alias named Rate_of_Pay for the existing column named pay_rate in the Payroll table.

3. Write a SELECT statement for the Payroll table that creates a new output column named gross_pay by multiplying the pay_rate column by the hours_worked column.

4. Write a SELECT statement for the Payroll table that returns only rows in which the pay_rate is greater than 20,000 and less than or equal to 55,000.

5. Write a SELECT statement for the Payroll table that returns only rows in which the employee_id column begins with the characters FT. The remaining characters in the employee_id are unimportant.

Page 18: Advanced Visual Basic 2005 4 th Edition Chapter 3: Using SQL Server Databases (c) 2007 Pearson Education Inc. All rights reserved. You may modify and copy.

Advanced Visual Basic 2005, 4th Edition. © Pearson Education Inc. 18

Using the DataGridViewUsing the DataGridView

Page 19: Advanced Visual Basic 2005 4 th Edition Chapter 3: Using SQL Server Databases (c) 2007 Pearson Education Inc. All rights reserved. You may modify and copy.

Advanced Visual Basic 2005, 4th Edition. © Pearson Education Inc. 19

Introducing the DataGridViewIntroducing the DataGridView

• Displays data in rows and columns• Usually bound to a DataTable• User can add, edit, and delete rows• Customizable (design time & run time)

Page 20: Advanced Visual Basic 2005 4 th Edition Chapter 3: Using SQL Server Databases (c) 2007 Pearson Education Inc. All rights reserved. You may modify and copy.

Advanced Visual Basic 2005, 4th Edition. © Pearson Education Inc. 20

Data-Related ComponentsData-Related Components

• Data source• connects to database or file containing data

• TableAdapter• transfers data from data source to the application

• may also update the data source

• DataSet• in-memory copy of data from the data source

• filled by TableAdapter

• BindingSource• optional link between DataSet and bound controls

BindingSource

TableAdapter

DataSet ApplicationData

Source

(Information flows in both directions)

Page 21: Advanced Visual Basic 2005 4 th Edition Chapter 3: Using SQL Server Databases (c) 2007 Pearson Education Inc. All rights reserved. You may modify and copy.

Advanced Visual Basic 2005, 4th Edition. © Pearson Education Inc. 21

Formatting Columns in a DataGridViewFormatting Columns in a DataGridView

• Select the Columns property• DefaultCellStyle property

• Format property

Page 22: Advanced Visual Basic 2005 4 th Edition Chapter 3: Using SQL Server Databases (c) 2007 Pearson Education Inc. All rights reserved. You may modify and copy.

Advanced Visual Basic 2005, 4th Edition. © Pearson Education Inc. 22

Tutorial 3-1Tutorial 3-1

• Showing a database table in a DataGridView control• SalesStaff table in the Company database

Page 23: Advanced Visual Basic 2005 4 th Edition Chapter 3: Using SQL Server Databases (c) 2007 Pearson Education Inc. All rights reserved. You may modify and copy.

Advanced Visual Basic 2005, 4th Edition. © Pearson Education Inc. 23

Tutorial 3-1Tutorial 3-1

• Configure the DataGridView tasks• Add a new connection

• VS copies database into the project folder

Page 24: Advanced Visual Basic 2005 4 th Edition Chapter 3: Using SQL Server Databases (c) 2007 Pearson Education Inc. All rights reserved. You may modify and copy.

Advanced Visual Basic 2005, 4th Edition. © Pearson Education Inc. 24

Tutorial 3-1 Tutorial 3-1 (continued)(continued)

• Select the SalesStaff table• Display in a DataGridView

• Fill the SalesStaff DataTable:

Page 25: Advanced Visual Basic 2005 4 th Edition Chapter 3: Using SQL Server Databases (c) 2007 Pearson Education Inc. All rights reserved. You may modify and copy.

Advanced Visual Basic 2005, 4th Edition. © Pearson Education Inc. 25

CheckpointCheckpoint

1. Using the application you created in Tutorial 3-1, explain how to change the formatting of the Salary column so it displays values in currency format.

2. The technique called ________ links database tables to controls on Visual Basic forms.

3. Which component pulls data from one or more database tables and passes it into a DataSet?

4. When changes are made to a DataSet at runtime, what happens to the database that filled the DataSet?

5. Which control displays DataSets in a spreadsheet-like format?

6. What type of object connects a program to a database?

Page 26: Advanced Visual Basic 2005 4 th Edition Chapter 3: Using SQL Server Databases (c) 2007 Pearson Education Inc. All rights reserved. You may modify and copy.

Advanced Visual Basic 2005, 4th Edition. © Pearson Education Inc. 26

Selecting DataSet RowsSelecting DataSet Rows

Page 27: Advanced Visual Basic 2005 4 th Edition Chapter 3: Using SQL Server Databases (c) 2007 Pearson Education Inc. All rights reserved. You may modify and copy.

Advanced Visual Basic 2005, 4th Edition. © Pearson Education Inc. 27

SQLSQL

• Structured Query Language• commonly used statements

SELECT, DELETE, INSERT, UPDATE, ...

• SELECT queries a database• retrieves rows

• Updating a database• INSERT

• DELETE

• UPDATE

Page 28: Advanced Visual Basic 2005 4 th Edition Chapter 3: Using SQL Server Databases (c) 2007 Pearson Education Inc. All rights reserved. You may modify and copy.

Advanced Visual Basic 2005, 4th Edition. © Pearson Education Inc. 28

Modifying the Query in a Data SourceModifying the Query in a Data Source

• Locate DataSet's schema file in the Solution Explorer window

• open the DataSet designer tool:

• Use Query Builder:

Page 29: Advanced Visual Basic 2005 4 th Edition Chapter 3: Using SQL Server Databases (c) 2007 Pearson Education Inc. All rights reserved. You may modify and copy.

Advanced Visual Basic 2005, 4th Edition. © Pearson Education Inc. 29

Query BuilderQuery Builder

• Diagram pane• Grid pane• SQL pane• Results pane

diagram pane

grid pane

SQL pane

result pane

Page 30: Advanced Visual Basic 2005 4 th Edition Chapter 3: Using SQL Server Databases (c) 2007 Pearson Education Inc. All rights reserved. You may modify and copy.

Advanced Visual Basic 2005, 4th Edition. © Pearson Education Inc. 30

Adding a SELECT Query to a DataGridViewAdding a SELECT Query to a DataGridView

• Right-click the TableAdapter icon• select Add Query

• Search Criteria Builder window

• ToolStrip added to the form, containing a button

Page 31: Advanced Visual Basic 2005 4 th Edition Chapter 3: Using SQL Server Databases (c) 2007 Pearson Education Inc. All rights reserved. You may modify and copy.

Advanced Visual Basic 2005, 4th Edition. © Pearson Education Inc. 31

Tutorial 3-2Tutorial 3-2

• Filtering rows in the SalesStaff table• Display results in a DataGridView• Query:

Page 32: Advanced Visual Basic 2005 4 th Edition Chapter 3: Using SQL Server Databases (c) 2007 Pearson Education Inc. All rights reserved. You may modify and copy.

Advanced Visual Basic 2005, 4th Edition. © Pearson Education Inc. 32

CheckpointCheckpoint

1. What does the acronym SQL represent, in relation to databases?

2. Why do SQL queries work with any relational database?

3. Write an SQL SELECT statement that retrieves the First_Name and Last_Name columns from a table named Employees.

4. How do you add a query to a TableAdapter in the component tray of a form?

5. Write a WHERE clause in SQL that limits the returned data to rows in which Salary is less than or equal to $85,000.

Page 33: Advanced Visual Basic 2005 4 th Edition Chapter 3: Using SQL Server Databases (c) 2007 Pearson Education Inc. All rights reserved. You may modify and copy.

Advanced Visual Basic 2005, 4th Edition. © Pearson Education Inc. 33

Data-Bound ControlsData-Bound Controls

Page 34: Advanced Visual Basic 2005 4 th Edition Chapter 3: Using SQL Server Databases (c) 2007 Pearson Education Inc. All rights reserved. You may modify and copy.

Advanced Visual Basic 2005, 4th Edition. © Pearson Education Inc. 34

Describing Data-Bound ControlsDescribing Data-Bound Controls

• Automatically display the contents of the current table or table row

• Update their contents automatically when the user moves through rows in a DataSet

• Can update the contents of the DataSet

Page 35: Advanced Visual Basic 2005 4 th Edition Chapter 3: Using SQL Server Databases (c) 2007 Pearson Education Inc. All rights reserved. You may modify and copy.

Advanced Visual Basic 2005, 4th Edition. © Pearson Education Inc. 35

Visual Studio Copies Database FilesVisual Studio Copies Database Files

• When you attach an SQL Server database file• Makes a local copy of the database

• Updates made at runtime only affect a temporary copy of the database

• saved in the \bin\Debug or \bin\Release folder

• replaced each time the program runs

Page 36: Advanced Visual Basic 2005 4 th Edition Chapter 3: Using SQL Server Databases (c) 2007 Pearson Education Inc. All rights reserved. You may modify and copy.

Advanced Visual Basic 2005, 4th Edition. © Pearson Education Inc. 36

Modifying a Connection StringModifying a Connection String

• Right-click project name in Solution Explorer window• select the Settings tab

• click the Browse button

Page 37: Advanced Visual Basic 2005 4 th Edition Chapter 3: Using SQL Server Databases (c) 2007 Pearson Education Inc. All rights reserved. You may modify and copy.

Advanced Visual Basic 2005, 4th Edition. © Pearson Education Inc. 37

Binding Data Source to DataGridViewBinding Data Source to DataGridView

• Existing data source• Drag onto a form

• VS inserts a DataGridView and navigation bar

Page 38: Advanced Visual Basic 2005 4 th Edition Chapter 3: Using SQL Server Databases (c) 2007 Pearson Education Inc. All rights reserved. You may modify and copy.

Advanced Visual Basic 2005, 4th Edition. © Pearson Education Inc. 38

Binding Individual Fields to ColumnsBinding Individual Fields to Columns

• Select Details in the table's dropdown list inside the Data Sources window

• Drag the table onto a form• or drag individual columns onto the form

• select control binding type

• edit the fields as needed

Page 39: Advanced Visual Basic 2005 4 th Edition Chapter 3: Using SQL Server Databases (c) 2007 Pearson Education Inc. All rights reserved. You may modify and copy.

Advanced Visual Basic 2005, 4th Edition. © Pearson Education Inc. 39

Introducing the Karate DatabaseIntroducing the Karate Database

• Karate school management database• Tables

• Members

• Payments

Page 40: Advanced Visual Basic 2005 4 th Edition Chapter 3: Using SQL Server Databases (c) 2007 Pearson Education Inc. All rights reserved. You may modify and copy.

Advanced Visual Basic 2005, 4th Edition. © Pearson Education Inc. 40

Binding Data Sources to ListBoxes and Binding Data Sources to ListBoxes and ComboBoxesComboBoxes

• DataSource property• identifies the DataTable supplying

the data

• DisplayMember property• identifies the displayed column

• ListBox or ComboBox can be used as a navigation or selection tool

• BindingSource repositions itself to the selected row

Page 41: Advanced Visual Basic 2005 4 th Edition Chapter 3: Using SQL Server Databases (c) 2007 Pearson Education Inc. All rights reserved. You may modify and copy.

Advanced Visual Basic 2005, 4th Edition. © Pearson Education Inc. 41

Tutorial 3-3Tutorial 3-3

• Displaying the Members table in a ListBox• add a new data source (Members table)

• attach it to the ListBox's DataSource property

• set the ListBox's DisplayMember property

• Run the application• when user selects a last name, the member's detailed

fields are displayed:

Page 42: Advanced Visual Basic 2005 4 th Edition Chapter 3: Using SQL Server Databases (c) 2007 Pearson Education Inc. All rights reserved. You may modify and copy.

Advanced Visual Basic 2005, 4th Edition. © Pearson Education Inc. 42

Adding Rows to DataSet TablesAdding Rows to DataSet Tables

•NewRow method• creates and returns an empty row with same structure

as the DataSet table:Dim row as DataRow = _

PaymentsDataSet.Payments.NewRow(

•Add method• adds a row to the table

• pass it the column values

• pass Nothing for autoincrement columnsPaymentsDataSet.Payments.Rows.Add(Nothing, _

5, '5/15/2006', 50D)

Page 43: Advanced Visual Basic 2005 4 th Edition Chapter 3: Using SQL Server Databases (c) 2007 Pearson Education Inc. All rights reserved. You may modify and copy.

Advanced Visual Basic 2005, 4th Edition. © Pearson Education Inc. 43

Updating, Inserting, Removing RowsUpdating, Inserting, Removing Rows

• TableAdapter Update method• writes changes to the underlying database

PaymentsTableAdapter.Update(PaymentsDataSet)

• TableAdapter Insert method• inserts a new row in a table

PaymentsTableAdapter.Insert(5, '5/15/2006', 50D)

• DataRow Remove method• requires a reference to a table row

Dim row As DataRow = _ PaymentsDataSet.Payments.FindByID(36)PaymentsDataSet.Payments.Rows.Remove(row)

Page 44: Advanced Visual Basic 2005 4 th Edition Chapter 3: Using SQL Server Databases (c) 2007 Pearson Education Inc. All rights reserved. You may modify and copy.

Advanced Visual Basic 2005, 4th Edition. © Pearson Education Inc. 44

Tutorial 3-4Tutorial 3-4

• Inserting rows in the Karate Payments table• uses DataGridView control

• calls the TableAdapter's Insert method

txtMemberId

txtAmount

txtDate

Page 45: Advanced Visual Basic 2005 4 th Edition Chapter 3: Using SQL Server Databases (c) 2007 Pearson Education Inc. All rights reserved. You may modify and copy.

Advanced Visual Basic 2005, 4th Edition. © Pearson Education Inc. 45

Using Loops with DataSetsUsing Loops with DataSets

• Iterate over the Rows collection of a DataTable• For-Each loop

• Example, using the Karate Payments table:

Page 46: Advanced Visual Basic 2005 4 th Edition Chapter 3: Using SQL Server Databases (c) 2007 Pearson Education Inc. All rights reserved. You may modify and copy.

Advanced Visual Basic 2005, 4th Edition. © Pearson Education Inc. 46

Tutorial 3-5Tutorial 3-5

• Adding a total to the Insert_Karate_Payments application

• continues Tutorial 3-4

• Click handler for the Total Payments button:

Page 47: Advanced Visual Basic 2005 4 th Edition Chapter 3: Using SQL Server Databases (c) 2007 Pearson Education Inc. All rights reserved. You may modify and copy.

Advanced Visual Basic 2005, 4th Edition. © Pearson Education Inc. 47

CheckpointCheckpoint

1. Which Visual Studio window displays the list of data sources belonging to a project?

2. The ______ Configuration Wizard is a tool you can use to create a connection to a database and select a database table.

3. If a certain DataTable already exists, what is the easiest way to bind it to a DataGridView control?

4. How do you bind a single DataSet column to a text box?

5. By default, which control binds to a DateTime field in a DataSet?

6. What is the menu command for adding a new DataSet to the current project?

Page 48: Advanced Visual Basic 2005 4 th Edition Chapter 3: Using SQL Server Databases (c) 2007 Pearson Education Inc. All rights reserved. You may modify and copy.

Advanced Visual Basic 2005, 4th Edition. © Pearson Education Inc. 48

Karate School Manager ApplicationKarate School Manager Application

Page 49: Advanced Visual Basic 2005 4 th Edition Chapter 3: Using SQL Server Databases (c) 2007 Pearson Education Inc. All rights reserved. You may modify and copy.

Advanced Visual Basic 2005, 4th Edition. © Pearson Education Inc. 49

FeaturesFeatures

1. Displays a list of all members

2. Permits the user to sort on any column, edit individual rows, and delete rows

3. Adds new students to the list of members

4. Finds a member by letting the user enter a partial last name

5. Displays payments by all members, sorting on any column

6. Displays a list of payments by one member

Page 50: Advanced Visual Basic 2005 4 th Edition Chapter 3: Using SQL Server Databases (c) 2007 Pearson Education Inc. All rights reserved. You may modify and copy.

Advanced Visual Basic 2005, 4th Edition. © Pearson Education Inc. 50

Main FormMain Form

• Starts the application• File, Membership, and Payments menus

Page 51: Advanced Visual Basic 2005 4 th Edition Chapter 3: Using SQL Server Databases (c) 2007 Pearson Education Inc. All rights reserved. You may modify and copy.

Advanced Visual Basic 2005, 4th Edition. © Pearson Education Inc. 51

Membership FormsMembership Forms

• All Members

• Find Member by Last Name

• Add New Member

Page 52: Advanced Visual Basic 2005 4 th Edition Chapter 3: Using SQL Server Databases (c) 2007 Pearson Education Inc. All rights reserved. You may modify and copy.

Advanced Visual Basic 2005, 4th Edition. © Pearson Education Inc. 52

Payments FormsPayments Forms

• Payments by All Members• sort on any column

• Payments by One Member• select member from ComboBox

Page 53: Advanced Visual Basic 2005 4 th Edition Chapter 3: Using SQL Server Databases (c) 2007 Pearson Education Inc. All rights reserved. You may modify and copy.

Advanced Visual Basic 2005, 4th Edition. © Pearson Education Inc. 53

Tutorials 3-6 and 3-7Tutorials 3-6 and 3-7

• Tutorial 3-6• Creating the Karate School Manager startup form

• Tutorial 3-7• Listing all members

Page 54: Advanced Visual Basic 2005 4 th Edition Chapter 3: Using SQL Server Databases (c) 2007 Pearson Education Inc. All rights reserved. You may modify and copy.

Advanced Visual Basic 2005, 4th Edition. © Pearson Education Inc. 54

Using a BindingSourceUsing a BindingSource

• Connection between DataSet and the data-bound controls on a form

• updates the DataSet when user modifies data in bound controls

• Items collection

• BindingSource class methods and properties• DataSource

• AddNew

• RemoveCurrent

• EndEdit

• CancelEdit

Page 55: Advanced Visual Basic 2005 4 th Edition Chapter 3: Using SQL Server Databases (c) 2007 Pearson Education Inc. All rights reserved. You may modify and copy.

Advanced Visual Basic 2005, 4th Edition. © Pearson Education Inc. 55

Tutorial 3-8Tutorial 3-8

• Karate School Manager: Adding new members• Members table

• uses data binding

• writes changes to a temporary copy of database

Page 56: Advanced Visual Basic 2005 4 th Edition Chapter 3: Using SQL Server Databases (c) 2007 Pearson Education Inc. All rights reserved. You may modify and copy.

Advanced Visual Basic 2005, 4th Edition. © Pearson Education Inc. 56

Using Query Parameters

• Create simpler, more flexible SQL queries• Avoid concatenating SQL statements to control names:

• Define parameter name with @ prefix:

• TableAdapter's Fill method has a new parameter:MembersTableAdapter.Fill( _

Me.FindMemberDataSet.Members, txtLastName.Text)

Page 57: Advanced Visual Basic 2005 4 th Edition Chapter 3: Using SQL Server Databases (c) 2007 Pearson Education Inc. All rights reserved. You may modify and copy.

Advanced Visual Basic 2005, 4th Edition. © Pearson Education Inc. 57

Wildcard Matches in SQL Queries

• Use the LIKE operator• % matches any character string• Example

• Returns all last names starting with letter G:WHERE Last_name LIKE 'G%'

Page 58: Advanced Visual Basic 2005 4 th Edition Chapter 3: Using SQL Server Databases (c) 2007 Pearson Education Inc. All rights reserved. You may modify and copy.

Advanced Visual Basic 2005, 4th Edition. © Pearson Education Inc. 58

Tutorial 3-9Tutorial 3-9

• Karate School Manager: Finding members by name• accepts partial name string

• uses query parameter

• uses LIKE operator with wildcard matches

Page 59: Advanced Visual Basic 2005 4 th Edition Chapter 3: Using SQL Server Databases (c) 2007 Pearson Education Inc. All rights reserved. You may modify and copy.

Advanced Visual Basic 2005, 4th Edition. © Pearson Education Inc. 59

Tutorial 3-10Tutorial 3-10

• Karate School Manager: Listing payments by all members

• joins Payments, Members tables

• uses DataGridView control

Page 60: Advanced Visual Basic 2005 4 th Edition Chapter 3: Using SQL Server Databases (c) 2007 Pearson Education Inc. All rights reserved. You may modify and copy.

Advanced Visual Basic 2005, 4th Edition. © Pearson Education Inc. 60

Tutorial 3-11Tutorial 3-11

• Karate School Manager: Showing payments by one member

• user selects name from ComboBox

• payments shown in DataGridView

Page 61: Advanced Visual Basic 2005 4 th Edition Chapter 3: Using SQL Server Databases (c) 2007 Pearson Education Inc. All rights reserved. You may modify and copy.

Advanced Visual Basic 2005, 4th Edition. © Pearson Education Inc. 61

CheckpointCheckpoint

1. In the Karate database, which table contains the names and dates when students joined the school?

2. In the PaymentsAllForm form, which two tables are required when filling the grid?

3. Which property of a DataGridView control lets you alter the order in which columns appear?

4. How does the PaymentsOneForm form obtain the ID number of the member selected by the user in the ComboBox?

5. What special keyword is used in the WHERE clause of a query when you want to perform a wildcard comparison?

Page 62: Advanced Visual Basic 2005 4 th Edition Chapter 3: Using SQL Server Databases (c) 2007 Pearson Education Inc. All rights reserved. You may modify and copy.

Advanced Visual Basic 2005, 4th Edition. © Pearson Education Inc. 62

SummarySummary

• Database Basics• SQL SELECT Statement• Using the DataGridView• Selecting DataSet Rows• Data-Bound Controls• Karate School Manager Application

Page 63: Advanced Visual Basic 2005 4 th Edition Chapter 3: Using SQL Server Databases (c) 2007 Pearson Education Inc. All rights reserved. You may modify and copy.

Advanced Visual Basic 2005, 4th Edition. © Pearson Education Inc. 63

The EndThe End


Recommended