使用StoredProcedure的Linq to SQL-如何返回值(插入,更新,删除) [英] Linq to SQL using StoredProcedure - How to return a value (insert, update, delete)

查看:40
本文介绍了使用StoredProcedure的Linq to SQL-如何返回值(插入,更新,删除)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们如何从存储过程中返回一个值

How do we return a value from a stored procedure

这是我的sp样子:

create proc dbo.spInsertGroup
@ID uniqueidentifier
@GroupName varchar(100),
@IsActive bit
AS
BEGIN



insert into tblGroup
values(@ID, @GroupName, @IsActive)

Select @@IDENTITY AS ID

END

基于返回值,我想向用户显示某种保存成功或失败的反馈.

based on the return value i want to show the user some kind of feedback wether the save was successfull or failed.

public void AddInquiry(Inquiry inq)
{
   using (var transaction = new TransactionScope())
   {
      using (MyDataContext dc = conn.GetContext())
      {
         var results =  dc.spInquiry_Insert(......).ToList();

         transaction.Complete();

         var returnValue = (int)results.ReturnValue; 
         // note that ReturnValue is of type object and must be cast. 
      }
   }
}

错误:

错误39'System.Collections.Generic.List'不包含针对的定义'ReturnValue',没有扩展方法"ReturnValue"接受第一个类型参数'System.Collections.Generic.List'可以找到(您是否想念一个使用指令或程序集参考?)

Error 39 'System.Collections.Generic.List' does not contain a definition for 'ReturnValue' and no extension method 'ReturnValue' accepting a first argument of type 'System.Collections.Generic.List' could be found (are you missing a using directive or an assembly reference?)

推荐答案

尝试:

using (MyDataContext dc = conn.GetContext())
{
    var returnValue = (int)dc.spInsertGroup(.......).ReturnValue;
}

由于调用.ToList()而导致该错误,因此var结果的类型为List<>,没有ReturnValue属性.

You are getting that error because you are calling the .ToList() so the var results is of type List<> that has no ReturnValue property.

如果看不到ReturnValue属性,也许您需要更新LINQ生成的方法

If you can't see the ReturnValue property, maybe you need to update the method generated by LINQ

这篇关于使用StoredProcedure的Linq to SQL-如何返回值(插入,更新,删除)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