Presentation is loading. Please wait.

Presentation is loading. Please wait.

Intro to C#.net and EF Ilan Shimshoni. The Three Faces of ADO.NET The connected layer – Directly connecting to the DB The disconnected layer – Using datasets.

Similar presentations


Presentation on theme: "Intro to C#.net and EF Ilan Shimshoni. The Three Faces of ADO.NET The connected layer – Directly connecting to the DB The disconnected layer – Using datasets."— Presentation transcript:

1 Intro to C#.net and EF Ilan Shimshoni

2 The Three Faces of ADO.NET The connected layer – Directly connecting to the DB The disconnected layer – Using datasets Connection through the Entity Framework (EF) – Automatically defining the relation between the DB and the C# classes and working with LINQ.

3 The connected layer

4 .Net Platform Data Provider Database Client Assembly Connection Object DataReader Object DataAdaptor Object Transaction Parameter Collection Select Command Update Command Delete Command Insert Command

5 Insert Method public void InsertAuto(NewCar car) { // Format and execute SQL statement. string sql = string.Format("Insert Into Inventory" + "(CarID, Make, Color, PetName) Values" + "('{0}', '{1}', '{2}', '{3}')", car.CarID, car.Make, car.Color, car.PetName); // Execute using our connection. using (SqlCommand cmd = new SqlCommand(sql, this.sqlCn)) { cmd.ExecuteNonQuery(); }

6 Select Method public List GetAllInventoryAsList() { // This will hold the records. List inv = new List (); // Prep command object. string sql = "Select * From Inventory"; using (SqlCommand cmd = new SqlCommand(sql, this.sqlCn)) { SqlDataReader dr = cmd.ExecuteReader(); while (dr.Read()) { inv.Add(new NewCar { CarID = (int)dr["CarID"], Color = (string)dr["Color"], Make = (string)dr["Make"], PetName = (string)dr["PetName"] }); } dr.Close(); } return inv; }

7 DataSets Client Application DataSet Data Adapter Database

8 DataSets Represents a database in memory Includes: – Tables – Relationships – Tables in include columns (fields) Constraints Name Datatype Unique? Column number (ordinal)

9 DataSets When the dataset is modified The modifications are known Errors are detected They can be written back to the database using the DataAdapter They can be rolled back

10 Datasets Gui and LinQ private void CreateDataView2() { dataGridYugosView.DataSource = from car in inventoryTable where (string)car["Make"] == "Yugo" select car; dataGridYugosView.DataError += new DataGridViewDataErrorEventHandler(DataGridView1_DataError); }

11 The Entity Framework The third layer

12 EF Separate the strong link between the DB and the program Build strongly typed objects classes to represent the DB in the program. These objects are called the Entity Data Model(EDM) A set of XML files called edmx files connect between the DB and the EDM classes.

13 EF components Object Services: – all EDM classes inherit from EntityObject. Its in charge of monitoring modifications to the object in order to then update the corresponding DB record(s) EntityClient namespace: – Its like a data provider but fro EDM objects. *.edmx files: – Conceptual model (Describe EDM classes) – Physical model (Describe DB tables and rels) – Logical model (relates the EDM to the DB tables)

14 All Together Now C# Code Object Services Entity Client Data Provider ADO.NET Data Provider Physical Database DbDataReader EntityDataReader *.edmx IEnumerable LINQ Query Entity SQL Command Tree

15 *.edmx files: DB Storage

16 Conceptual Model (EDM)

17 C-S mapping


Download ppt "Intro to C#.net and EF Ilan Shimshoni. The Three Faces of ADO.NET The connected layer – Directly connecting to the DB The disconnected layer – Using datasets."

Similar presentations


Ads by Google