开发者

How to retrieve the result value and a select statement of a stored procedure at the same time in C#?

OK so this might sound a bit weird!

The thing is that 开发者_JAVA百科in a project there are many stored procedure already written like this:

CREATE PROCEDURE [Status_Insert]

        @StatusId       int OUTPUT,
        @Status         nvarchar (50),
        @IsDeleted      bit = 0

AS

SET NOCOUNT ON;

BEGIN TRY
BEGIN TRAN

INSERT INTO dbo.[Status]
(
        [Status],
        [IsDeleted]
)
VALUES
(
        @Status,
        @IsDeleted
)

SET @StatusId = @@IDENTITY

COMMIT TRAN
RETURN 1
END TRY

BEGIN CATCH
ROLLBACK TRAN

DECLARE @ErrorNumber_INT INT;
DECLARE @ErrorSeverity_INT INT;
DECLARE @ErrorProcedure_VC VARCHAR(200);
DECLARE @ErrorLine_INT INT;
DECLARE @ErrorMessage_NVC NVARCHAR(4000);

SELECT
        @ErrorMessage_NVC = ERROR_MESSAGE(),
        @ErrorSeverity_INT = ERROR_SEVERITY(),
        @ErrorNumber_INT = ERROR_NUMBER(),
        @ErrorProcedure_VC = ERROR_PROCEDURE(),
        @ErrorLine_INT = ERROR_LINE()

RETURN -1
END CATCH

The primary key is an Identity and an output variable is used to retrieve its value after the insert statement. (By the way, is this approach a good practice?)

Now the return values are used to indicate the success or failure of the procedure. (IE. if the operation was successful 1 is returned and -1 is returned if the operation has failed) If there is an error in the execution of the procedure, the last select statement will return the error to the user.

Now how can I call this procedure in C# and get these results at the same time? What I want to do is, execute the procedure, and get the return value, if it was 1 then get the result of the last select statement (which contains more info about the error that has occured) Thanks in advance.


You can use "ReturnValue" parameter in Command.

using(var connection = GetOpenConnection()) {
    using(var cmd = connection.CreateCommand()){
        cmd.CommandText = "Status_Insert";
        cmd.CommandType = CommandType.StoredProcedure;

        var prmReturnValue = new SqlParameter( "@ReturnValue", DBNull.Value );
        prmReturnValue.Direction = ParameterDirection.ReturnValue;

        using(var reader = cmd.ExecuteReader()) {
            // process table result(s)
        }

        var returnValue = prmReturnValue.Value;
    }
}

EDIT: The problem is in select. If you select something into variable, the select will not be in result-sets.

If you want assign variable(s) from functions you don't need SELECT statement, you can use SET command or you can initialize values in DECLARE statement (from version 2008):

/* variable initialization in DECLARE statements >>> */
DECLARE @ErrorNumber_INT INT              = ERROR_NUMBER();
DECLARE @ErrorSeverity_INT INT            = ERROR_SEVERITY();
DECLARE @ErrorProcedure_VC VARCHAR(200)   = ERROR_PROCEDURE();
/* <<< variable initialization in DECLARE statements */

/* variable initialization with SET statements >>> */
DECLARE @ErrorLine_INT INT;
DECLARE @ErrorMessage_NVC NVARCHAR(4000);
SET @ErrorLine_INT INT = ERROR_LINE();
SET @ErrorMessage_NVC  = ERROR_MESSAGE();
/* <<< variable initialization with SET statements */

/* this select will be in result-set(s) >>> */
SELECT
        @ErrorNumber_INT AS [ErrorNumber_INT],
        @ErrorSeverity_INT AS [ErrorSeverity_INT],
        @ErrorProcedure_VC AS [ErrorProcedure_VC],
        @ErrorLine_INT AS [ErrorLine_INT],
        @ErrorMessage_NVC AS [ErrorMessage_NVC]
/* <<< this select will be in result-set(s) */
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