vs2010 c# programming - db intro 1 topics - mantech1.pdf · 2 from vs c# 2010 programming, john...

21
From VS C# 2010 Programming, John Allwork 1 VS2010 C# Programming - DB intro 1 Topics – Database Relational - linked tables SQL ADO.NET objects Referencing Data Using the Wizard Displaying data

Upload: hoangbao

Post on 21-Dec-2018

219 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: VS2010 C# Programming - DB intro 1 Topics - Mantech1.pdf · 2 From VS C# 2010 Programming, John Allwork VS2010 C# Programming - DB intro 2 Database – A collection of data. Searchable

From VS C# 2010 Programming, John Allwork1

VS2010 C# Programming - DB intro 1

Topics –• Database• Relational - linked tables• SQL• ADO.NET objects• Referencing Data• Using the Wizard• Displaying data

Page 2: VS2010 C# Programming - DB intro 1 Topics - Mantech1.pdf · 2 From VS C# 2010 Programming, John Allwork VS2010 C# Programming - DB intro 2 Database – A collection of data. Searchable

From VS C# 2010 Programming, John Allwork2

VS2010 C# Programming - DB intro 2

Database –A collection of data.Searchable – user extracts detailed information

Relational database – queried and data extracted using SQL languageRelation – individual tables linked together.Data held in one place onlye.g, employees, customers, orders, suppliers

Page 3: VS2010 C# Programming - DB intro 1 Topics - Mantech1.pdf · 2 From VS C# 2010 Programming, John Allwork VS2010 C# Programming - DB intro 2 Database – A collection of data. Searchable

From VS C# 2010 Programming, John Allwork3

VS2010 C# Programming - DB intro 3

Microsoft example: Northwind databaseEmployees table

Northwind database provided with AccessDownload from Microsoft

Page 4: VS2010 C# Programming - DB intro 1 Topics - Mantech1.pdf · 2 From VS C# 2010 Programming, John Allwork VS2010 C# Programming - DB intro 2 Database – A collection of data. Searchable

From VS C# 2010 Programming, John Allwork4

VS2010 C# Programming - DB intro 4

Relational database is composed of linked tables.

Table made from records.A record (or row) consists of fields (or columns) of data.Usually one unique record – ID

Search using Structured Query Language (SQL)Search – all employees aged about 21, over a certain wage, or called John

Page 5: VS2010 C# Programming - DB intro 1 Topics - Mantech1.pdf · 2 From VS C# 2010 Programming, John Allwork VS2010 C# Programming - DB intro 2 Database – A collection of data. Searchable

From VS C# 2010 Programming, John Allwork5

VS2010 C# Programming - DB intro 5

Structured Query Language (SQL)SQL search commands:SELECT (field) FROM (table) WHERE (criteria) GROUP BY (criteria)ORDER BY (age)

SQL edit commands:INSERT, UPDATE, DELETE

Page 6: VS2010 C# Programming - DB intro 1 Topics - Mantech1.pdf · 2 From VS C# 2010 Programming, John Allwork VS2010 C# Programming - DB intro 2 Database – A collection of data. Searchable

From VS C# 2010 Programming, John Allwork6

VS2010 C# Programming - DB intro 6

Example SQL commands:SELECT firstName, lastName FROM Employees

SELECT firstName, lastName FROM Employees WHERE firstName LIKE ‘J*’

DELETE FROM Employees WHERE firstName = ‘John’ AND lastName = ‘ALLWORK’

Page 7: VS2010 C# Programming - DB intro 1 Topics - Mantech1.pdf · 2 From VS C# 2010 Programming, John Allwork VS2010 C# Programming - DB intro 2 Database – A collection of data. Searchable

From VS C# 2010 Programming, John Allwork7

VS2010 C# Programming - DB intro 7

ADO.NET – Active database objectsCollection of objects to interface to databases

Establish a connection between program and databaseData held in memory – a DataSet- a collection of DataTable objects

Populated using a TableAdapter / ManagerDisplayed using ‘Data bound’ UI controlsAutomatically update as user scrolls

Page 8: VS2010 C# Programming - DB intro 1 Topics - Mantech1.pdf · 2 From VS C# 2010 Programming, John Allwork VS2010 C# Programming - DB intro 2 Database – A collection of data. Searchable

From VS C# 2010 Programming, John Allwork8

VS2010 C# Programming - DB intro 8

Connection:

Page 9: VS2010 C# Programming - DB intro 1 Topics - Mantech1.pdf · 2 From VS C# 2010 Programming, John Allwork VS2010 C# Programming - DB intro 2 Database – A collection of data. Searchable

From VS C# 2010 Programming, John Allwork9

VS2010 C# Programming - DB intro 9

Database wizard- builds commands for you

• Declare connection• Specify database type and location• Open connection• Create and fill the Dataset• - memory resident copy of the database• Access data in DataTable• – one table of the Dataset

