简体   繁体   中英

Linq to sql stored procedure return value

I have the following stored procedure in an SQL Server 2005 database (meant simply to return the database size in MB).

ALTER PROCEDURE [dbo].[dbSize]
AS
BEGIN
SET NOCOUNT ON;

DECLARE @sizeMb int
DECLARE @DB_NAME varchar(100)

SELECT @DB_NAME = DB_NAME()

SELECT @sizeMb = (size*8)/1024 FROM sys.master_files
    WHERE DB_NAME(database_id) = @DB_NAME
    AND Name = @DB_NAME

RETURN @sizeMb  
END

When I run this in SQL Server Management Studio, it works correctly, returning the current DB Size in MB.

I want this to run inside an application, so I added it to a linq to sql datacontext, which generated the following code:

    [global::System.Data.Linq.Mapping.FunctionAttribute(Name="dbo.dbSize")]
    public int dbSize()
    {
        IExecuteResult result = this.ExecuteMethodCall(this, ((MethodInfo)(MethodInfo.GetCurrentMethod())));
        return ((int)(result.ReturnValue));
    }

I call it like so:

int dbSize = db.dbSize();

However, it only returns zero, never anything else. No exceptions of any kind are thrown either. I experimented with selecting a result and using output parameters, but that didn't help either (the output parameter was zero as well). Any suggestions?

I've run into the same problem. As dumb as it sounds, you have to return a single row with single column containing your value from a stored procedure . L2S doesn't do something akin to ExecuteScalar(...) .

If you use a UDF , you'll have better luck. This post speaks to the problem nicely.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM