Introduction
In this article we would see how can we use Linq to Sql ORM in Silverlight 3 Application for interactinig with SQL Server Database.
Creating Silverlight Project
Fire up Visual Studio 2008 and create a new Silverlight 3 Project. Name it as LINQ2SQLSL3.
Now that you have created the Silverlight 3 Application, we would add a LINQ to SQL file that is *.dbml .
For your information, I have created a Database named "EmployeeDB" with a table "EMP_BASIC_INFO" with the following details.
Now, connect to the SQL Server from Server Explorer as shown in below figure.
Drag and Drop the above above displayed table to the Object Relational Designer for Linq to SQL.
Now we would change the above names to be user friendly.
To interact with LINQ to SQL we need to have a service that client can call at any time.
Add Silverlight enabled WCF Service to the Web Project.
In the Service.svc.cs add methods as per your requirement.
I have written two methods such as:
And
After writing the service build the project and get this service's reference in Silverlight 3 Project as shown in below figure.
After you add the service it would create the ServiceReferences.ClientConfig file.
Now it's time to design our Silverlight UI in Blend, I have gone for a simple approach, where simple buttons and textbox would help us getting the result which would be displayed in DataGrid.
The following XAML is for your reference:
Now we would write client side code to call the service and display the result.
Following code is for Show All Button Click and displaying the result.
Following code is for Show the text typed in txtSearch and based on that the result would be displayed.
I guess that's it. Now we can test our application.
When some text is typed and Search Button is clicked all the Employee whose name starts with the given text would be displayed.
Hope this article helps.
Join the conversation! Your thoughts help the community grow.
Sign in to leave a comment
It is the same account you read, post and publish with — and you will come straight back to this page.

Harald UlbrichPosted Mar 18, 2012, 11:50 PM
Hi Diptimaya, Service.svc.cs renders 2 error-messages: 1)The name 'context' does not exist in current context 2)The type of namespace 'List' could not be found Could you please help me? Thank you Harald!
Nguyen NhaPosted Feb 14, 2012, 12:27 PM
I do not find the object "ServiceClient"
Dale HermsenPosted Jan 31, 2011, 6:05 PM
This article was very, very helpful! I was so happy to see it in C#. Thanks!
rkbhuvanagiriPosted Feb 16, 2010, 11:09 PM
Nice Article
Artyom MuradovPosted Feb 16, 2010, 4:45 AM
1. This code is better because DataContext is IDisposable: using (var dc = new DBDataContext()) { } 2. You do not use LINQ to SQL in Silverlight! You use LINQ to SQL in WCF and WCF in Silverlight! Please, write correct title of articles. p.s. Sorry for my english - i am russian