LINQ to SQL - Stored Procedure Return Type Error

2019-07-24 16:54发布

问题:

On SQL 2005 I have a simple SP that selects rows from a table. The SP does not use temporary tables or return multiple result sets. In VS2010 I drag the SP from the Server Explorer to the DBML designer and the SP is added to the LINQ data context. Everything works okay. If I then script the table and SP on to another live SQL 2005 server I am getting the error "The return types for the following stored procedures could not be detected .. " Like I say, no temp tables or multiple result sets that would typically produce this error. Could there be something else on the server causing this?

SET ANSI_NULLS ON
    GO
    SET QUOTED_IDENTIFIER ON
    GO
    CREATE PROCEDURE [dbo].[spUsers]
    AS
    BEGIN
        SET NOCOUNT ON;

        SELECT top 100 * from Users
    END

回答1:

Make sure you have:

SET NOCOUNT ON;

as the first line in your SP after the 'BEGIN' statement.

If your SP does not have this, then it returns messages like

'10 Rows affected...'

Which Linq tries to interprete as part of the function result. Iv'e had it bite me loads of times!!



回答2:

This is a known issue with Linq to SQL, and a frustrating one. It also seems to be somewhat unpredictable. The only way I have gotten around this, reliably, is by not having L2S call a stored procedure when I need it to return a result set. I have it call a table-valued UDF instead. L2S seems to have no problems with these, and I've never experienced any problems using them.



回答3:

Okay, the reason it was failing on the live server is to do with privilages needed to access the meta-data on the DBMS. These are needed to create the return value data type from the SP. By elevating the SQL user account and then dragging the SP on onto the DBML designer .. bingo .. it works!



回答4:

Use

create procedure Getxxxxxxx

@xxxxx uniqueidentifier ,@xxxxx uniqueidentifier

set fmtonly off //you query set fmtonly on