ASP.NET - How to call stored procedure with Linq to Sql

Asked By aileni giri on 31-Mar-12 02:02 AM
i want to delete one record  from table....
 so i was  prepared one stored procedure ,but i don;t know how to  call through linq to sql
  
         anybody can help  me  please..................................Thanks Adv
Chintan Vaghela replied to aileni giri on 31-Mar-12 02:16 AM

Hello,

 

using LINQ to call stored procedure, first we need to create new connection and map it on LINQ To SQL designer.

check this article for more detail: http://www.fryan0911.com/2009/05/what-is-linq-to-sql-introduction.html

If you already finished mapping the stored procedure on LINQ to SQL designer, lets move forward to the actual code. On the example below, I used the stored procedure "GetProductsByCategory" which accepts category as parameter.


using (NorthwindDataContext datacontext = new NorthwindDataContext())
{
 //retrieve all product that are beverage using stored procedure "GetProductsByCategory"
 var products = datacontext.GetProductsByCategory("Beverages");
 //display all retrieved products from stored procedure
 foreach(Product p in products)
 {
  Console.WriteLine(p.ProductName);
 }
}

 

Hope this helpful!

Thanks

Anoop S replied to aileni giri on 31-Mar-12 03:17 AM
To use these stored procedures with LINQ to SQL, LINQ to SQL need  maps database-defined abstractions to code-generated client objects, so that you can access them in a strongly typed manner from client code.

Refer this for more details ->Stored Procedures (LINQ to SQL)
http://msdn.microsoft.com/en-us/library/bb386946.aspx