简体   繁体   中英

Return Multiple Out and Return Image value in C# using Dapper

How to return image from Stored procedure using Dapper in C#?

I have a stored procedure with multiple out's and a return value. I want to read the return type in C#

PFB the Code and Stored procedure. Please suggest me how to resolve this.

CREATE PROCEDURE procedure_Sessions
    @id nvarchar(80),  
    @IsLocked  bit OUTPUT,  
    @LockAge    int OUTPUT

AS  
    DECLARE @textptr AS varbinary(16)  
    DECLARE @length AS int  
    DECLARE @now AS datetime  
    SET @now = GETUTCDATE() 
-- Update Query Begins not complete Query example --

@textptr = CASE IsLocked  
            WHEN 0 THEN TEXTPTR(State)  
            ELSE NULL  
            END
        @length = CASE IsLocked  
            WHEN 0 THEN DATALENGTH(State)  
            ELSE NULL  
            END

-- Update Query Ends--

    IF @length IS NOT NULL BEGIN  
        READTEXT dbo.commons.State @textptr 0 @length  
    END  

    RETURN 0

C# Code:

DynamicParameters dp = new DynamicParameters();
dp.Add("@Id", Id);
dp.Add("@IsLocked", dbType: DbType.Boolean, direction: ParameterDirection.Output);
dp.Add("@LockAge", dbType: DbType.Int32, direction: ParameterDirection.Output);
dp.Add("ReturnValue", dbType: DbType.Object, direction: ParameterDirection.ReturnValue);

connection.Query<byte>("Exclusive", dp, commandType: CommandType.StoredProcedure);

isLocked = dp.Get<bool>("@IsLocked");
lockAge = dp.Get<int>("@LockAge");

var returnvalue = dp.Get<object>("ReturnValue");

您可以定义一个名为“ResultClass”的类并将过程的字段作为其属性包含(您可以使用 byte[] 类型作为图像字段)。然后您可以查询表的所有字段并将结果放入结果类,如下:

var result = db.Query<ResultClass>("SELECT * from procedure_Sessions") ;

https://www.learndapper.com/relationships

   public async Task<List<ResultClass>> Load()
   {
       try
       {
           var query = "SELECT * from procedure_Sessions";
           var result = await Db.QueryMultipleAsync(query).ConfigureAwait(false);
           return result.Read<ResultClass>().ToList();
       }
       catch (Exception exception)
       {
           Console.WriteLine();
           throw;
       }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM