我的代码如下:
int retval = databaseContext.Database.ExecuteSqlCommand(command, sqlParameters.ToArray());
其中databaseContext的类型为System.Data.Entity.DbContext
我想使用返回值来了解存储过程是否成功运行。基于the documentation ExecuteSqlCommand
应该返回存储过程的结果。
但是,无论我设置存储过程做什么,该命令总是返回-1到retval。我已经尝试返回各种整数甚至在存储过程中调用RAISERROR,但返回值始终为-1。
这是我尝试的两个存储过程,但它们都返回-1并且没有给出任何成功运行的指示:
CREATE PROCEDURE [dbo].[myproc]
AS
BEGIN
RAISERROR ('You fail', -- Message text.
0, -- Severity - operation failed.
500-- State.
);
END
GO
CREATE PROCEDURE [dbo].[myproc]
AS
BEGIN
RETURN 1
END
GO
知道我在这里做错了吗?
答案 0 :(得分:5)
基于DavidG的评论:
ExecuteSqlCommand只返回值,如果它是select语句的结果。
如果您想要存储过程的返回值(如问题中所示),则必须使用this SO answer中所述的EXEC @ReturnValue = [StoredProc] @param1 ...
方法。
答案 1 :(得分:0)
存储库
public async Task<int> ExecuteStoredProcedure(string sqlQuery,int user_id, T entity, params object[] parameters)
{
const int ENTITY_ID_INDEX = 19;
var result= _context.Database.ExecuteSqlCommand(sqlQuery, parameters);
var history = _context.History.AddAsync(new History { CreatedDate = DateTime.Now, EventId = Events.CREATE_EVENT_ID, ObjectId =Convert.ToInt32(((SqlParameter) parameters[ENTITY_ID_INDEX]).Value), ObjectStr = JsonConvert.SerializeObject(entity), ObjectName = entity.GetType().Name, UserId = user_id });
_context.SaveChanges();
return result;
}
存储过程
ALTER PROCEDURE [dbo].[spInboxDocumentAdd]
@DocumentTypeId int,
@DocumentKindId int,
@DocumentKindCode nvarchar(50),
@RegistrationDate datetime,
@ApplicantId int,
@ApplicantAddress nvarchar(255),
@ApplicantFullName nvarchar(255),
@DocumentDate datetime,
@DocumentNumber nvarchar(255),
@IsForInformation bit,
@IsForControl bit,
@DeadlineDate datetime,
@ReminderDate datetime,
@SummaryId int,
@UserId int,
@Note nvarchar(255),
@Mail nvarchar(255),
@Account nvarchar(10),
@RegistrationNumber nvarchar(50) Output,
@CurrentId int
AS
Declare @OrderNumber int,@CreatedDate datetime, @RegistrationNumberIn nvarchar(50), @Deleted bit
SET @CreatedDate=SYSDATETIME();
SET @OrderNumber=ISNULL((SELECT MAX(OrderNumber)+1 FROM dbo.InboxDocuments WHERE DocumentKindId=@DocumentKindId AND YEAR(CreatedDate)=YEAR(@CreatedDate)),0);
IF (@OrderNumber IS NULL)
BEGIN
SET @OrderNumber=0;
END
SET @RegistrationNumberIn=CONVERT(nvarchar(50),@DocumentKindCode+'/'+CONVERT(nvarchar(50),@OrderNumber)+'-'+CONVERT(nvarchar(50),YEAR(@CreatedDate)));
SET @Deleted=0;
INSERT INTO [dbo].[InboxDocuments]
([DocumentTypeId]
,[DocumentKindId]
,[OrderNumber]
,[RegistrationDate]
,[RegistrationNumber]
,[CreatedDate]
,[ApplicantId]
,[ApplicantAddress]
,[ApplicantFullName]
,[DocumentDate]
,[DocumentNumber]
,[IsForInformation]
,[IsForControl]
,[DeadlineDate]
,[ReminderDate]
,[SummaryId]
,[Deleted]
,[UserId]
,[Note]
,[Mail]
,[Account])
VALUES
(
@DocumentTypeId,
@DocumentKindId,
@OrderNumber,
@RegistrationDate,
@RegistrationNumberIn,
@CreatedDate,
@ApplicantId,
@ApplicantAddress,
@ApplicantFullName,
@DocumentDate,
@DocumentNumber,
@IsForInformation,
@IsForControl,
@DeadlineDate,
@ReminderDate,
@SummaryId,
@Deleted,
@UserId,
@Note,
@Mail,
@Account
);
SET @CurrentId= @@IDENTITY;
SET @RegistrationNumber=@RegistrationNumberIn;
SELECT @RegistrationNumber;
RETURN @CurrentId;
控制器
private string CreateInboxDocument(InboxDocuments inboxDocument, string documentKindCode, int user_id)
{
const int REGISTRATION_DATE_INDEX = 18;
const int ENTITY_ID = 19;
SqlParameter DocumentTypeId = new SqlParameter();
DocumentTypeId.ParameterName = "@DocumentTypeId";
DocumentTypeId.SqlDbType = SqlDbType.Int;
DocumentTypeId.Direction = ParameterDirection.Input;
DocumentTypeId.Value = inboxDocument.DocumentTypeId;
SqlParameter DocumentKindId = new SqlParameter();
DocumentKindId.ParameterName = "@DocumentKindId";
DocumentKindId.SqlDbType = SqlDbType.Int;
DocumentKindId.Direction = ParameterDirection.Input;
DocumentKindId.Value = inboxDocument.DocumentKindId;
SqlParameter DocumentKindCode = new SqlParameter();
DocumentKindCode.ParameterName = "@DocumentKindCode";
DocumentKindCode.SqlDbType = SqlDbType.VarChar;
DocumentKindCode.Size = 50;
DocumentKindCode.Direction = ParameterDirection.Input;
DocumentKindCode.Value = documentKindCode;
SqlParameter RegistrationDate = new SqlParameter();
RegistrationDate.ParameterName = "@RegistrationDate";
RegistrationDate.SqlDbType = SqlDbType.DateTime;
RegistrationDate.Direction = ParameterDirection.Input;
RegistrationDate.Value = inboxDocument.RegistrationDate;
SqlParameter ApplicantId = new SqlParameter();
ApplicantId.ParameterName = "@ApplicantId";
ApplicantId.SqlDbType = SqlDbType.Int;
ApplicantId.Direction = ParameterDirection.Input;
ApplicantId.Value = inboxDocument.ApplicantId;
SqlParameter ApplicantAddress = new SqlParameter();
ApplicantAddress.ParameterName = "@ApplicantAddress";
ApplicantAddress.SqlDbType = SqlDbType.VarChar;
ApplicantAddress.Size = 50;
ApplicantAddress.Direction = ParameterDirection.Input;
ApplicantAddress.Value = documentKindCode;
SqlParameter ApplicantFullName = new SqlParameter();
ApplicantFullName.ParameterName = "@ApplicantFullName";
ApplicantFullName.SqlDbType = SqlDbType.VarChar;
ApplicantFullName.Size = 50;
ApplicantFullName.Direction = ParameterDirection.Input;
ApplicantFullName.Value = documentKindCode;
SqlParameter DocumentDate = new SqlParameter();
DocumentDate.ParameterName = "@DocumentDate";
DocumentDate.SqlDbType = SqlDbType.DateTime;
DocumentDate.Direction = ParameterDirection.Input;
DocumentDate.Value = inboxDocument.RegistrationDate;
SqlParameter IsForInformation = new SqlParameter();
IsForInformation.ParameterName = "@IsForInformation";
IsForInformation.SqlDbType = SqlDbType.Bit;
IsForInformation.Direction = ParameterDirection.Input;
IsForInformation.Value = inboxDocument.IsForInformation;
SqlParameter IsForControl = new SqlParameter();
IsForControl.ParameterName = "@IsForControl";
IsForControl.SqlDbType = SqlDbType.Bit;
IsForControl.Direction = ParameterDirection.Input;
IsForControl.Value = inboxDocument.IsForControl;
SqlParameter DeadlineDate = new SqlParameter();
DeadlineDate.ParameterName = "@DeadlineDate";
DeadlineDate.SqlDbType = SqlDbType.DateTime;
DeadlineDate.Direction = ParameterDirection.Input;
DeadlineDate.Value = inboxDocument.DeadlineDate;
SqlParameter ReminderDate = new SqlParameter();
ReminderDate.ParameterName = "@ReminderDate";
ReminderDate.SqlDbType = SqlDbType.DateTime;
ReminderDate.Direction = ParameterDirection.Input;
ReminderDate.Value = inboxDocument.RegistrationDate;
SqlParameter UserId = new SqlParameter();
UserId.ParameterName = "@UserId";
UserId.SqlDbType = SqlDbType.Int;
UserId.Direction = ParameterDirection.Input;
UserId.Value = inboxDocument.UserId;
SqlParameter Note = new SqlParameter();
Note.ParameterName = "@Note";
Note.SqlDbType = SqlDbType.VarChar;
Note.Size = 255;
Note.Direction = ParameterDirection.Input;
Note.Value = inboxDocument.Note;
SqlParameter Mail = new SqlParameter();
Mail.ParameterName = "@Mail";
Mail.SqlDbType = SqlDbType.VarChar;
Mail.Size = 255;
Mail.Direction = ParameterDirection.Input;
Mail.Value = inboxDocument.Mail;
SqlParameter Account = new SqlParameter();
Account.ParameterName = "@Account";
Account.SqlDbType = SqlDbType.VarChar;
Account.Size = 255;
Account.Direction = ParameterDirection.Input;
Account.Value = inboxDocument.Account;
SqlParameter CurrentId = new SqlParameter();
CurrentId.ParameterName = "@CurrentId";
CurrentId.SqlDbType = SqlDbType.Int;
CurrentId.Direction = ParameterDirection.Output;
CurrentId.Value = 0;
SqlParameter DocumentNumber = new SqlParameter();
DocumentNumber.ParameterName = "@DocumentNumber";
DocumentNumber.SqlDbType = SqlDbType.VarChar;
DocumentNumber.Size = 50;
DocumentNumber.Direction = ParameterDirection.Output;
DocumentNumber.Value = inboxDocument.DocumentNumber;
SqlParameter RegistrationNumber = new SqlParameter();
RegistrationNumber.ParameterName = "@RegistrationNumber";
RegistrationNumber.SqlDbType = SqlDbType.VarChar;
RegistrationNumber.Size = 50;
RegistrationNumber.Direction = ParameterDirection.Output;
RegistrationNumber.Value = "";
SqlParameter SummaryId = new SqlParameter();
SummaryId.ParameterName = "@SummaryId";
SummaryId.SqlDbType = SqlDbType.Int;
SummaryId.Direction = ParameterDirection.Input;
SummaryId.Value = inboxDocument.SummaryId;
using (var unitOfWork = new UnitOfWork(new DbContext.ApplicationDbContext(workflow_optionsBuilder.Options)))
{
SqlParameter[] @params = new SqlParameter[] { DocumentTypeId, DocumentKindId, DocumentKindCode, RegistrationDate, ApplicantId, ApplicantAddress, ApplicantFullName, DocumentDate, DocumentNumber, IsForInformation, IsForControl, DeadlineDate, ReminderDate, SummaryId, UserId, Note, Mail, Account, RegistrationNumber, CurrentId };
var row_affected = unitOfWork.InboxDocuments.ExecuteStoredProcedure("exec @CurrentId=spInboxDocumentAdd @DocumentTypeId, @DocumentKindId, @DocumentKindCode, @RegistrationDate, @ApplicantId, @ApplicantAddress, @ApplicantFullName, @DocumentDate, @DocumentNumber, @IsForInformation, @IsForControl, @DeadlineDate, @ReminderDate,@SummaryId, @UserId, @Note, @Mail, @Account,@RegistrationNumber OUTPUT, @CurrentId", user_id, inboxDocument, @params);
CurrentDocumentId = Convert.ToInt32(@params[ENTITY_ID].Value);
return @params[REGISTRATION_DATE_INDEX].Value.ToString();
}
}