2006 pearson education, inc. all rights reserved. 1 20 database, sql and ado.net

44
1 2006 Pearson Education, Inc. All rights rese 2 0 Database, SQL and ADO.NET

Upload: rose-francis

Post on 02-Jan-2016

216 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 2006 Pearson Education, Inc. All rights reserved. 1 20 Database, SQL and ADO.NET

1

2006 Pearson Education, Inc. All rights reserved.

2020Database, SQL and

ADO.NET

Page 2: 2006 Pearson Education, Inc. All rights reserved. 1 20 Database, SQL and ADO.NET

2

2006 Pearson Education, Inc. All rights reserved.

It is a capital mistake to theorize before one has data.

— Arthur Conan Doyle

Now go, write it before them in a table, and note it in a book, that it may be for the time to come for ever and ever.

— Holy Bible, Isaiah 30:8

Get your facts first, and then you can distort them as much as you please.

— Mark Twain

I like two kinds of men: domestic and foreign. — Mae West

Page 3: 2006 Pearson Education, Inc. All rights reserved. 1 20 Database, SQL and ADO.NET

3

2006 Pearson Education, Inc. All rights reserved.

OBJECTIVES

In this chapter you will learn: The relational database model. To write basic database queries in SQL. To add data sources to projects. To use the IDE's drag-and-drop capabilities to display

database tables in applications. To use the classes of namespaces System.Data

and System.Data.SqlClient to manipulate databases.

To use ADO.NET's disconnected object model to store data from a database in local memory.

To create XML documents from data sources.

Page 4: 2006 Pearson Education, Inc. All rights reserved. 1 20 Database, SQL and ADO.NET

4

2006 Pearson Education, Inc. All rights reserved.

20.1 Introduction

20.2  Relational Databases

20.3  Relational Database Overview: Books Database

20.4  SQL

20.4.1 Basic SELECT Query

20.4.2 WHERE Clause

20.4.3 ORDER BY Clause

20.4.4 Merging Data from Multiple Tables: INNER JOIN

20.4.5 INSERT Statement

20.4.6 UPDATE Statement

20.4.7 DELETE Statement

20.5  ADO.NET Object Model

Page 5: 2006 Pearson Education, Inc. All rights reserved. 1 20 Database, SQL and ADO.NET

5

2006 Pearson Education, Inc. All rights reserved.

20.6 Programming with ADO.NET: Extracting Information from a Database

20.6.1 Displaying a Database Table in a DataGridView

20.6.2 How Data Binding Works

20.7 Querying the Books Database

20.8   Programming with ADO.NET: Address Book Case Study

20.9   Using a DataSet to Read and Write XML

20.10 Wrap-Up

20.11 Web Resources

Page 6: 2006 Pearson Education, Inc. All rights reserved. 1 20 Database, SQL and ADO.NET

6

2006 Pearson Education, Inc. All rights reserved.

Fig. 20.23 | Adding a data source to a project.

Data Sources windowData menu

Page 7: 2006 Pearson Education, Inc. All rights reserved. 1 20 Database, SQL and ADO.NET

7

2006 Pearson Education, Inc. All rights reserved.

Fig. 20.24 | Choosing the data source type in the Data Source Configuration Wizard.

Page 8: 2006 Pearson Education, Inc. All rights reserved. 1 20 Database, SQL and ADO.NET

8

2006 Pearson Education, Inc. All rights reserved.

Fig. 20.25 | Adding a new data connection.

Page 9: 2006 Pearson Education, Inc. All rights reserved. 1 20 Database, SQL and ADO.NET

9

2006 Pearson Education, Inc. All rights reserved.

Fig. 20.26 | Choosing the Books.mdf data connection.

Page 10: 2006 Pearson Education, Inc. All rights reserved. 1 20 Database, SQL and ADO.NET

10

2006 Pearson Education, Inc. All rights reserved.

Fig. 20.27 | Saving the connection string to the application configuration file.

Page 11: 2006 Pearson Education, Inc. All rights reserved. 1 20 Database, SQL and ADO.NET

11

2006 Pearson Education, Inc. All rights reserved.

Fig. 20.28 | Choosing the database objects to include in the DataSet.

Page 12: 2006 Pearson Education, Inc. All rights reserved. 1 20 Database, SQL and ADO.NET

