+ All Categories
Home > Documents > LINQ to ADO.NET

LINQ to ADO.NET

Date post: 01-Jun-2018
Category:
Upload: hector-felipe-calla-mamani
View: 233 times
Download: 0 times
Share this document with a friend
26
Transcript
Page 1: LINQ to ADO.NET

8/9/2019 LINQ to ADO.NET

http://slidepdf.com/reader/full/linq-to-adonet 1/26

Page 2: LINQ to ADO.NET

8/9/2019 LINQ to ADO.NET

http://slidepdf.com/reader/full/linq-to-adonet 2/26

Page 3: LINQ to ADO.NET

8/9/2019 LINQ to ADO.NET

http://slidepdf.com/reader/full/linq-to-adonet 3/26

LINQ to SQL allows querying a relational structureby converting the LINQ query into a native SQLquery.This means… that the queries that you write incode generates appropriate queries to access theactual database, AUTOMATICALLY!There are several concepts that you will come toknow in this module… ◦ How is the query written using object names (like tables

or views)?◦

How is it validated by the compiler?◦ When is the query generated?◦ When is the query executed?

Page 4: LINQ to ADO.NET

8/9/2019 LINQ to ADO.NET

http://slidepdf.com/reader/full/linq-to-adonet 4/26

You could either… ◦ Create a class and annotate it with the Table name◦ Define Fields (which correspond to columns)◦ Get the Data context (The DataContext class

handles the communication between LINQ andexternal relational data sources)

◦ Create a Generic type◦ Query through LINQ

OR… use LINQ to SQL Classes in VS 2008

Page 5: LINQ to ADO.NET

8/9/2019 LINQ to ADO.NET

http://slidepdf.com/reader/full/linq-to-adonet 5/26

Data abstractionWorking with Strongly Typed objectsTied to the object model and NOT to thedatabaseQuery semantics is much more meaningfulRight now, ONLY SQL is supported, but goingforward there will be more providers whichwould provide access to different databasesOnce new providers come in, you couldconnect to any database with virtually thesame code base

Page 6: LINQ to ADO.NET

8/9/2019 LINQ to ADO.NET

http://slidepdf.com/reader/full/linq-to-adonet 6/26

Page 7: LINQ to ADO.NET

8/9/2019 LINQ to ADO.NET

http://slidepdf.com/reader/full/linq-to-adonet 7/26

Page 8: LINQ to ADO.NET

8/9/2019 LINQ to ADO.NET

http://slidepdf.com/reader/full/linq-to-adonet 8/26

Page 9: LINQ to ADO.NET

8/9/2019 LINQ to ADO.NET

http://slidepdf.com/reader/full/linq-to-adonet 9/26

Page 10: LINQ to ADO.NET

8/9/2019 LINQ to ADO.NET

http://slidepdf.com/reader/full/linq-to-adonet 10/26

Page 11: LINQ to ADO.NET

8/9/2019 LINQ to ADO.NET

http://slidepdf.com/reader/full/linq-to-adonet 11/26

So far, in the last few slides we have seen thatcreating classes needs manually a lot of effortIt probably defeats the purpose of makingprogramming easyLet’s have a look at another way before youmake up your mind

Page 12: LINQ to ADO.NET

8/9/2019 LINQ to ADO.NET

http://slidepdf.com/reader/full/linq-to-adonet 12/26

Follow these steps with me… ◦ Create a new Windows Forms Application (C#)◦ Add a new item Linq to SQL Classes◦ Drag and drop Customer and Order tables◦ Switch to Form1◦ Go to Data Source window -> Add new data source◦ Select object (hit next)◦ Select LINQtoSQL -> Customer (click next followed with

Finish)◦ Drag and drop customers (dock the control)◦ Go to Form_load and write the following code…

NorthwindDataContext db = new NorthwindDataContext();◦ Now, you are all set… just start querying!!!

Page 13: LINQ to ADO.NET

8/9/2019 LINQ to ADO.NET

http://slidepdf.com/reader/full/linq-to-adonet 13/26

Write the following in Form1_Load and hitF5!!

Page 14: LINQ to ADO.NET

8/9/2019 LINQ to ADO.NET

http://slidepdf.com/reader/full/linq-to-adonet 14/26

By default, you will be able to add therecords, and edit them as well, but the SAVEbutton is enabled.Right click on the Save button and selectpropertiesMake enabled property = true and doubleclick on the save button

Page 15: LINQ to ADO.NET

8/9/2019 LINQ to ADO.NET

http://slidepdf.com/reader/full/linq-to-adonet 15/26

Add a textbox and a button to the toolstripand in the button’s click event put in thefollowing code…

Page 16: LINQ to ADO.NET

8/9/2019 LINQ to ADO.NET

http://slidepdf.com/reader/full/linq-to-adonet 16/26

Page 17: LINQ to ADO.NET

8/9/2019 LINQ to ADO.NET

http://slidepdf.com/reader/full/linq-to-adonet 17/26

Page 18: LINQ to ADO.NET

8/9/2019 LINQ to ADO.NET

http://slidepdf.com/reader/full/linq-to-adonet 18/26

Page 19: LINQ to ADO.NET

8/9/2019 LINQ to ADO.NET

http://slidepdf.com/reader/full/linq-to-adonet 19/26

Show how you can attach to an SPShow method optionsCheck the DelayLoaded property in thedesignerModify the custom logic for Delete, Update,Insert

Page 20: LINQ to ADO.NET

8/9/2019 LINQ to ADO.NET

http://slidepdf.com/reader/full/linq-to-adonet 20/26

Page 21: LINQ to ADO.NET

8/9/2019 LINQ to ADO.NET

http://slidepdf.com/reader/full/linq-to-adonet 21/26

Page 22: LINQ to ADO.NET

8/9/2019 LINQ to ADO.NET

http://slidepdf.com/reader/full/linq-to-adonet 22/26

Drag and drop customer and ordersAdd a new Windows FormClick on Data Menu -> Show data sourcesAdd a new data source and select object and clicknextSelect Customer from the tree and click nextDrag and drop CustomerNow, drag and drop OrdersCreate the datacontext in the main class called

db.Assign the customerBindingSource’s DataSourceto CustomersAdd db.log = console.out in form_load

Page 23: LINQ to ADO.NET

8/9/2019 LINQ to ADO.NET

http://slidepdf.com/reader/full/linq-to-adonet 23/26

Page 24: LINQ to ADO.NET

8/9/2019 LINQ to ADO.NET

http://slidepdf.com/reader/full/linq-to-adonet 24/26

LINQ to DataSet interacts with data already inADO.NET DataSet structures.A dataset could be considered as an in memorydatabase, and hence it is a good target for a LINQimplementation

A DataTable can be queried with LINQ, just as anyother IEnumerable T > list.NOTE > DataTable does not implementIEnumerable<T>. You have to call AsEnumerable ,which is an extension method for DataTable , toobtain a wrapper that implements that interface.A typed DataSet can be queried with a simpler syntaxbecause it is not necessary to use the Field<T>accessor and the AsEnumerable method.

Page 25: LINQ to ADO.NET

8/9/2019 LINQ to ADO.NET

http://slidepdf.com/reader/full/linq-to-adonet 25/26

Page 26: LINQ to ADO.NET

8/9/2019 LINQ to ADO.NET

http://slidepdf.com/reader/full/linq-to-adonet 26/26


Recommended