views:

45

answers:

1

I'm working on a project right now where we have to implement all communications to the database via stored procedures. We are using Linq-to-SQL, so all the database entities are in place. My question is how you could write a stored procedure that returns an entity, if this is possible.

Example, we validate a user when he's logging in with username and pass, and then do a check, but how should I write to get the stored procedure to return an Employee entity?

CREATE PROCEDURE GetLogin (
    @UserName NVARCHAR(50),
    @Password NVARCHAR(50))
+2  A: 

What does your GetLogin procedure do and/or return??

When you add a stored procedure to a Linq-to-SQL data context, you can define a "return type" in the properties window - if you're sure your stored procedure returns all the properties that make up a User, then you could set the return type of that stored proc to User and then this should work.

In that case, your code would look something like this:

User found = ctx.GetLogin("YourUserName", "TOP$SECRET").SingleOrDefault();

If the user was found in the database, found will contain that user - otherwise, it'll be NULL.

Update: once you have such a User object, of course, you can use it just like any other Linq-to-SQL object !

You can modify properties:

found.UserName = "New User Name";
ctx.SubmitChanges();

or you can delete it:

ctx.Users.DeleteOnSubmit(found);
ctx.SubmitChanges();

The object you're getting back is a Linq-to-SQL object just like any other!

marc_s
Hello, thanks for the help! It worked like a charm when I changed it to return users, so now it reutrns a list and I just select the first. But if i'm not sure of what I will get in return, how do I in the best way interact with that data? For example, if I want to return a user like above, but there is an error, and I then want the procedure to return an string with error message, how should I handle that? //Thanks
Fore
@Fore: in that case, throw an exception with that error message, and in your application, catch the exception and present the error to the user (something like "user not found") - that's the standard .NET way of doing stuff like that
marc_s
ok, so once I have this user, am I able to edit it, or delete it from the database, without resorting to database lookup? I mean, how does it know that it is actually a valid user from the database? What if I transmit the data in the same form, but no such data actually exists in the database? In this case this will no longer be a real entity, but rather a simple data structure with zero functionality (which is what you get from SP in LINQ to SQL). That's what I meant in the above comment.
Alexander
@Alexander: YES YES YES ! You can do **ALL OF THOSE THINGS** ! See my update - and TRY IT YOURSELF !
marc_s
Ok then. Thank you for your patience :). Will definitely try this in my new project. For now I'm far from the DB, using mock classes.
Alexander
Although I am not getting, why am I having trouble with LINQ to SQL when working with detached entities. I guess it's just another question.
Alexander