Upload
tyrone-morrison
View
216
Download
0
Embed Size (px)
Citation preview
11/8/05 CS360 Windows Programming 1
Databases and Data Representation
11/8/05 CS360 Windows Programming 2
Relationships
Relation: one-to-many
1 Shereen CS360-01 12
2 Doug CS300-01 11
3 Chris CS445-01 15
4 Josh CS120-01 23
5 Mike CS120-02 19
Name Course StudentsLectID
1 1 Strain 203C
2 4 Marsh 324
3 4 Strain 202
4 2 Strain 201
Building RoomLectID
Foreign key
Primary keyOfficeID
11/8/05 CS360 Windows Programming 3
Database Views
CREATE VIEW StrainOffices AS SELECT OfficeID, LectID, Room FROM Offices WHERE Building = ‘Strain’
1 1 Strain 203C
2 4 Marsh 324
3 4 Strain 202
4 2 Strain 201
Building RoomLectIDOfficeID
1 1 203C
3 4 202
4 2 201
RoomLectIDOfficeID
11/8/05 CS360 Windows Programming 4
Database Views
Views do not store data – they are “virtual” tables
If we query a view, tuples are obtained from the base table so that the query can be answered
SELECT OfficeID, RoomFROM StrainOfficesWHERE LectID = 1
1 1 203C
3 4 202
4 2 201
RoomLectIDOfficeID
1 203C
RoomOfficeID
11/8/05 CS360 Windows Programming 5
Database Views
We can rename the columns in the view if we want
CREATE VIEW StrainOffices(OId, Lid, RoomNum) AS SELECT OfficeID, LectID, Room FROM Offices WHERE Building = ‘Strain’
1 1 203C
3 4 202
4 2 201
RoomNumLIdOId
11/8/05 CS360 Windows Programming 6
Database Joins
1 Shereen CS360-01 12
2 Doug CS300-01 11
3 Chris CS445-01 15
4 Josh CS120-01 23
5 Mike CS120-02 19
Name Course StudentsLectID
1 1 Strain 203C
2 4 Marsh 324
3 4 Strain 202
4 2 Strain 201
Building RoomLectID
Foreign key
Primary keyOfficeID
11/8/05 CS360 Windows Programming 7
Joins
SELECT * FROM Lectures INNER JOIN Offices ON Lecturers.LectID = Offices.LectID ORDER BY Offices.LectID
LId Name Course Students OId LId Bld Room
1 Shereen CS360-01 12 1 1 Strain 203C
2 Doug CS300-01 11 4 2 Strain 201
3 Chris CS445-01 15 3
4 Josh CS120-01 23 2 4 Marsh 324
4 Josh CS120-01 23 3 4 Strain 202
5 Mike CS120-02 19 5
11/8/05 CS360 Windows Programming 8
Your Turn
SELECT Name FROM Lecturers INNER JOIN Offices ON Lecturers.LectID = Office.LectIDINNER JOIN Advisees ON Lecturers.LectID = Advisees.AdvIDWHERE Building = ‘Strain’ AND Name = ‘Harry’
1 Shereen CS360-01 12
2 Doug CS300-01 11
3 Chris CS445-01 15
4 Josh CS120-01 23
5 Mike CS120-02 19
Name Course StudentsLectID
1 1 Strain 203C
2 4 Marsh 324
3 4 Strain 202
4 2 Strain 201
Building RoomLectIDOfficeID
1 1 John
2 1 Mike
3 1 Harry
4 2 Holly
5 2 Ron
NameLectIDAdvID
11/8/05 CS360 Windows Programming 9
Connecting to MySQL
C# can connect to MySQL
Need to download a .NET connector
http://dev.mysql.com/downloads/connector/net/1.0.html
Need the MySql.Data.dllo I’ve placed it in CS360 Pub under MySQL
Connector\bin\.NET 1.1
11/8/05 CS360 Windows Programming 10
Connecting to MySQL
11/8/05 CS360 Windows Programming 11
Connecting to MySQL
using MySql.Data.MySqlClient;
string connStr = "server=cs445.cs.pacificu.edu; user id=shereen; password=abc123; database=shereen;";
MySqlConnection conn = null;
MySqlDataAdapter da = null;
MySqlDataReader reader = null;
MySqlCommand cmd = null;
11/8/05 CS360 Windows Programming 12
Connecting to MySQL
try
{
conn = new MySqlConnection(connStr);
conn.Open();
cmd = new MySqlCommand("SELECT * FROM pet", conn);
reader = cmd.ExecuteReader();
lb2.Text = "";
while (reader.Read())
{
lb2.Text = lb2.Text + reader.GetString(0) + "\n";
}
}
11/8/05 CS360 Windows Programming 13
Connecting to MySQL
catch (MySqlException ex)
{
lb2.Text = "Exception accessing MySQL server: " + ex.Message;
}
catch (Exception ex)
{
lb2.Text = "Exception accessing database list: " + ex.Message;
}
finally
{
if (reader != null) reader.Close();
if (conn != null) conn.Close();
}
11/8/05 CS360 Windows Programming 14