Week5 DB

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 45

Software Engineering

Week 5
Software Engineering
Wk Lecture Practical
1 Introduction Canvas, Assessment Understand the case study.
Software lifecycle Write the user stories.
Design the database and the
2 Work as a group! Agile
software
Plan the work on the UI Review of the software requirements and
Set the version control
and the Use Case design
diagram. Review of the OOP concepts.
3 User Stories Git

4 Plan the work on the Graphical User Interface. MVC pattern. Coding
user stories Retrospective
Check if you are on track.
5 Plan the work on the Create and connect the database to the
database application.
From UML to C# code
6 Plan the current task Testing

7-9 Retrospective. Design patterns More coding.


Plan the current task. Accomplish the quality.
Secure coding.
10 Software Validation and Verification Double check.
No new features.
Software Maintenance.
11 Enhancements.
Software Management.
12 Review Presentation
Learning Outcomes

Learn how to create a Database in Visual Studio.


Learn how to manipulate a database in C#.
Creating a DB in Visual Studio

Connecting to and working with the databases involve a


few components that work together.

A binding source is that


component that connects the
user interface controls to a
dataset.
Creating a DB in Visual Studio

Connecting to and working with the databases involve a


few components that work together.

A dataset gets a copy of a table from


the table adapter and keeps it in
memory.
The application works with the
dataset rather than the database
itself. The changes done in the
dataset are sent to the table adapter
to be done in database.
Creating a DB in Visual Studio

Connecting to and working with the databases involve a


few components that work together.

A table adapter connects to a data


source and can retrieve data from a
table in a data source. It can also
update the table in the data source.
Creating a DB in Visual Studio

Connecting to and working with the databases involve a


few components that work together.

A data source is a source of data


(the database) the application works
with.
How to create the Database

Create and add the database to the project.

1.Right-click on the project in SolutionExplorer and select


Add -> New item.
How to create the Database

2. In the Add New Item window:

i. select Data under Visual C# on the left panel

ii. select Service-based Database on the middle


panel

iii. Change the database’s name

iv. Click Add


How to create the Database
In the Add New Item window:
How to create the Database

3. In the Data Source Configuration Wizard window


(Choose the Database Model)

i. Click Cancel

This Wizard should be used to configure the data source.

Currently our database is empty.


Add tables to the Database

1. Go to Solution Explorer where you can see the new


database under your project.

2. Double click on the database.


Add tables to the Database
Server Explorer opens showing the database.
3.Expand the database if it’s not expanded.
4.Right click on Tables and select Add new table.
Add tables to the Database

5. Create the columns. For each column enter the name,


the data type and tick Allow Nulls if a null value can be
accepted.
Add tables to the Database

5. Create the columns.


To set a column as an identity column:
i.click on it
ii.go to Properties window
iii.expand Identity Specification
iv.set (Is Identity) property to true
Add tables to the Database

5. Create the columns.


To set a column as a primary key:
i.Right-click on the column
ii.Select Set Primary Key
Add tables to the Database

6. Save the table.


i. Change the table name in the SQL statement
ii. Click Update
Add tables to the Database

7. Right-click on Tables in Server Explorer and select


Refresh. The new created table is shown.
Add tables to the Database

8. Enter data into the table

i. Right-click on the table you want to enter data into


and select Show Table Data

ii. Enter the data in all the columns apart of the


identity column.
How to create the Database

The database was successfully created and an .mdf file


can be seen in the project folder.
Set the application’s properties

Expand the project in the Solution Explorer.

Double click on the Properties.

A new window with the project’s properties is shown.

Click on the Settings.


Set the application’s properties

Create the connection string that will be used by the


application to connect to the database.
Set the application’s properties

Click onto the Name box and type in a name for


connection string.
Set the application’s properties

Click onto the Type box and select Connection.


Set the application’s properties

Click onto the Scope box and select Application.


Set the application’s properties

Click the button into the Value


box.

The Connection Properties


window is shown.

Change the Data source to


“Microsoft SQL Server
Database”.

Browse and select your MDF


database.
Set the database’s properties

Copies of the database at runtime.


A copy of the database is copied in /bin directory every time
when the application is run. This copy is used by the
application and the changes are done into it.
Set the database’s properties
Set the database’s properties

Copies of the database at runtime.


To manage the copies of the database:
• Go to the Solution Explorer
• Right-click on the .mdf file and select Properties
• Change the value of Copy to Output Directory

More about this:


http://msdn.microsoft.com/en-us/library/vstudio/
ms246989.aspx
Connect the database to the application

The applications interact with the databases via a user


interface (UI) control connected to the data source.

The DataGridView control is such a user interface control


used to display the data from database in a table format.
Connect the database to the application

The back end code is responsible for:

•handling the event generated by the UI,

•connecting to the database,

•send a query to the database

•process the results

•display on the UI the results


Connect the database to the application

The applications should have a class (for instance


DatabaseConnection.cs) that contains all the attributes
and methods that deal with the database.

This class is used by all the other classes that have to


interact with the database.
Connect the database to the application

Let’s consider the DatabaseConnection.cs used for the


application StudentDissertations.
Connect the database to the application

The constructors:
Connect the database to the application

The methods:
Connect the database to the application
The methods:
Connect the database to the application
How to use the class DatabaseConnection.cs
Groups with different levels of access

Students – login

– see marks

– see calendar

Academics – login

– see list of students

– see calendar

There is a difference between what these two groups should


see.
Groups with different levels of access

Are there two different applications, one for students and


one for academics?

How does the application make the difference between a


student and an academic?
Groups with different levels of access

The application should use the same login page.

Based on the credentials entered the application transfers the


user to:

- the student page or academic page

Or

- one page with different contents – one for


students and a different one for academics
Groups with different levels of access

Two achieve this the following steps are required:

• update the database:

- create a table users (we should already have it);

- create a table groups with at least id_group,


group_name;

What is the relationship between these two tables?

- create a join table groups_users to simulate the


many-to-many relationship between users and
groups;
Groups with different levels of access

Example:

groups groups_users

users
Groups with different levels of access

• When the authorisation is given:

- find the user in the users table;

- find the group the user belongs to by searching


the user in the groups_users table;

- load the next page depending on the group


found; this page could have a group specific
menu.
Groups with different levels of access

Example: Check if the user linda is an academic


groups groups_users

users

SELECT gu.user_id
FROM groups_users gu, groups g
WHERE g.name = ‘Academics’
AND g.id = gu.group_id
AND gu.user_id = 1
Questions

You might also like