【问题标题】:How to call stored procedure which returns data using Entity Framework and .Net Core 1.1如何使用 Entity Framework 和 .Net Core 1.1 调用返回数据的存储过程
【发布时间】:2018-02-08 12:50:31
【问题描述】:

我正在尝试调用一个返回值的存储过程。但由于某种原因,它没有返回值,而是抛出错误:

我这样调用存储过程:

var arTransactionid = context.Set<ARTransaction>()
                             .FromSql("core.ARTransaction_Insert @PersonID = {0},@ContractID = {1},@TransactionCodeID = {2},@TransactionDate = {3}," +
        "@TransactionDesc = {4},@Amount = {5},@CurrencyID = {6},@ExchangeRate = {7}," +
        "@BaseAmount = {8},@PostedDate = {9},@DueDate = {10},@Reference = {11}," +
        "@Reference2 = {12},@Reversal = {13},@BaseAdjustment = {14},@BatchID = {15}," +
        "@ParentTransactionID = {16},@InvoiceID = {17},@UserID = {18}", 46736, null, 197, "2017-08-25 00:00:00 -05:00", null, 501.0000, 2, 1, 501.0000, null, null, null, null, 0, 0, null, 0, null, 3052)
                             .FirstOrDefault();

这是我的模型类,应该填充存储过程的返回值:

public class ARTransaction
{
    public String arTransactionID { set; get; }
}

这是存储过程:

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER Procedure [core].[ARtransaction_Insert]
    @PersonID int,
    @ContractID int,
    @TransactionCodeID int,
    @TransactionDate date,
    @TransactionDesc nvarchar(100),
    @Amount money,
    @CurrencyID int,
    @ExchangeRate decimal(18,6),
    @BaseAmount money,
    @PostedDate DATE = null,
    @DueDate date,
    @Reference nvarchar(50),
    @Reference2 nvarchar(50),
    @Reversal BIT = 0,
    @BaseAdjustment BIT = 0,
    @BatchID int,
    @ParentTransactionID int,
    @InvoiceID INT = null,
    @UserID INT,
    @SessionGUID UNIQUEIDENTIFIER = null
AS
    SET NOCOUNT ON

    DECLARE @ARtransactionID INT,
            @Valid INT,
            @ValidMessage NVARCHAR(100)

    --More code goes here ***********

    RETURN @ARtransactionID

如果我在 SQL Server Management Studio 中调用存储过程,它不会返回值 - 只是一条消息。

为了得到一个结果,我声明了一个变量并赋值给存储过程。

DECLARE @arTransctionInserted int;

EXEC @arTransctionInserted = core.ARTransaction_Insert 
          @PersonID = 46736,
          @ContractID = NULL,
          @TransactionCodeID = 197,
          @TransactionDate = '2017-08-25 00:00:00 -05:00',
          @TransactionDesc = NULL,
          @Amount = 500.0000,
          @CurrencyID = 2,
          @ExchangeRate = 1, 
          @BaseAmount = 500.0000,
          @PostedDate = NULL,
          @DueDate = NULL,
          @Reference = NULL,
          @Reference2 = NULL,
          @Reversal = 0,
          @BaseAdjustment = 0,
          @BatchID = NULL,
          @ParentTransactionID = 0,
          @InvoiceID = NULL,
          @UserID = 3052;

SELECT @arTransctionInserted as IDTransaction;

如何调用存储过程返回值?

context.Set<ARTransaction>().FromSql("")

我已经尝试调用它,包括所有查询,但错误不断抛出。

var arTransactionid = context.Set<ARTransaction>().FromSql("DECLARE @arTransctionInserted int; exec @arTransctionInserted = core.ARTransaction_Insert @PersonID = {0},@ContractID = {1},@TransactionCodeID = {2},@TransactionDate = {3}," +
               "@TransactionDesc = {4},@Amount = {5},@CurrencyID = {6},@ExchangeRate = {7}," +
               "@BaseAmount = {8},@PostedDate = {9},@DueDate = {10},@Reference = {11}," +
               "@Reference2 = {12},@Reversal = {13},@BaseAdjustment = {14},@BatchID = {15}," +
               "@ParentTransactionID = {16},@InvoiceID = {17},@UserID = {18};SELECT @arTransctionInserted as IDTransaction;", 46736, null, 197, "2017-08-25 00:00:00 -05:00", null, 501.0000, 2, 1, 501.0000, null, null, null, null, 0, 0, null, 0, null, 3052).FirstOrDefault();

【问题讨论】:

  • 你能把你的select语句贴在sproc里面吗?
  • 你指的是SP的缺失部分吗?
  • 是的,检查一下会很有帮助。
  • 唯一缺少的部分是我为 **code ARtransactionID Exec ARtransactionID = core.ARtransaction_Insert ** 赋值的地方

标签: c# sql-server entity-framework stored-procedures asp.net-core-1.1


【解决方案1】:

RETURN 不会输出 SP 值,只会返回要放在调用数据库对象的变量上的数据。这就是为什么EXECUTE 可以工作,但运行 SP 本身只会返回“命令已成功完成”。

使用SELECT @ARtransactionID;,因为这应该会输出您要查找的 ID。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-05-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-25
    • 2021-11-08
    相关资源
    最近更新 更多