LINQ to SQL allows querying a relational structure by converting the LINQ query into a native SQL query.. You could either… ◦ Create a class and annotate it with the Table name ◦ D
Trang 2 LINQ to SQL
LINQ to DataSet
Trang 3 LINQ to SQL allows querying a relational structure
by converting the LINQ query into a native SQL
query
This means… that the queries that you write in
code generates appropriate queries to access the actual database, AUTOMATICALLY!
There are several concepts that you will come to know 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?
Trang 4 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 and
external relational data sources)
◦ Create a Generic type
◦ Query through LINQ
OR… use LINQ to SQL Classes in VS 2008
Trang 5 Data abstraction
Working with Strongly Typed objects
Tied to the object model and NOT to the
database
Query semantics is much more meaningful
Right now, ONLY SQL is supported, but going forward there will be more providers which would provide access to different databases
Once new providers come in, you could
connect to any database with virtually the
same code base
Trang 7 It plays a few important roles…
◦ Provide metadata to LINQ queries
◦ Provide storage for data read from relational data source
◦ Also helps in tracking updates
◦ Supports submission back to the datasource
It does not need to be instantiated
It can’t be a value type (struct)
Any class could be annotated with the Table attribute to declare an
entity
It can have any number and type of members
ONLY members annotated with Column is mapped to the table
MUST have a key If you don’t have a key, you cannot update (although you can still access the data)
IsPrimaryKey attribute needs to be changed to make the Column a
PrimaryKey
If a column name is Auto-generated by the database, you need to
provide IsDBGenerated parameter to true along with the DBType, like…
Trang 10 Class entities can use Association attribute to define relationships
Uses EntityRef to refer to the related table
Trang 11 So far, in the last few slides we have seen that creating classes needs manually a lot of effort
It probably defeats the purpose of making
programming easy
Let’s have a look at another way before you make up your mind
Trang 12 Follow these steps with me…
◦ Add a new item Linq to SQL Classes
◦ 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)
NorthwindDataContext db = new NorthwindDataContext();
◦ Now, you are all set… just start querying!!!
Trang 13 Write the following in Form1_Load and hit F5!!
Trang 14 By default, you will be able to add the
records, and edit them as well, but the SAVE button is enabled
Right click on the Save button and select
properties
Make enabled property = true and double click on the save button
Trang 15 Add a textbox and a button to the toolstrip and in the button’s click event put in the
following code…
Trang 19 Show how you can attach to an SP
Show method options
Check the DelayLoaded property in the
designer
Modify the custom logic for Delete, Update, Insert
Trang 22 Drag and drop customer and orders
Click on Data Menu -> Show data sources
Add a new data source and select object and click next
Select Customer from the tree and click next
Now, drag and drop Orders
Create the datacontext in the main class called
Trang 24 LINQ to DataSet interacts with data already in
ADO.NET DataSet structures
A dataset could be considered as an in memory
database, and hence it is a good target for a LINQ
implementation
A DataTable can be queried with LINQ, just as any
other IEnumerable<T> list
NOTE > DataTable does not implement
IEnumerable<T> You have to call AsEnumerable,
which is an extension method for DataTable, to
obtain a wrapper that implements that interface
A typed DataSet can be queried with a simpler syntax because it is not necessary to use the Field<T>
accessor and the AsEnumerable method