views:

31

answers:

2

I am in the process of migrating some legacy code to LINQ to SQL and I am having some difficulty with the following Stored Procedure.

BEGIN
    INSERT INTO tblCMOEncounterHeader (
        submitter_organization_id, 
        submission_date, 
        begin_posting_date,
        end_posting_date, 
        number_of_records_transmitted) 
    VALUES (
        @submitter_organization_id,
        @submission_date,
        @begin_posting_date,
        @end_posting_date,
        @number_of_records_transmitted)

    RETURN @@ROWCOUNT
END

Specifically, what does @@ROWCOUNT return when partnered with an INSERT? How would I accomplish the same thing in LINQ?

+1  A: 
@@ROWCOUNT 

Returns the number of rows affected by the last statement

MSDN

In your case it will always return 1, I guess, or raise an error in case of constraint violation, etc.

Your LINQ to SQL code should definitely return (numsRowsAffected > 0);, i.e. a boolean value or something similar

abatishchev
is there a built in way to get `numRowsAffected` from the `Context` or `Changeset`, maybe, in LINQ?
Refracted Paladin
@Refracted Paladin: after `db.SubmitChanges();` call `db.GetChangeSet().Updates.Count` it is exactly what are looking for
abatishchev
@abatishchev: `Updates` or `Inserts`? I don't want to take your answer too literal I just want to make sure there isn't something strange going on.
Refracted Paladin
@Refracted Paladin: Yea, sorry, you're right, `Inserts`. http://msdn.microsoft.com/en-us/library/system.data.linq.changeset_properties.aspx
abatishchev
+1  A: 

Since you are only inserting one row, it should return 1. (Had you used INSERT INTO ... SELECT FROM... to insert multiple rows, it would be higher)

In LINQ, drag the stored proc from the Server Explorer, to the LINQ diagram (much as you did for the tables). It will create a method on the DataContext for the strored proc. (You may have to open the Method pane, from the context menu)

James Curran
I think I may have my answer then. It looks like it was being used in a `boolean` way to validate the success of the `INSERT`
Refracted Paladin