LogoASPAlliance: Articles, reviews, and samples for .NET Developers
DLINQ with Stored Procedures
by Scott Guthrie
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 12788/ 74


Republished with Permission - Original Article

I've blogged a few times about LINQ and DLINQ over the last few weeks (and will be blogging a lot more about them in the future).  You can read my overview post on LINQ here, and my introduction to DLINQ with ASP.NET here.

Sahil Malik has also been working on a great LINQ/DLINQ tutorial series on his blog here (he is also the author of the very popular Pro ADO.NET 2.0 book).  Recently he has posted several good posts about how to call SPROCs and UDFs using LINQ and DLINQ.  You can read them here:

How to use SQLMetal to create DLINQ classes that call SPROCs

Calling SPROCs from DLINQ that return Scalar Values

Calling SPROCs from DLINQ that return a definite shape

Calling SPROCs from DLINQ that return variable shapes

To quickly summarize at a high-level, to enable SPROCs with DLINQ you should pass the "/sprocs" flag to SQLMetal when you create your DAL classes:

   "c:\Program Files\LINQ Preview\Bin\SqlMetal.exe" /server:. /database:Northwind /pluralize /sprocs /code:Northwind.cs

Then assuming you have a SPROC defined like so in the Northwind database like so:

      @City NVARCHAR(30)
      CustomerID, ContactName, Address 
WHERE City = @City

You can invoke the SPROC and get back and print out a list of name results using the below LINQ/DLINQ code:

   Northwind db = new Northwind(connectionString); 
   var contactNames = from customer in db.GetCustomersInCity("London")
                      select customer.ContactName;
   foreach (string contactName in contactNames) {
       Response.Write("Contact: " + contactName + "<br>") ;

You could also obviously databind the list of customers to an ASP.NET control like so as well (the below result will output a GridView with 3 columns):

   Northwind db = new Northwind(connectionString); 
   GridView1.DataSource = from customer in db.GetCustomersInCity("London")
                          select customer;

Subscribe to Sahil's blog to learn more. And if you haven't yet, make sure to read my LINQ and DLINQ overview posts for ASP.NET.

Hope this helps,

- Scott


Product Spotlight
Product Spotlight 

©Copyright 1998-2018  |  Page Processed at 2018-11-15 2:04:02 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search