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

Post on 02-Jan-2016

217 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

1

2006 Pearson Education, Inc. All rights reserved.

2020Database, 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

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.

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

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

6

2006 Pearson Education, Inc. All rights reserved.

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

Data Sources windowData menu

7

2006 Pearson Education, Inc. All rights reserved.

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

8

2006 Pearson Education, Inc. All rights reserved.

Fig. 20.25 | Adding a new data connection.

9

2006 Pearson Education, Inc. All rights reserved.

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

10

2006 Pearson Education, Inc. All rights reserved.

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

11

2006 Pearson Education, Inc. All rights reserved.

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

12

2006 Pearson Education, Inc. All rights reserved.

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

Data Sources window

13

2006 Pearson Education, Inc. All rights reserved.

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

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

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

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.

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)

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)

19

2006 Pearson Education, Inc. All rights reserved.

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

Dataset Designer TitlesTableAdapter

20

2006 Pearson Education, Inc. All rights reserved.

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

21

2006 Pearson Education, Inc. All rights reserved.

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

22

2006 Pearson Education, Inc. All rights reserved.

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

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)

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)

25

2006 Pearson Education, Inc. All rights reserved.

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

26

2006 Pearson Education, Inc. All rights reserved.

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

27

2006 Pearson Education, Inc. All rights reserved.

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

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)

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)

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)

31

2006 Pearson Education, Inc. All rights reserved.

Outline

DisplayQueryResult.cs

(4 of 5)

(b)

(a)

32

2006 Pearson Education, Inc. All rights reserved.

Outline

DisplayQueryResult.cs

(5 of 5)

(c)

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)

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)

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)

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.

37

2006 Pearson Education, Inc. All rights reserved.

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

38

2006 Pearson Education, Inc. All rights reserved.

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

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.

40

2006 Pearson Education, Inc. All rights reserved.

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

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)

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)

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

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

top related