Monday, November 1, 2010

Calling a SQL Server Stored Procedure using LINQ

1. Make sure you have Northwind database in your Sql server.
2. Create a simple stored procedure to retrieve customers based on a specific country.
The parameter of the stored procedure is of type varchar.
3. Create a simple asp.net website or winforms or console project.
4. Add a Linq to SQL class to your project.
5. Make sure to add the stored procedure in your linq to sql class.
6. In your asp.net website, add a button,textbox and a gridview.
7. In your button event click, add the following snippet:

1
2
3
4
5
6
7
8
protected void Bind()  
{  
    string country = this.txtCity.Text.Trim();  
    var cr = new NorthWindDataContext();  
    var north = cr.CustomerByCountry(country);  
    this.gridCustomers.DataSource = north;  
    this.gridCustomers.DataBind();  
}  

That's it...

0 comments:

Post a Comment