12

2006 Pearson Education, Inc. All rights reserved.

Fig. 20.29 | Viewing a data source listed in the Data Sources window.

Data Sources window

Page 13: 2006 Pearson Education, Inc. All rights reserved. 1 20 Database, SQL and ADO.NET

13

2006 Pearson Education, Inc. All rights reserved.

Fig. 20.30 | Viewing a database listed in the Solution Explorer.

Page 14: 2006 Pearson Education, Inc. All rights reserved. 1 20 Database, SQL and ADO.NET

14

2006 Pearson Education, Inc. All rights reserved.

Fig. 20.31 | Design view after dragging the Authors data source node to the Form.

authorsBindingNavigator authorsDataGridView

Component tray

Page 15: 2006 Pearson Education, Inc. All rights reserved. 1 20 Database, SQL and ADO.NET

15

2006 Pearson Education, Inc. All rights reserved.

Fig. 20.32 | Displaying the Authors table in a DataGridView.

Move to first row Move to next row

Move to previous row Move to last row

(a)

Delete the current row

Add a new row Save changes

(b)

Currently selected row

Page 16: 2006 Pearson Education, Inc. All rights reserved. 1 20 Database, SQL and ADO.NET

16

2006 Pearson Education, Inc. All rights reserved.

Fig. 20.33 | Data binding architecture used to display the Authors table of the Books database in a GUI.

Page 17: 2006 Pearson Education, Inc. All rights reserved. 1 20 Database, SQL and ADO.NET

17

2006 Pearson Education, Inc. All rights reserved.

1 // Fig. 20.34: DisplayTable.cs

2 // Displays data from a database table in a DataGridView.

3 using System;

4 using System.Windows.Forms;

5

6 namespace DisplayTable

7 {

8 public partial class DisplayTableForm : Form

9 {

10 public DisplayTableForm()

11 {

12 InitializeComponent();

13 } // end constructor

14

15 // Click event handler for the Save Button in the

16 // BindingNavigator saves the changes made to the data

17 private void authorsBindingNavigatorSaveItem_Click(

18 object sender, EventArgs e )

19 {

20 this.Validate();

21 this.authorsBindingSource.EndEdit();

22 this.authorsTableAdapter.Update( this.booksDataSet.Authors );

23 } // end method authorsBindingNavigatorSaveItem_Click

Outline

DisplayTable.cs

(1 of 2)

Page 18: 2006 Pearson Education, Inc. All rights reserved. 1 20 Database, SQL and ADO.NET

18

2006 Pearson Education, Inc. All rights reserved.

Outline

DisplayTable.cs

(2 of 2)

24

25 // loads data into the booksDataSet.Authors table,

26 // which is then displayed in the DataGridView

27 private void DisplayTableForm_Load( object sender, EventArgs e )

28 {

29 // TODO: This line of code loads data into the

30 // 'booksDataSet.Authors' table. You can move, or remove it,

31 // as needed.

32 this.authorsTableAdapter.Fill( this.booksDataSet.Authors );

33 } // end method DisplayTableForm_Load

34 } // end class DisplayTableForm

35 } // end namespace DisplayTable

(a) (b)

Page 19: 2006 Pearson Education, Inc. All rights reserved. 1 20 Database, SQL and ADO.NET

19

2006 Pearson Education, Inc. All rights reserved.

Fig. 20.35 | Viewing the BooksDataSet in the Dataset Designer.

Dataset Designer TitlesTableAdapter

Page 20: 2006 Pearson Education, Inc. All rights reserved. 1 20 Database, SQL and ADO.NET

20

2006 Pearson Education, Inc. All rights reserved.

Fig. 20.36 | TableAdapter Query Configuration Wizard to add a query to a TableAdapter.

Page 21: 2006 Pearson Education, Inc. All rights reserved. 1 20 Database, SQL and ADO.NET

21

2006 Pearson Education, Inc. All rights reserved.

Fig. 20.37 | Choosing the type of query to be generated for the TableAdapter.

Page 22: 2006 Pearson Education, Inc. All rights reserved. 1 20 Database, SQL and ADO.NET

22

2006 Pearson Education, Inc. All rights reserved.

