how to use OID parameter in Dapper
Hi guys,
I'm trying to use Dapper to query sql database, but the problem is my batchId is a string, and in the database batchId is an OID.
I have tried to convert from string to Oid, but I got error said dapper doesn't support this type.
The member batchId of type System.Security.Cryptography.Oid cannot be used as a parameter valueMay I know how to handle this scenario? Thanksss!!
public virtual async Task<GetTDataDTO> GetData(string batchId)
{
//batchId example: 0x30c4b63ee3050080
var result = new GetTDataDTO();
Oid batchOid = new Oid(batchId);
var sqlSelect=
@"
SELECT *
FROM data_table fls WITH (NOLOCK)
WHERE batchId = @batchId
";
var parameters = new DynamicParameters();
parameters.Add("batchId", batchOid);
using (var conn = new SqlConnection(ConnectingString))
{
var result = await conn.QueryAsync<GetTDataDTO>(sqlSelect.ToString(), parameters);
}
return result;
}
7 Replies
Normally you just pass in a model that has the properties with names matching the parameters.
So you pass in
new { batchId = batchOid }
as a 2nd parameter.
Well, you can use dynamic parameters like you did, but the name of the parameter would need to be "@batchId"
, not "batchId"
.even I used @batchId, the error still happened, I updated the error message in the post
Well, convert it to a type supported by your chosen SQL engine flavor.
didn't see any type dapper can use
Dapper isn't a SQL engine, it's an ORM; a tool to access a database.
Generally, you want to be using more primitive stuff like int, float, decimal, string, date or even Guids, but not full fledged objects.
Got it, thank you @Yawnder
Great