0% found this document useful (0 votes)
4 views

LINQ To SQL

Uploaded by

mailtrash939
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

LINQ To SQL

Uploaded by

mailtrash939
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 8

LINQ VARITIES

1. LINQ to Object:
using this we can write queries on arrays , collections, lists e t c…
2. LINQ to DATABASE:
using this we can write queries on data tables, relational D.B tables.
i) LINQ to ADO.net: using this we can write queries on database such as
Microsoft SQL server, Oracle and others.
ii) LINQ to SQL : use to write queries on database available only on SQL server.
iii) LINQ
to entities: use to write queries on SQL server as well as other relational
D.B like oracle.
iv) LINQ to data set : The data set object was introduced in first version of the .Net
frame work .This variety of LINQ enables us to write query on dataset.

3. LINQ to XML: Using this we can write queries on XML files . It provides creation and
manipulation of XML documents .
Using LINQ with Databases:
• LINQ to SQL is a query language that is introduced in .NET 3.5 framework for working with
relational data base. i.e. SQL Server.
• LINQ to SQL is not only about querying data but also allows us to perform insert, delete,
update operations and we call them as CRUD operations.
• CRUD : Create (insert) Read (Select) Update , delete.

LINQ SQL
Compile time syntax checking Runtime syntax checking
Type safe Not type safe
Intelligence support is available No intelligence support
Debugging of LINQ sql is possible Debugging of Slq statements are not
possible.
When working with LINQ
• Every table is now going to become a class
• Column -> property
• Rows/Records  Instance
• Stored procedures  methods.
How to work with LONQ to SQL:
• To work with LINQ to SQL first we need to convert all the relational objects of DB into
object oriented types this process is known as ORM. (object relational mapping)
• To perform ORM use tool called OR designer.
1. Perform ORM by adding OR designer.
2. Adding a reference for an assembly . i.e. System.Data.Linq.dll
3. We need to write connection string into the config file.
• First in SQL serve create one DB in that table emp table.
• In VS 2010 open NewFile Project  window application
• In window project goto server explorer and configure the DB under server explorer.
• Now perform ORM ; to do this use OR designer under project open solution explorer right
click on application
• Add newitem Linq to Sql classes. Its extension is .dbml (data base markup language)
click ok
• Now we get two panels in diagram window.
• Now what ever the tables we want to work, all those tables drag and drop into left side
panel from server explorer.
• When drag and drop tables 2 things going to happen:
• connection string is added into config file automatically by OR designer.
• In designer. Cs file one more parameter less constructor added.
• Place grid view control in the form
• Place grid view control in the form
• Form1_ Load()
{ // create instance of DB context class
DBcontext class dc= new Dbcontext class();
datagridview1.Datasource=dc.emps;
}
Now run the application.
Here datacontext class will be used to connect to DB.
It returns generic of emptable.
Using LINQ write Database Query:
• The syntax for writing query to access data from DB using LINQ is:
From < alias> in <table> [ <clauses>]
select <alias> | new {<column list>}
Here clauses are: where, groupby, orderby
Ex: to access all data from emp
syntax:
var tab= from e in dc.emps
select e;
Where tab is table implicitly created to store result.
Ex2:- var tab= from e in dc.emps
where e.status==“manager”
select e;
Now in form_Load() { write query and bind the tab to data grid view control.}
LINQ to SQL (using Console Application)
• It is mainly designed to work with SQL server DB.
• It provides a run time infrastructure for managing relational data as objects.
• It allows us to write queries to retrieve and manipulate data from the sql
server.
• LINQ to SQL creates an object relation mapping (ORM) layer between the
tables in the SQL DB and objects in C# program.
• LINQ to SQl translates the SQL queries into an object model and send them to
DB for execution
• 1. Open Console application.
• Create LINQ to SQL class by right click the solution name in solution explorer
select add newitem  add Linq to SQL class click ok.
• This will open “object Relation Designer window.
LINQ to SQL (using Console Application)
• in this window, we can drag and drop he tables from server explorer.
• This action will add the table to our application
• In program.cs
class Program
{
static void main()
{
myclassdatacontex dc= new myclass datacontext();
var query = dc. Products;
foreach (product iten in query )
{ console.Write (“{0} {1} {2} “ item.productid, item,productname); }

You might also like