Fig. 20.38 | Specifying a SELECT statement for the query.

Page 23: 2006 Pearson Education, Inc. All rights reserved. 1 20 Database, SQL and ADO.NET

23

2006 Pearson Education, Inc. All rights reserved.

Fig. 20.39 | Query Builder after adding a WHERE clause by entering a value in the Filter column. (Part 1 of 2.)

(a)

Page 24: 2006 Pearson Education, Inc. All rights reserved. 1 20 Database, SQL and ADO.NET

24

2006 Pearson Education, Inc. All rights reserved.

Fig. 20.39 | Query Builder after adding a WHERE clause by entering a value in the Filter column. (Part 2 of 2.)

(b)

Page 25: 2006 Pearson Education, Inc. All rights reserved. 1 20 Database, SQL and ADO.NET

25

2006 Pearson Education, Inc. All rights reserved.

Fig. 20.40 | The SELECT statement created by the Query Builder.

Page 26: 2006 Pearson Education, Inc. All rights reserved. 1 20 Database, SQL and ADO.NET

26

2006 Pearson Education, Inc. All rights reserved.

Fig. 20.41 | Specifying names for the methods to be added to the TitlesTableAdapter.

Page 27: 2006 Pearson Education, Inc. All rights reserved. 1 20 Database, SQL and ADO.NET

27

2006 Pearson Education, Inc. All rights reserved.

Fig. 20.42 | Dataset Designer after adding Fill and Get methods to the TitlesTableAdapter.

Page 28: 2006 Pearson Education, Inc. All rights reserved. 1 20 Database, SQL and ADO.NET

28

2006 Pearson Education, Inc. All rights reserved.

1 // Fig. 20.43: DisplayQueryResult.cs

2 // Displays the result of a user-selected query in a DataGridView.

3 using System;

4 using System.Windows.Forms;

5

6 namespace DisplayQueryResult

7 {

8 public partial class DisplayQueryResultForm : Form

9 {

10 public DisplayQueryResultForm()

11 {

12 InitializeComponent();

13 } // end DisplayQueryResultForm constructor

14

15 // Click event handler for the Save Button in the

16 // BindingNavigator saves the changes made to the data

17 private void titlesBindingNavigatorSaveItem_Click(

18 object sender, EventArgs e )

19 {

20 this.Validate();

21 this.titlesBindingSource.EndEdit();

22 this.titlesTableAdapter.Update( this.booksDataSet.Titles );

23 } // end method titlesBindingNavigatorSaveItem_Click

Outline

DisplayQueryResult.cs

(1 of 5)

Page 29: 2006 Pearson Education, Inc. All rights reserved. 1 20 Database, SQL and ADO.NET

29

2006 Pearson Education, Inc. All rights reserved.

24

25 // loads data into the booksDataSet.Titles table,

26 // which is then displayed in the DataGridView

27 private void DisplayQueryResultForm_Load(

28 object sender, EventArgs e )

29 {

30 // TODO: This line of code loads data into the

31 // 'booksDataSet.Titles' table. You can move, or remove it,

32 // as needed.

33 this.titlesTableAdapter.Fill( this.booksDataSet.Titles );

34

35 // set the ComboBox to show the default query that

36 // selects all books from the Titles table

37 queriesComboBox.SelectedIndex = 0;

38 } // end method DisplayQueryResultForm_Load

Outline

DisplayQueryResult.cs

(2 of 5)

Page 30: 2006 Pearson Education, Inc. All rights reserved. 1 20 Database, SQL and ADO.NET

30

2006 Pearson Education, Inc. All rights reserved.

39

40 // loads data into the booksDataSet.Titles table based on

41 // user-selected query

42 private void queriesComboBox_SelectedIndexChanged(

43 object sender, EventArgs e )

44 {

45 // fill the Titles DataTable with

46 // the result of the selected query

47 switch ( queriesComboBox.SelectedIndex )

48 {

49 case 0: // all books

50 titlesTableAdapter.Fill( booksDataSet.Titles );

51 break;

52 case 1: // books with copyright year 2006

53 titlesTableAdapter.FillWithCopyright2006(

54 booksDataSet.Titles );

55 break;

56 case 2: // How to Program books, sorted by Title

57 titlesTableAdapter.FillWithHowToProgramBooks(

58 booksDataSet.Titles );

59 break;

60 } // end switch

61 } // end method queriesComboBox_SelectedIndexChanged

62 } // end class DisplayQueryResultForm

63 } // end namespace DisplayQueryResult

