Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 11/8/05CS360 Windows Programming Databases and Data Representation.

Similar presentations


Presentation on theme: "1 11/8/05CS360 Windows Programming Databases and Data Representation."— Presentation transcript:

1 1 11/8/05CS360 Windows Programming Databases and Data Representation

2 2 11/8/05CS360 Windows Programming Relationships  Relation: one-to-many 1ShereenCS360-0112 2DougCS300-0111 3ChrisCS445-0115 4JoshCS120-0123 5MikeCS120-0219 NameCourseStudentsLectID 11Strain203C 24Marsh324 34Strain202 42Strain201 Building Room LectID Foreign key Primary key OfficeID

3 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 34202 42201 Room LectID OfficeID

4 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 = 1 11203C 34202 42201 Room LectID OfficeID 1203C Room OfficeID

5 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 34202 42201 RoomNum LId OId

6 6 11/8/05CS360 Windows Programming Database Joins 1ShereenCS360-0112 2DougCS300-0111 3ChrisCS445-0115 4JoshCS120-0123 5MikeCS120-0219 NameCourseStudentsLectID 11Strain203C 24Marsh324 34Strain202 42Strain201 Building Room LectID Foreign key Primary key OfficeID

7 7 11/8/05CS360 Windows Programming Joins  SELECT * FROM Lectures INNER JOIN Offices ON Lecturers.LectID = Offices.LectID ORDER BY Offices.LectID LIdNameCourseStudentsOIdLIdBldRoom 1ShereenCS360-011211Strain203C 2DougCS300-011142Strain201 3ChrisCS445-01153 4JoshCS120-012324Marsh324 4JoshCS120-012334Strain202 5MikeCS120-02195

8 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’ 1ShereenCS360-0112 2DougCS300-0111 3ChrisCS445-0115 4JoshCS120-0123 5MikeCS120-0219 NameCourseStudentsLectID 11Strain203C 24Marsh324 34Strain202 42Strain201 BuildingRoomLectIDOfficeID 11John 21Mike 31Harry 42Holly 52Ron NameLectIDAdvID

9 9 11/8/05CS360 Windows Programming Connecting to MySQL  C# can connect to MySQL  Need to download a.NET connector  http://dev.mysql.com/downloads/connector/n et/1.0.html http://dev.mysql.com/downloads/connector/n 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 10 11/8/05CS360 Windows Programming Connecting to MySQL

11 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 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 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 14 11/8/05CS360 Windows Programming


Download ppt "1 11/8/05CS360 Windows Programming Databases and Data Representation."

Similar presentations


Ads by Google