How to use Char and VarBinary in Dapper?

3.1k Views Asked by At

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

2

There are 2 best solutions below

2
Zohar Peled On BEST ANSWER

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);
0
dcansyn On

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 }