简体   繁体   English

Linq to sql存储过程返回值

[英]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). 我在SQL Server 2005数据库中具有以下存储过程(仅用于返回以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. 当我在SQL Server Management Studio中运行此文件时,它可以正常工作,并以MB为单位返回当前的数据库大小。

I want this to run inside an application, so I added it to a linq to sql datacontext, which generated the following code: 我希望它在应用程序中运行,因此我将其添加到了sql datacontext的linq中,生成了以下代码:

    [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(...) . L2S不执行类似于ExecuteScalar(...)

If you use a UDF , you'll have better luck. 如果您使用UDF ,那么会更好。 This post speaks to the problem nicely. 这篇文章很好地说明了这个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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