1 11/8/05CS360 Windows Programming Databases and Data Representation
2 11/8/05CS360 Windows Programming Relationships Relation: one-to-many 1ShereenCS DougCS ChrisCS JoshCS MikeCS NameCourseStudentsLectID 11Strain203C 24Marsh324 34Strain202 42Strain201 Building Room LectID Foreign key Primary key OfficeID
3 11/8/05CS360 Windows Programming Database Views CREATE VIEW StrainOffices AS SELECT OfficeID, LectID, Room FROM Offices WHERE Building = ‘Strain’ 11Strain203C 24Marsh324 34Strain202 42Strain201 Building Room LectID OfficeID 11203C Room LectID OfficeID
4 11/8/05CS360 Windows Programming 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, Room FROM StrainOffices WHERE LectID = C Room LectID OfficeID 1203C Room OfficeID
5 11/8/05CS360 Windows Programming 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’ 11203C RoomNum LId OId
6 11/8/05CS360 Windows Programming Database Joins 1ShereenCS DougCS ChrisCS JoshCS MikeCS NameCourseStudentsLectID 11Strain203C 24Marsh324 34Strain202 42Strain201 Building Room LectID Foreign key Primary key OfficeID
7 11/8/05CS360 Windows Programming Joins SELECT * FROM Lectures INNER JOIN Offices ON Lecturers.LectID = Offices.LectID ORDER BY Offices.LectID LIdNameCourseStudentsOIdLIdBldRoom 1ShereenCS Strain203C 2DougCS Strain201 3ChrisCS JoshCS Marsh324 4JoshCS Strain202 5MikeCS
8 11/8/05CS360 Windows Programming Your Turn SELECT Name FROM Lecturers INNER JOIN Offices ON Lecturers.LectID = Office.LectID INNER JOIN Advisees ON Lecturers.LectID = Advisees.AdvID WHERE Building = ‘Strain’ AND Name = ‘Harry’ 1ShereenCS DougCS ChrisCS JoshCS MikeCS NameCourseStudentsLectID 11Strain203C 24Marsh324 34Strain202 42Strain201 BuildingRoomLectIDOfficeID 11John 21Mike 31Harry 42Holly 52Ron NameLectIDAdvID
9 11/8/05CS360 Windows Programming Connecting to MySQL C# can connect to MySQL Need to download a.NET connector et/1.0.html et/1.0.html Need the MySql.Data.dll o I’ve placed it in CS360 Pub under MySQL Connector\bin\.NET 1.1
10 11/8/05CS360 Windows Programming Connecting to MySQL
11 11/8/05CS360 Windows Programming 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;
12 11/8/05CS360 Windows Programming 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"; }
13 11/8/05CS360 Windows Programming 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(); }
14 11/8/05CS360 Windows Programming