Dapper parameters not working?

dapper

Question

Dapper 1.34 (on earlier Dapper ver like 1.1x this worked fine).

db.Query(@"Select [whatever] from @TableName Where [PREFIX]='@Prefix' order by [something] desc", new { TableName = tableName, Prefix = prefix })

Error: System.Data.SqlClient.SqlException (0x80131904): Must declare the table variable "@TableName".

I get same error trying to define DynamicParameters and passing those.

======= I am currently doing string substitution {%1} .. but that does not seem acceptable ...

Can I please get a sample, also looking at test class for dapper I cant see it running, maybe something wrong with my project setup ?

Accepted Answer

No, that has never worked fine, due to how SQL works:

  • table names cannot be parameterized, and dapper has never offered this; that could work if you pass in a DataTable as a table-valued-parameter, though - which dapper does support - but I don't think this is what you are after
  • '@Prefix' is a string literal; you just mean @Prefix (no single quotes)



Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why