Saturday, September 11, 2010

How to connect linq to sql:-

1.Add New Item -->LINQ to SQL classes
It will create 3 files in App_Code Folder
1.Whatever you name created.dbml
2.Whatever you name created.dbml.layout
3.Whatever you name created.dbml.cs
2.Open .dbml file
It will totally blank with two part.
3.Drag and drop all the tables in the dbml file.
When you drop tables it will automatically create relationship between tables as per primary key and foreign key.
4.Now you are ready to use linq to sql

First create the object of that lind class
Suppose name is HotelDb then create object as shown
HotelDbDataContext obj=new HotelDbDataContext();
It will by default attach DataContext with class name.
Now all the table names converted in the plural(Bahuvachan)
e.g. City become cities etc.
Now to select all the data from table
Griedview1.datasource=obj.tablename in plural(It will display in dropdown)
Griedview.databind();