31
1 Chapter 20 – Data sources and datasets Outline How to create a data source How to use a data source How to use Query Builder to build a simple query How to use the Dataset Designer How to generate detail controls from a data source How to use Query Builder to build more complicated parameterized queries

1 Chapter 20 – Data sources and datasets Outline How to create a data source How to use a data source How to use Query Builder to build a simple query

Embed Size (px)

Citation preview

Page 1: 1 Chapter 20 – Data sources and datasets Outline How to create a data source How to use a data source How to use Query Builder to build a simple query

1

Chapter 20 – Data sources and datasets

OutlineHow to create a data source

How to use a data source

How to use Query Builder to build a simple query

How to use the Dataset Designer

How to generate detail controls from a data source

How to use Query Builder to build more complicated

parameterized queries

Page 2: 1 Chapter 20 – Data sources and datasets Outline How to create a data source How to use a data source How to use Query Builder to build a simple query

2

How to create a data source

• Data Source – specifies the source of the data for the application– most apps get their data from a database

• Data source window shows all tables and columns in the dataset that are available to the application.– display by clicking on Data Sources tab at left edge – display by selecting Data > Show Data Sources

• If not data sources available, click on Add New Data Source link.– Starts the Data Source Configuration Wizard– actually 4 different ways to start this Wizard (pg. 535)

Page 3: 1 Chapter 20 – Data sources and datasets Outline How to create a data source How to use a data source How to use Query Builder to build a simple query

3

Data Sources

Page 4: 1 Chapter 20 – Data sources and datasets Outline How to create a data source How to use a data source How to use Query Builder to build a simple query

4

Page 5: 1 Chapter 20 – Data sources and datasets Outline How to create a data source How to use a data source How to use Query Builder to build a simple query

5

Page 6: 1 Chapter 20 – Data sources and datasets Outline How to create a data source How to use a data source How to use Query Builder to build a simple query

6

Choosing the connection for the data source

• Can choose from a previously defined connection

• Or click New Connection

• Click the Connection string button to make sure you have the right connection

Page 7: 1 Chapter 20 – Data sources and datasets Outline How to create a data source How to use a data source How to use Query Builder to build a simple query

7

Creating a connection to a database

• How you do this differs for– SQL Server Express (default)– Microsoft Access– using a database server running on a network server– etc.

• WE ARE USING MICROSOFT ACCESS – NO WHINING!!!– Change the data source to Microsoft Access Database File– Enter the database filename

Page 8: 1 Chapter 20 – Data sources and datasets Outline How to create a data source How to use a data source How to use Query Builder to build a simple query

8

Page 9: 1 Chapter 20 – Data sources and datasets Outline How to create a data source How to use a data source How to use Query Builder to build a simple query

9

Creating a connection to a database

– Click Test Connection to be sure connection is configured properly

– Save the Connection string in the app configuration file• Now table adapters that use the connection can refer to the

connection string by name

Page 10: 1 Chapter 20 – Data sources and datasets Outline How to create a data source How to use a data source How to use Query Builder to build a simple query

10

If you add a database file to your project

– By default, the DB file is copied to the output directory for the project every time the project is built

– When you run the app, it works with the copy of the database file in the output directory

– Any changes made to the database aren’t applied to the database file in the project directory

– Each time you rebuild, database in output directory is overwritten– Change this , by selecting database file in Solution Explorer and

change its “Copy to Output Directory” property to “Copy if newer”

Page 11: 1 Chapter 20 – Data sources and datasets Outline How to create a data source How to use a data source How to use Query Builder to build a simple query

11

Choosing database objects for a data source

• Last step of the Data Source Configuration Wizard– Choose any tables, views, stored procedures, functions

available from database– Can expand nodes and just choose columns– Can also enter the name you want to use for the dataset – Default: databasename + “DataSet”

• NOTE: selection of several tables in the dataset, maintains the relationships between those tables

Page 12: 1 Chapter 20 – Data sources and datasets Outline How to create a data source How to use a data source How to use Query Builder to build a simple query

12

Page 13: 1 Chapter 20 – Data sources and datasets Outline How to create a data source How to use a data source How to use Query Builder to build a simple query

13

Page 14: 1 Chapter 20 – Data sources and datasets Outline How to create a data source How to use a data source How to use Query Builder to build a simple query

14

Schema file

• After completing Data Source Configuration Wizard– new data source is displayed in Data Sources Window– generates a file that contains the schema for the DataSet class

• defines the structure of the dataset, incl. tables and columns it contains, data types, and constraints

• listed in Solution Explorer Window with extension .xsd• double- click it to view graphic representation• generated code is below it• when you create bound controls from the data source, the code in this

class is used to define the DataSet object that the controls are bound to – DO NOT CHANGE THIS CODE!

Page 15: 1 Chapter 20 – Data sources and datasets Outline How to create a data source How to use a data source How to use Query Builder to build a simple query

15

Schema file, generated code file for DataSet

Page 16: 1 Chapter 20 – Data sources and datasets Outline How to create a data source How to use a data source How to use Query Builder to build a simple query

16

