Vivek Nuna
Vivek Nuna

Reputation: 1

How to use Char and VarBinary in Dapper?

I have to call Stored Procedure in which parameters are char and VarBinary(MAX). I need to call this stored procedure from C# code using Dapper. But I am not able to find any parameter supported by dapper.

SP:

ALTER PROCEDURE [dbo].[uspTest]      
 , @Param1   CHAR(1)        
 , @Param2   VARBINARY(MAX)=null

C#:

DynamicParameters parameter = new DynamicParameters();
parameter.Add("@Param1", email.SenderType, DbType.char, ParameterDirection.Input);
parameter.Add("@Param2", email.AttachedFileStream, DbType.varbinary, ParameterDirection.Input);

Compilation Error:

DBType does not contain definition for char and varbinary

Upvotes: 3

Views: 3759

Answers (2)

dcansyn
dcansyn

Reputation: 106

You can use this;

db.Execute("Sql Query", new Dapper.DynamicParameters(new Dictionary<string, object>() { { "ColumnName", "ValueObject" } }));

You must use variable type on Enum, like this;

public enum TestEnum : byte { // Values }

Upvotes: 0

Zohar Peled
Zohar Peled

Reputation: 82474

This problem doesn't have anything to do with Dapper, you're attempting to access enum members that simply don't exist.

The DbType enum does not contain members named char or varbinary.

Instead of char use AnsiStringFixedLength and instead of varbinary use Binary.
Also, at least in the first parameter, you should also include the size:

DynamicParameters parameter = new DynamicParameters();
parameter.Add("@Param1", email.SenderType, DbType.AnsiStringFixedLength, ParameterDirection.Input, 1);
parameter.Add("@Param2", email.AttachedFileStream, DbType.Binary, ParameterDirection.Input);

Upvotes: 5

Related Questions