这是我的代码部分:
CResponseVO objCResponseVO = new CResponseVO();
try
{
var parameters = new DynamicParameters();
parameters.Add("@UserId", currentUser.userId, DbType.Int32);
parameters.Add("@Operation", operation, DbType.String);
parameters.Add("@Output", dbType: DbType.Int32, direction: ParameterDirection.Output);
using (var connection = SqlAccessHelper.SqlHelper.GetOpenConnection(SqlConnectionHelper.SqlConnectionString()))
{
var reader = connection.QueryMultiple("USP_DataExtract", parameters, (SqlTransaction)null, 1000000, CommandType.StoredProcedure);
int result = parameters.Get<int>("@Output");
if (operation != "insert")
{
ObservableCollection<DataExtraction.DataExtractionTracker> DataExtractionTracker = new ObservableCollection<DataExtraction.DataExtractionTracker>(reader.Read<DataExtraction.DataExtractionTracker>());
objCResponseVO.addObject("ExtractionStatus", DataExtractionTracker);
}
objResponseVO.Result = result;
}
这是我的 SP,我使用try
并catch
根据输出参数值提交或回滚:
@UserID int=0,
@Operation varchar(50)= NULL,
@Output INT OUTPUT
AS
BEGIN
BEGIN TRY
BEGIN TRANSACTION
If(@Operation = 'select')
BEGIN
SELECT RequestId, UserId, RequestTime, Status,DownloadPath from DataExtractTracker where UserId= @UserID
END
If(@Operation = 'insert')
BEGIN
Insert into DataExtractTracker( UserId, RequestTime, Status) values (@UserID, GETDATE(), 'Waiting')
END
SET @Output = 0
COMMIT TRANSACTION
END TRY
BEGIN CATCH
ROLLBACK TRANSACTION
SET @Output = 1
DECLARE @ErrorMessage NVARCHAR(4000);
DECLARE @ErrorSeverity INT;
DECLARE @ErrorState INT;
SELECT @ErrorMessage = ERROR_MESSAGE(), @ErrorSeverity = ERROR_SEVERITY(), @ErrorState = ERROR_STATE();
-- Use RAISERROR inside the CATCH block to return
-- error information about the original error that
-- caused execution to jump to the CATCH block.
RAISERROR (@ErrorMessage, -- Message text.
@ErrorSeverity, -- Severity.
@ErrorState -- State.);
END CATCH
END
但是,我无法检索输出参数值。执行时出现异常:
int result = parameters.Get<int>("@Output");
异常是这样说的:
你调用的对象是空的。