37
Data Base

Data Base. Objective Become familiar with database terminology. Create a project to display data for a single database table. Use a DataGrid control

Embed Size (px)

Citation preview

Page 1: Data Base. Objective Become familiar with database terminology. Create a project to display data for a single database table. Use a DataGrid control

Data Base

Page 2: Data Base. Objective Become familiar with database terminology. Create a project to display data for a single database table. Use a DataGrid control

Objective

Become familiar with database terminology. Create a project to display data for a single database table. Use a DataGrid control. Bind and display data from databases to textbox and label

controls. Write procedures to navigate table rows in a dataset. Display a row number on a form. Create a parameterized query. Write procedures to perform data maintenance activities

including Add, Edit, and Delete rows. Use data validation routines in a data maintenance program.

Page 3: Data Base. Objective Become familiar with database terminology. Create a project to display data for a single database table. Use a DataGrid control

Database Files

What is a data base? Use the internet to give me an answer

Page 4: Data Base. Objective Become familiar with database terminology. Create a project to display data for a single database table. Use a DataGrid control

Answer:

database – a special repository—sometimes a single file, sometimes multiple files—used to store and to retrieve information. 

Page 5: Data Base. Objective Become familiar with database terminology. Create a project to display data for a single database table. Use a DataGrid control

Relational database

–What is a relational database? You find the answer for me:

Page 6: Data Base. Objective Become familiar with database terminology. Create a project to display data for a single database table. Use a DataGrid control

Answer

a specific type of database where data rows are stored in separate tables, and the tables are related to each other by key values

Page 7: Data Base. Objective Become familiar with database terminology. Create a project to display data for a single database table. Use a DataGrid control

What we will use

We will use Microsoft Access Perfect for small-sized, individual user or

small group systems.

We will look at how vb.net and access can work together.

Page 8: Data Base. Objective Become familiar with database terminology. Create a project to display data for a single database table. Use a DataGrid control

How do we connect the two?

VB.NET uses ADO.NET – a database technology that supports connection to different database products. 

ADO.NET stores and transfers data using the Extensible Markup Language (XML). 

ADO.NET uses two basic types of connections to databases across networks: SQLClient – used to connect to Microsoft's SQL

Server DBMS. We won’t use this one OLEDB – used to connect to all other database

formats. We will use this one.

Page 9: Data Base. Objective Become familiar with database terminology. Create a project to display data for a single database table. Use a DataGrid control

Database Concepts and Terminology A database stores data about individual

entities in separate tables.  Examples of entities in a school setting

include Students, Courses, and Enrollment in courses. 

Page 10: Data Base. Objective Become familiar with database terminology. Create a project to display data for a single database table. Use a DataGrid control

DataBase Example

Page 11: Data Base. Objective Become familiar with database terminology. Create a project to display data for a single database table. Use a DataGrid control

Sample data from a database

Page 12: Data Base. Objective Become familiar with database terminology. Create a project to display data for a single database table. Use a DataGrid control

Table Explained

Each table consists of rows and columns. 

Row = row for an individual student; also referred to as a record.

Column = field of data stored for each student such as the SSN, Last Name, First Name, etc.

Page 13: Data Base. Objective Become familiar with database terminology. Create a project to display data for a single database table. Use a DataGrid control

ADO.net

provides controls that you add to a form that are used to connect to and manage data in a database table. 

Columns from a database table are bound to database controls.

Some of the controls you've already used such as labels, textboxes, and combo boxes can be found to database columns. 

Other special data controls such as the DataGrid and DataList controls need to be looked at. 

Page 14: Data Base. Objective Become familiar with database terminology. Create a project to display data for a single database table. Use a DataGrid control

Connecting VB and a database Configure a connection. 

The connection links to a data source – a data source is either a specific database file and/or a server computer.

Configure a data adapter.  A data adapter handles data retrieval and updating.  The

data adapter creates a dataset.  A dataset stores rows that have been retrieved.

Add controls to the form and set properties of the controls to bind the

controls to the columns of a specific table in the dataset. Write VB code

to fill the dataset.

Page 15: Data Base. Objective Become familiar with database terminology. Create a project to display data for a single database table. Use a DataGrid control

Storing your database

You should always store your database file on drive and folder:  C:\Temp – this needs to be setup on your computer.

Why… because of network security issues you will need to copy the database each day to the temp folder.

Page 16: Data Base. Objective Become familiar with database terminology. Create a project to display data for a single database table. Use a DataGrid control

Creating a Connetion

We will use OleDbConnection

Page 17: Data Base. Objective Become familiar with database terminology. Create a project to display data for a single database table. Use a DataGrid control

Class Exercise

Copy the VBUniv.Mdb Microsoft Access database from the class server to your client computer to drive and folder: C:\Temp

Page 18: Data Base. Objective Become familiar with database terminology. Create a project to display data for a single database table. Use a DataGrid control

Starting the project

Download the VBUniv.Mdb Microsoft Access database to the c:\temp

