LINQ-to-SQL:返回单个标量值的存储过程?

dri*_*iis 13 c# linq-to-sql

我正在使用LINQ-to-SQL来查询旧数据库的应用程序.我需要调用一个存储过程,它选择一个整数值.无法更改存储过程.

设计者使用此签名创建方法:

private ISingleResult<sp_xal_seqnoResult> NextRowNumber([Parameter(DbType="Int")] System.Nullable<int> increment, [Parameter(DbType="Char(3)")] string dataset)
Run Code Online (Sandbox Code Playgroud)

我希望返回类型为int.如何使用LINQ-to-SQL执行此操作?

Mar*_*ell 14

对于标量函数(UDF)而不是SP,这将是微不足道的.但是,它应该足够容易 - 虽然如果SP很复杂(即FMT_ONLY无法100%检查它),那么你可能需要"帮助"它......

这是我从一个返回整数的简化SP生成的一些dbml; 您可以通过"open with ... xml editor"编辑dbml:

<Function Name="dbo.foo" Method="foo">
    <Parameter Name="inc" Type="System.Int32" DbType="Int" />
    <Parameter Name="dataset" Type="System.String" DbType="VarChar(20)" />
    <Return Type="System.Int32" />
</Function>
Run Code Online (Sandbox Code Playgroud)

(注意你显然需要调整名称和数据类型).

这是生成的C#:

[Function(Name="dbo.foo")]
public int foo([Parameter(DbType="Int")] System.Nullable<int> inc, [Parameter(DbType="VarChar(20)")] string dataset)
{
    IExecuteResult result = this.ExecuteMethodCall(this, ((MethodInfo)(MethodInfo.GetCurrentMethod())), inc, dataset);
    return ((int)(result.ReturnValue));
}
Run Code Online (Sandbox Code Playgroud)

如果您当前的SP使用SELECT(而不是RETURN),那么DBML将需要反映这一点.您可以通过隐藏实现细节并在分部类中提供公共包装来解决此问题; 例如:

<Function Name="dbo.foo" Method="FooPrivate" AccessModifier="Private">
    <Parameter Name="inc" Type="System.Int32" DbType="Int" />
    <Parameter Name="dataset" Type="System.String" DbType="VarChar(20)" />
    <ElementType Name="fooResult" AccessModifier="Internal">
      <Column Name="value" Type="System.Int32" DbType="Int NOT NULL" CanBeNull="false" />
    </ElementType>
</Function>
Run Code Online (Sandbox Code Playgroud)

以上描述了一个返回单个表的SP的SP; 但我已经使SP"私有"到数据上下文,结果类型"内部"到程序集(隐藏它):

[Function(Name="dbo.foo")]
private ISingleResult<fooResult> FooPrivate(
    [Parameter(DbType="Int")] System.Nullable<int> inc,
    [Parameter(DbType="VarChar(20)")] string dataset)
{
    IExecuteResult result = this.ExecuteMethodCall(this, ((MethodInfo)(MethodInfo.GetCurrentMethod())), inc, dataset);
    return ((ISingleResult<fooResult>)(result.ReturnValue));
}
Run Code Online (Sandbox Code Playgroud)

现在在我自己的类文件中,我可以在正确的命名空间中添加一个新的部分类(一个新的.cs文件),这样可以更方便地公开该方法:

namespace MyNamespace {
    partial class MyDataContext
    {
        public int Foo(int? inc, string dataSet)
        {
            return FooPrivate(inc, dataSet).Single().value;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

(命名空间和上下文名称需要与实际数据上下文相同).这会添加一个公共方法,隐藏来自调用者的脏话细节.

不要直接编辑designer.cs文件; 你的改变将会丢失.仅编辑dbml或partial类.


小智 5

将@VALUE作为proc的最后一个语句返回.

然后它将自动检测int,string,bool等类型,并相应地生成包装器方法.