Page 17: 1 Chapter 20 – Data sources and datasets Outline How to create a data source How to use a data source How to use Query Builder to build a simple query

17

How to use a data source

• Bind controls to the data source and then use the bound controls to add, update, and delete the data in the data source.

• We’ll use DataGridView and TextBox controls in our examples.

• Steps– Drag a table from the Data Sources Window onto a

form• adds a DataGridView control to the form and binds it to the

table - allows browsing of rows, as well as add, update and delete rows (complex data binding)

• adds five more objects to the Component Designer tray

Page 18: 1 Chapter 20 – Data sources and datasets Outline How to create a data source How to use a data source How to use Query Builder to build a simple query

18

Page 19: 1 Chapter 20 – Data sources and datasets Outline How to create a data source How to use a data source How to use Query Builder to build a simple query

Objects in Component Designer Tray

• DataSet– defines the dataset that contains the Authors table

• TableAdapter – provides commands that can be used to work with the Authors table

• TableAdapterManager – provides for writing the data in two or more related tables to the database, so referential integrity is maintained

• BindingSource - specifies the data source (the Authors table) that the controls are bound to and provides functionality for working with the data source

• BindingNavigator – defines the toolbar that contains the controls for working with the data structure

19

Page 20: 1 Chapter 20 – Data sources and datasets Outline How to create a data source How to use a data source How to use Query Builder to build a simple query

©1992-2014 by Pearson Education, Inc. All Rights Reserved.

Created when data source representing Authors table is dragged onto the form

Page 21: 1 Chapter 20 – Data sources and datasets Outline How to create a data source How to use a data source How to use Query Builder to build a simple query

Generated code

• When application starts, the first event handler is executed – it uses the Fill method of the TableAdapter object to load

data into the DataSet object– because the DataGridView control is bound to this table,

the data is displayed in this control

21

private void Form1_Load(object sender, EventArgs e) { this.authorsTableAdapter.Fill(this.booksDataSet.Authors); }

Page 22: 1 Chapter 20 – Data sources and datasets Outline How to create a data source How to use a data source How to use Query Builder to build a simple query

Generated code

• When user changes the data in the DataGridView control– changes are saved back to the dataset– changes are not saved to database until user clicks the Save

button• calls Validate method• calls EndEdit method of the BindingSource – apply any

pending changes to the dataset (added/updated rows are not saved until you move to another row

• calls UpdateAll of the TableAdapterManager – saves the data in the DataSet object to the database (only updates rows that need updating maintaining referential integrity)

22

Page 23: 1 Chapter 20 – Data sources and datasets Outline How to create a data source How to use a data source How to use Query Builder to build a simple query

Generated code

user clicks the Save button

23

private void authorsBindingNavigatorSaveItem_Click (object sender, EventArgs e) { this.Validate(); this.authorsBindingSource.EndEdit(); this.tableAdapterManager.UpdateAll(this.booksDataSet);}

Page 24: 1 Chapter 20 – Data sources and datasets Outline How to create a data source How to use a data source How to use Query Builder to build a simple query

How to use Query Builder

• DataSource Configuration Wizard doesn’t give you enough flexibility, e.g., can’t join data or sort

• Query Builder provides a graphical interface that you can use to modify a Select statement without knowing proper SQL syntax

• Steps:– Select the table adapter that contains the query – Right click Add query,– Change query name if a new query– Click Query Builder

24

Page 25: 1 Chapter 20 – Data sources and datasets Outline How to create a data source How to use a data source How to use Query Builder to build a simple query

Type new name for query

Click Query Builder

25

Page 26: 1 Chapter 20 – Data sources and datasets Outline How to create a data source How to use a data source How to use Query Builder to build a simple query

26

Diagram Pane right click to add tables to pane

Grid Pane shows all columns selected

SQL Pane

Results Pane

Page 27: 1 Chapter 20 – Data sources and datasets Outline How to create a data source How to use a data source How to use Query Builder to build a simple query

27

Page 28: 1 Chapter 20 – Data sources and datasets Outline How to create a data source How to use a data source How to use Query Builder to build a simple query

How to use the Dataset Designer

• Lets you work with a dataset schema using a graphic interface

• To view the schema – double-click on the schema file for the dataset (.xsd file)

• Properties you can view– table adapter, query, columns in a table

• Example: Fix the autoincrement of the authorID column in the Authors table

28

Page 29: 1 Chapter 20 – Data sources and datasets Outline How to create a data source How to use a data source How to use Query Builder to build a simple query

Generating detail controls from a data source

• DataGridView may not be appropriate for app• Can bind columns of data source to individual

controls (simple data binding)• Select Details option from drop-down list when

you select the table in Data Sources Window• Drag the table onto the form• Generates a label and a bound control for each

column– most string/numeric columns TextBox– Change to other appropriate types by selecting column in

Data Sources Window and the select a different type of control from drop-down list

29

Page 30: 1 Chapter 20 – Data sources and datasets Outline How to create a data source How to use a data source How to use Query Builder to build a simple query

30

Page 31: 1 Chapter 20 – Data sources and datasets Outline How to create a data source How to use a data source How to use Query Builder to build a simple query

31