C
C#5mo ago
Alice

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 value
May 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
Yawnder
Yawnder5mo ago
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".
Alice
AliceOP5mo ago
even I used @batchId, the error still happened, I updated the error message in the post
Yawnder
Yawnder5mo ago
Well, convert it to a type supported by your chosen SQL engine flavor.
Alice
AliceOP5mo ago
didn't see any type dapper can use
Yawnder
Yawnder5mo ago
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.
Alice
AliceOP5mo ago
Got it, thank you @Yawnder
Yawnder
Yawnder5mo ago
Great
Want results from more Discord servers?
Add your server