I found this explanation and solution for using Dapper to search a VARCHAR
field using a string
as input:
Query<Thing>("select * from Thing where Name = @Name", new {Name =
new DbString { Value = "abcde", IsFixedLength = true, Length = 10, IsAnsi = true });
Source: Dapper and varchars
But is there a way to adapt this to do the DbString
conversion for every item in a list (using an IN clause)?
The query I am trying to run looks like this:
Query<IndexRec>("SELECT * FROM T_INDEX WHERE CallId IN @callIds",
new { callIds = model.LogEntries.Select(x => x.Id) });
Unfortunately, this query runs slowly because:
model.LogEntries
contains around 300 items. VARCHAR(30)
field.NVarchar
with strings in a WHERE
clause by default.How can I tell Dapper to use ansi strings in my IN
clause for this query?
Parameter. Dynamic parameters are special types of parameters. Dynamic parameter value is recalculated each time you assess the parameter; i.e., this parameter acts as a function.
It's Prevent SQL Injection from external user input by avoiding raw-SQL query string building. Dapper provides methods to build parameterized queries as well as passing sanitized parameters to stored procedures. Dapper has provided multiple methods to Query or execute the store procedures and SQL queries.
You should be able to pass in a list of DbString
items, for example:
var parameters = model.LogEntries
.Select(x => new DbString
{
Value = x.Id,
IsAnsi = true
});
Query<IndexRec>("SELECT * FROM T_INDEX WHERE CallId IN @callIds", new { callIds = parameters })
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With