Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
189 views
in Technique[技术] by (71.8m points)

sql - 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");
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can Define a class named 'ResultClass' and include the fields of the procedures as its properties(you can use byte[] type for the image field).then you can query all fields of the table and put the result into an instance of the resultClass,as follow:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...