Begin a new project – name it Database Name the form frmStudent. Copy the VBUniv.MDB database file from

C:\Temp to your project folder. This will be a copy for use in the event that the file on C:\Temp becomes corrupted.

Page 19: Data Base. Objective Become familiar with database terminology. Create a project to display data for a single database table. Use a DataGrid control

Configuring a Data Adapter and Connection Add a data adapter by using the Data

Adapter Configuration Wizard – add the OleDbDataAdapter control from the toolbox Data tab by double-clicking

Page 20: Data Base. Objective Become familiar with database terminology. Create a project to display data for a single database table. Use a DataGrid control
Page 21: Data Base. Objective Become familiar with database terminology. Create a project to display data for a single database table. Use a DataGrid control

New Connection….

Page 22: Data Base. Objective Become familiar with database terminology. Create a project to display data for a single database table. Use a DataGrid control

Data Link Properties

Page 23: Data Base. Objective Become familiar with database terminology. Create a project to display data for a single database table. Use a DataGrid control

The Connection tab

Locate the VBUniv.MDB database file at C:\Temp.

Leave the User name as Admin with no password—a password is not needed for Microsoft Access as it is basically a single-user database management system.

Click the Test Connection button. The connection should succeed if it doesn't, use the Browse button again to ensure you have the correct database or use the Provider tab to ensure you have the correct OLE DB Provider.

Page 24: Data Base. Objective Become familiar with database terminology. Create a project to display data for a single database table. Use a DataGrid control
Page 25: Data Base. Objective Become familiar with database terminology. Create a project to display data for a single database table. Use a DataGrid control

Choose a Query Type

For Microsoft Access, you have a single choice – Use SQL statements

A query is a SQL command that retrieves the desired data rows and columns from a database table.

Page 26: Data Base. Objective Become familiar with database terminology. Create a project to display data for a single database table. Use a DataGrid control
Page 27: Data Base. Objective Become familiar with database terminology. Create a project to display data for a single database table. Use a DataGrid control

Query Builder

Page 28: Data Base. Objective Become familiar with database terminology. Create a project to display data for a single database table. Use a DataGrid control
Page 29: Data Base. Objective Become familiar with database terminology. Create a project to display data for a single database table. Use a DataGrid control

Click the Query Builder button. All of the tables in the VBUniversity database (States, Course, Enrollment, and Student) are listed.

If the Add Table window is not shown, right-click on the Query Builder window to open the Add Table window.

Select the Student table and close the Add Table window.

Page 30: Data Base. Objective Become familiar with database terminology. Create a project to display data for a single database table. Use a DataGrid control

Why use the Query Builder

The Query Builder window interface is used to select the columns from the STUDENT table to include in the SQL Select statement. The columns (fields) are selected in the order in which they are checked. Notice that the output will be sorted by student's LastName column.

Select ONLY the StudentSSN, LastName, FirstName, and MiddleInitial columns.

You can specify the data rows to be sorted by last name.

Page 31: Data Base. Objective Become familiar with database terminology. Create a project to display data for a single database table. Use a DataGrid control
Page 32: Data Base. Objective Become familiar with database terminology. Create a project to display data for a single database table. Use a DataGrid control

Finishing up

Examine your VB project form. You will discover the addition of two controls in the component tray named OleDbDataAdapter1 and OleDbConnection1.

Rename the OleDbConnection1 control conVBUniversity.

Page 33: Data Base. Objective Become familiar with database terminology. Create a project to display data for a single database table. Use a DataGrid control

Defining a Dataset

Select the OleDbAdapter named daStudent,

Select Generate Dataset from the Data menu, or Right-click and select from the shortcut menu.

Name a dataset with the prefix "ds"—here we will name the dataset dsStudent.

Page 34: Data Base. Objective Become familiar with database terminology. Create a project to display data for a single database table. Use a DataGrid control
Page 35: Data Base. Objective Become familiar with database terminology. Create a project to display data for a single database table. Use a DataGrid control

After you click OK, you'll see the dataset listed in the system tray with the name DsStudent1 – Visual Basic automatically adds the digit 1 to the first dataset, 2 to the second dataset, and so forth.

Page 36: Data Base. Objective Become familiar with database terminology. Create a project to display data for a single database table. Use a DataGrid control

The Fill Method

At runtime, the data adapter's Fill method is executed to fill the dataset.

This is usually coded in the Form_Load event for Windows applications. Examples: ‘DataAdapterName.Fill(DataSetName) daStudent.Fill(DaStudent1)

When there is more than one table in a dataset, you can specify the dataset name and table name. daStudent.Fill(DsStudent1, "Student")

Page 37: Data Base. Objective Become familiar with database terminology. Create a project to display data for a single database table. Use a DataGrid control

Binding Data to Controls

A dataset is bound to controls on a form by setting control properties.

When binding to a DataGrid, these properties include the: DataSource property – set it to the name of

the dataset. DataMember property – set it to the name

of the table.