Page 10: VS2010 C# Programming - DB intro 1 Topics - Mantech1.pdf · 2 From VS C# 2010 Programming, John Allwork VS2010 C# Programming - DB intro 2 Database – A collection of data. Searchable

From VS C# 2010 Programming, John Allwork10

VS2010 C# Programming - DB intro 10

Main database tasks:1. View a database2. Create our own database with linked tables3. Display database using controls

(grid view, details view and navigator)4. Accessing and displaying data from code5. Adding data to database with code

1 now, 2-5 in next lectures

Page 11: VS2010 C# Programming - DB intro 1 Topics - Mantech1.pdf · 2 From VS C# 2010 Programming, John Allwork VS2010 C# Programming - DB intro 2 Database – A collection of data. Searchable

From VS C# 2010 Programming, John Allwork11

VS2010 C# Programming - DB intro 11Viewing a databaseNew Project > View Server/Database ExplorerAdd link to existing database (e.g. Northwind)Right-click Data Connections. Add Connection:

Page 12: VS2010 C# Programming - DB intro 1 Topics - Mantech1.pdf · 2 From VS C# 2010 Programming, John Allwork VS2010 C# Programming - DB intro 2 Database – A collection of data. Searchable

From VS C# 2010 Programming, John Allwork12

VS2010 C# Programming - DB intro 12

Browse for database

Test connection

Page 13: VS2010 C# Programming - DB intro 1 Topics - Mantech1.pdf · 2 From VS C# 2010 Programming, John Allwork VS2010 C# Programming - DB intro 2 Database – A collection of data. Searchable

From VS C# 2010 Programming, John Allwork13

VS2010 C# Programming - DB intro 13

Database added to Solution/Database explorer:

Page 14: VS2010 C# Programming - DB intro 1 Topics - Mantech1.pdf · 2 From VS C# 2010 Programming, John Allwork VS2010 C# Programming - DB intro 2 Database – A collection of data. Searchable

From VS C# 2010 Programming, John Allwork14

VS2010 C# Programming - DB intro 14

View table data:Display tables on the form.Right-click table:

Page 15: VS2010 C# Programming - DB intro 1 Topics - Mantech1.pdf · 2 From VS C# 2010 Programming, John Allwork VS2010 C# Programming - DB intro 2 Database – A collection of data. Searchable

From VS C# 2010 Programming, John Allwork15

VS2010 C# Programming - DB intro 15

Employees table:(same as before)

Page 16: VS2010 C# Programming - DB intro 1 Topics - Mantech1.pdf · 2 From VS C# 2010 Programming, John Allwork VS2010 C# Programming - DB intro 2 Database – A collection of data. Searchable

From VS C# 2010 Programming, John Allwork16

VS2010 C# Programming - DB intro 16Display from C# program – use wizard:Data > Add New Data Source:Choose database objects

Add tables to your database

Page 17: VS2010 C# Programming - DB intro 1 Topics - Mantech1.pdf · 2 From VS C# 2010 Programming, John Allwork VS2010 C# Programming - DB intro 2 Database – A collection of data. Searchable

From VS C# 2010 Programming, John Allwork17

VS2010 C# Programming - DB intro 17

Use DataGridView control to display tableClick table and drag icon to form

Page 18: VS2010 C# Programming - DB intro 1 Topics - Mantech1.pdf · 2 From VS C# 2010 Programming, John Allwork VS2010 C# Programming - DB intro 2 Database – A collection of data. Searchable

From VS C# 2010 Programming, John Allwork18

VS2010 C# Programming - DB intro 18

Table added to form:

DataSet, BindingSource, TableAdaptor/Manager controls also added

Page 19: VS2010 C# Programming - DB intro 1 Topics - Mantech1.pdf · 2 From VS C# 2010 Programming, John Allwork VS2010 C# Programming - DB intro 2 Database – A collection of data. Searchable

From VS C# 2010 Programming, John Allwork19

VS2010 C# Programming - DB intro 19

GridView display

Run program to display

Page 20: VS2010 C# Programming - DB intro 1 Topics - Mantech1.pdf · 2 From VS C# 2010 Programming, John Allwork VS2010 C# Programming - DB intro 2 Database – A collection of data. Searchable

From VS C# 2010 Programming, John Allwork20

VS2010 C# Programming - DB intro 20

DataGridView tasks:Enable adding, editing, deleting by userClick the arrow (top r.h. corner) to display tasks

Page 21: VS2010 C# Programming - DB intro 1 Topics - Mantech1.pdf · 2 From VS C# 2010 Programming, John Allwork VS2010 C# Programming - DB intro 2 Database – A collection of data. Searchable

From VS C# 2010 Programming, John Allwork21

VS2010 C# Programming - DB intro 21

Summary –• Database - A searchable collection of data.• Relational - individual tables linked together• Data held in one place only• Data extracted using SQL language• Wizard builds commands• Easy to view an existing database