Outline

DisplayQueryResult.cs

(3 of 5)

Page 31: 2006 Pearson Education, Inc. All rights reserved. 1 20 Database, SQL and ADO.NET

31

2006 Pearson Education, Inc. All rights reserved.

Outline

DisplayQueryResult.cs

(4 of 5)

(b)

(a)

Page 32: 2006 Pearson Education, Inc. All rights reserved. 1 20 Database, SQL and ADO.NET

32

2006 Pearson Education, Inc. All rights reserved.

Outline

DisplayQueryResult.cs

(5 of 5)

(c)

Page 33: 2006 Pearson Education, Inc. All rights reserved. 1 20 Database, SQL and ADO.NET

33

2006 Pearson Education, Inc. All rights reserved.

1 // Fig. 20.44: AddressBook.cs

2 // Allows users to manipulate an address book.

3 using System;

4 using System.Windows.Forms;

5

6 namespace AddressBook

7 {

8 public partial class AddressBookForm : Form

9 {

10 public AddressBookForm()

11 {

12 InitializeComponent();

13 } // end AddressBookForm constructor

14

15 // Click event handler for the Save Button in the

16 // BindingNavigator saves the changes made to the data

17 private void addressesBindingNavigatorSaveItem_Click(

18 object sender, EventArgs e )

19 {

20 this.Validate();

21 this.addressesBindingSource.EndEdit();

22 this.addressesTableAdapter.Update(

23 this.addressBookDataSet.Addresses );

24 } // end method bindingNavigatorSaveItem_Click

Outline

AddressBook.cs

(1 of 3)

Page 34: 2006 Pearson Education, Inc. All rights reserved. 1 20 Database, SQL and ADO.NET

34

2006 Pearson Education, Inc. All rights reserved.

25

26 // loads data into the addressBookDataSet.Addresses table

27 private void AddressBookForm_Load( object sender, EventArgs e )

28 {

29 // TODO: This line of code loads data into the

30 // 'addressBookDataSet.Addresses' table. You can move,

31 // or remove it, as needed.

32 this.addressesTableAdapter.Fill(

33 this.addressBookDataSet.Addresses );

34 } // end method AddressBookForm_Load

35

36 // loads data for the rows with the specified last name

37 // into the addressBookDataSet.Addresses table

38 private void findButton_Click( object sender, EventArgs e )

39 {

40 // fill the DataSet's DataTable with only rows

41 // containing the user-specified last name

42 addressesTableAdapter.FillByLastName(

43 addressBookDataSet.Addresses, findTextBox.Text );

44 } // end method findButton_Click

45

46 // reloads addressBookDataSet.Addresses with all rows

47 private void browseAllButton_Click( object sender, EventArgs e )

48 {

49 // fill the DataSet's DataTable with all rows in the database

50 addressesTableAdapter.Fill( addressBookDataSet.Addresses );

Outline

AddressBook.cs

(2 of 3)

Page 35: 2006 Pearson Education, Inc. All rights reserved. 1 20 Database, SQL and ADO.NET

35

2006 Pearson Education, Inc. All rights reserved.

Outline

AddressBook.cs

(3 of 3)

51

52 findTextBox.Text = ""; // clear Find TextBox

53 } // end method browseAllButton_Click

54 } // end class AddressBookForm

55 } // end namespace AddressBook

(b)(a)

(c)

Page 36: 2006 Pearson Education, Inc. All rights reserved. 1 20 Database, SQL and ADO.NET

36

2006 Pearson Education, Inc. All rights reserved.

Fig. 20.45 | Selecting the control(s) to be created when dragging and dropping a data source member onto the Form.

Page 37: 2006 Pearson Education, Inc. All rights reserved. 1 20 Database, SQL and ADO.NET

37

2006 Pearson Education, Inc. All rights reserved.

Fig. 20.46 | Displaying a table on a Form using a series of Labels and TextBoxes.

Page 38: 2006 Pearson Education, Inc. All rights reserved. 1 20 Database, SQL and ADO.NET

