Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 9 Working with Databases. Copyright © 2011 Pearson Addison-Wesley Introduction In this chapter you will learn: – Basic database concepts – How.

Similar presentations


Presentation on theme: "Chapter 9 Working with Databases. Copyright © 2011 Pearson Addison-Wesley Introduction In this chapter you will learn: – Basic database concepts – How."— Presentation transcript:

1 Chapter 9 Working with Databases

2 Copyright © 2011 Pearson Addison-Wesley Introduction In this chapter you will learn: – Basic database concepts – How to write Visual Basic applications that interact with databases – How to use a DataGridView control and display the data in a database – How to sort and update database data – To create an application that displays database data in list boxes, text boxes, labels, and combo boxes Chapter 10 – Slide 2

3 Section 10.1 DATABASE MANAGEMENT SYSTEMS Visual Basic applications use database management systems to make large amounts of data available to programs.

4 Copyright © 2011 Pearson Addison-Wesley Layered Approach to Using a DBMS Applications that work with a DBMS use a layered approach – VB application is topmost layer – VB sends instructions to next layer, the DBMS – DBMS works directly with data Programmer need not understand the physical structure of the data – Just need to know how to interact with the database Chapter 10 – Slide 4

5 Copyright © 2011 Pearson Addison-Wesley Visual Basic Supports Many DBMS’s Visual Basic can interact with many DBMS’s – Microsoft SQL Server – Oracle – DB2 – MySQL Chapter 10 – Slide 5

6 Section 10.3 DATAGRIDVIEW CONTROL The DataGridView control allows you to display a database table in a grid. The grid can be used at runtime to sort and edit the contents of a table.

7 Copyright © 2011 Pearson Addison-Wesley Connecting to a Database Visual Basic uses a technique called Data binding to link tables to controls on forms We will use these data-related components: – A Data source is usually a database Can include text files, Excel spreadsheets, XML data, and Web services – A Binding source connects data bound controls to a dataset – A Table adapter pulls data from the database and passes it to your program Uses Structured Query Language (SQL) is used to select data, add new rows, delete rows, and modify existing rows – A Dataset is an in-memory copy of data pulled from database tables Chapter 10 – Slide 7

8 Copyright © 2011 Pearson Addison-Wesley Connecting to a Database The flow of data from database to application – Data travels from data source to application – Application can view/change dataset contents – Changes to dataset can be written back to the data source Chapter 10 – Slide 8

9 Section 10.4 DATA-BOUND CONTROLS Some controls can be bound to a dataset. A data-bound control can be used to display and edit the contents of a particular row and column.

10 Copyright © 2011 Pearson Addison-Wesley Advantages of Data-Bound Controls Can bind fields in a data source to controls: – Text boxes – Labels – List boxes Contents of data-bound controls change automatically when moving from row to row Data-bound controls also allow the contents of a database field to be changed Chapter 10 – Slide 10

11 Copyright © 2011 Pearson Addison-Wesley Adding a New Data Source Open the Data Sources window and click the Add New Data Source link Follow the steps in the Data Source Configuration Wizard to create a connection to the database Chapter 10 – Slide 11

12 Copyright © 2011 Pearson Addison-Wesley Deleting a Data Source A data source named Employees for example would be defined by a file named Employees.xsd To delete this data source: – Select Employees.xsd file in Solution Explorer – Press Delete on the keyboard Chapter 10 – Slide 12

13 Copyright © 2011 Pearson Addison-Wesley Binding the Data Source to a DataGridView Control Drag and drop an existing dataset from the Data Sources window to an open area on the form – For example: Chapter 10 – Slide 13

14 Copyright © 2011 Pearson Addison-Wesley Binding the Data Source to a DataGridView Control At the same time Visual Studio builds a DataGridView on the form, it adds a number of important objects to the form’s component tray: – The BindingNavigator creates a ToolStrip at the top of the form – The DataSet is an in-memory copy of the table – The BindingSource connects the DataGridView to the DataSet – The TableAdapter pulls data from the database into the DataSet – The AdapterManager is a tool for saving data in related tables Chapter 10 – Slide 14

15 Copyright © 2011 Pearson Addison-Wesley Binding Individual Fields to Controls Use the dataset in the Data Sources window – Select Details from the table drop-down list – Drag table to an open area of a form – Creates a separate control for each field – Can also drag columns individually Text and numeric fields added as text boxes Yes/No fields added as checkboxes DateTime fields use DateTimePicker controls May wish to change some control properties Chapter 10 – Slide 15

16 Copyright © 2011 Pearson Addison-Wesley Adding Rows to a Database Table A TableAdapter provides an easy way to add a row to a database table To find the TableAdapter you must open a data set’s Schema Definition A schema definition file (.xsd) was automatically created in Tutorial 10-5 for the Members table Dataset – Displays the names and data types of fields in the table To edit the schema definition file: – Double-click its name in the Solution Explorer window – An editor window will open A TableAdapter object was automatically created for the Members DataTable Each DataTable has a TableAdapter associated with it Chapter 10 – Slide 16

17 Copyright © 2011 Pearson Addison-Wesley Adding Rows to a Database Table A TableAdapter object has an Insert method – Used to add a new row to the database table – Each column is an argument of the method – Just provide the values for each argument – For example: Chapter 10 – Slide 17 MembersTableAdapter.Insert(10, "Hasegawa", "Adrian", "305-999-8888",#5/15/2010#)

18 Copyright © 2011 Pearson Addison-Wesley Identity Columns Some database tables have an identity column – Assigned a unique number by the database – Occurs automatically for identity columns – No need to manually supply a value for this column Payments table uses an identity column – Omit ID column value – Only supply Member_Id, Payment_Date, and Amount Chapter 10 – Slide 18 PaymentsTableAdapter.Insert(5, #5/15/2010#, 50D)


Download ppt "Chapter 9 Working with Databases. Copyright © 2011 Pearson Addison-Wesley Introduction In this chapter you will learn: – Basic database concepts – How."

Similar presentations


Ads by Google