Upload
hector-felipe-calla-mamani
View
248
Download
0
Embed Size (px)
Citation preview
8/9/2019 LINQ to ADO.NET
http://slidepdf.com/reader/full/linq-to-adonet 1/26
8/9/2019 LINQ to ADO.NET
http://slidepdf.com/reader/full/linq-to-adonet 2/26
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?
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
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
8/9/2019 LINQ to ADO.NET
http://slidepdf.com/reader/full/linq-to-adonet 6/26
8/9/2019 LINQ to ADO.NET
http://slidepdf.com/reader/full/linq-to-adonet 7/26
8/9/2019 LINQ to ADO.NET
http://slidepdf.com/reader/full/linq-to-adonet 8/26
8/9/2019 LINQ to ADO.NET
http://slidepdf.com/reader/full/linq-to-adonet 9/26
8/9/2019 LINQ to ADO.NET
http://slidepdf.com/reader/full/linq-to-adonet 10/26
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
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!!!
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!!
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
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…
8/9/2019 LINQ to ADO.NET
http://slidepdf.com/reader/full/linq-to-adonet 16/26
8/9/2019 LINQ to ADO.NET
http://slidepdf.com/reader/full/linq-to-adonet 17/26
8/9/2019 LINQ to ADO.NET
http://slidepdf.com/reader/full/linq-to-adonet 18/26
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
8/9/2019 LINQ to ADO.NET
http://slidepdf.com/reader/full/linq-to-adonet 20/26
8/9/2019 LINQ to ADO.NET
http://slidepdf.com/reader/full/linq-to-adonet 21/26
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
8/9/2019 LINQ to ADO.NET
http://slidepdf.com/reader/full/linq-to-adonet 23/26
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.
8/9/2019 LINQ to ADO.NET
http://slidepdf.com/reader/full/linq-to-adonet 25/26
8/9/2019 LINQ to ADO.NET
http://slidepdf.com/reader/full/linq-to-adonet 26/26