38

2006 Pearson Education, Inc. All rights reserved.

Fig. 20.47 | Dataset Designer for the AddressBookDataSet after adding a query to AddressesTableAdapter.

Page 39: 2006 Pearson Education, Inc. All rights reserved. 1 20 Database, SQL and ADO.NET

39

2006 Pearson Education, Inc. All rights reserved.

Fig. 20.48 | Design view after adding controls to locate a last name in the address book.

Page 40: 2006 Pearson Education, Inc. All rights reserved. 1 20 Database, SQL and ADO.NET

40

2006 Pearson Education, Inc. All rights reserved.

Fig. 20.49 | Viewing the DataBindings.Text property of a TextBox in the Properties window.

Page 41: 2006 Pearson Education, Inc. All rights reserved. 1 20 Database, SQL and ADO.NET

41

2006 Pearson Education, Inc. All rights reserved.

1 // Fig. 20.50: XMLWriter.cs

2 // Demonstrates generating XML from an ADO.NET DataSet.

3 using System;

4 using System.Windows.Forms;

5

6 namespace XMLWriter

7 {

8 public partial class XMLWriterForm : Form

9 {

10 public XMLWriterForm()

11 {

12 InitializeComponent();

13 } // end XMLWriterForm constructor

14

15 // Click event handler for the Save Button in the

16 // BindingNavigator saves the changes made to the data

17 private void playersBindingNavigatorSaveItem_Click(

18 object sender, EventArgs e )

19 {

20 this.Validate();

21 this.playersBindingSource.EndEdit();

22 this.playersTableAdapter.Update( this.baseballDataSet.Players );

23 } // end method bindingNavigatorSaveItem_Click

Outline

XMLWriter.cs

(1 of 3)

Page 42: 2006 Pearson Education, Inc. All rights reserved. 1 20 Database, SQL and ADO.NET

42

2006 Pearson Education, Inc. All rights reserved.

24

25 // loads data into the baseballDataSet.Players table

26 private void XMLWriterForm_Load( object sender, EventArgs e )

27 {

28 // TODO: This line of code loads data into the

29 // 'baseballDataSet.Players' table. You can move,

30 // or remove it, as needed.

31 this.playersTableAdapter.Fill( this.baseballDataSet.Players );

32 }

33

34 // write XML representation of DataSet when Button clicked

35 private void writeButton_Click( object sender, EventArgs e )

36 {

37 // set the namespace for this DataSet

38 // and the resulting XML document

39 baseballDataSet.Namespace = "http://www.deitel.com/baseball";

40

41 // write XML representation of DataSet to a file

42 baseballDataSet.WriteXml( "Players.xml" );

Outline

XMLWriter.cs

(2 of 3)

Page 43: 2006 Pearson Education, Inc. All rights reserved. 1 20 Database, SQL and ADO.NET

43

2006 Pearson Education, Inc. All rights reserved.

Outline

XMLWriter.cs

(3 of 3)

43

44 // display XML representation in TextBox

45 outputTextBox.Text += "Writing the following XML:\r\n" +

46 baseballDataSet.GetXml() + "\r\n";

47 } // end method writeButton_Click

48 } // end class XMLWriterForm

49 } // end namespace XMLWriter

Page 44: 2006 Pearson Education, Inc. All rights reserved. 1 20 Database, SQL and ADO.NET

44

2006 Pearson Education, Inc. All rights reserved.

1 <?xml version="1.0" standalone="yes"?>

2 <BaseballDataSet xmlns="http://www.deitel.com/baseball">

3 <Players>

4 <PlayerID>1</PlayerID>

5 <FirstName>John</FirstName>

6 <LastName>Doe</LastName>

7 <BattingAverage>0.375</BattingAverage>

8 </Players>

9 <Players>

10 <PlayerID>2</PlayerID>

11 <FirstName>Jack</FirstName>

12 <LastName>Smith</LastName>

13 <BattingAverage>0.223</BattingAverage>

14 </Players>

15 <Players>

16 <PlayerID>3</PlayerID>

17 <FirstName>George</FirstName>

18 <LastName>O'Malley</LastName>

19 <BattingAverage>0.344</BattingAverage>

20 </Players>

21 </BaseballDataSet>

Outline

Players.xml