Shawn Weisfeld

I find when I talk to myself nobody listens. - Shawn Weisfeld
posts - 356, comments - 173, trackbacks - 34

My Links

News

The views expressed in this blog are mine and mine alone, not that of my employer, Microsoft, or anyone else’s. No warrantee is given for the quality of any material on this site.

Archives

Post Categories

Call a stored procedure with LINQ to SQL and without the designer

In my last post I talked about how to do a simple select with LINQ to SQL without the designer (http://drowningintechnicaldebt.com/blogs/shawnweisfeld/archive/2009/07/11/a-simple-select-with-linq-to-sql-and-without-the-designer.aspx). This is all well and good, but what if you need to use a stored procedure. Like this one. . . .

image

The first step is to add to our data context a method that maps to our stored procedure. You can see we start off with a Function attribute that tells LINQ to SQL what stored procedure to use. Additionally we decorate the parameters on our method with Parameter attributes that tell LINQ to SQL how to map the parameters. Then we just call our base classes ExecuteMethodCall passing along all the attributes (using the .NET reflection MethodInfo class), and our parameter.

image

Now we just need to use our new method.

image

Here we can see we get the same results as we did in my last post.

image

In my next post we will use Multiple-Rowset to return multiple result sets from my stored procedure.

Print | posted on Saturday, July 11, 2009 10:15 PM | Filed Under [ C# LINQ to SQL ]

Feedback

Gravatar

# Call a Multiple-Rowset stored procedure with LINQ to SQL and without the designer

7/12/2009 4:45 PM | drowningintechnicaldebt.com
Gravatar

# re: Call a stored procedure with LINQ to SQL and without the designer

Sir Can you please tell me what will be the coding if we are using join query with in a store procedure.
2/28/2012 4:07 AM | Ankit Nandekar
Gravatar

# re: Call a stored procedure with LINQ to SQL and without the designer

Ankit

First, if this is a new project I would strongly consider using Entity Framework not LINQ to SQL.

To call a sproc that contains a join would be no difference. All LINQ to SQL cares about is the “shape” of the result set. So you will need to create an object that looks like the result set of your stored procedure.

Hope that helps.
2/28/2012 8:05 AM | shawnweisfeld

Post Comment

Title  
Name  
Email
Url
Comment   
Please add 2 and 3 and type the answer here:

Powered by: