Dapper can't recognize wildcard param in the single quotes

故事扮演 提交于 2019-12-11 05:18:42

问题


I am trying to get indexes fragmentation info from database.

Here the dapper sql query:

var result = await _dbConnection.QueryAsync<IndexFragmentationModel>($@"
        select
        a.index_id as Id, name as Name, avg_fragmentation_in_percent as FragmentationPercent 
        from sys.dm_db_index_physical_stats (DB_ID(N'@dbName'), OBJECT_ID(N'@tableName'), null, null, null) as a  
        join sys.indexes as b on a.object_id = b.object_id and a.index_id = b.index_id;    
        ", new
        {
            dbName = dbName,
            tableName = tableName
        });
        return result.ToList();

Parameters are not passing the the places where they are expected.

Could anybody please suggest - maybe there is another way to pass them ?


回答1:


You're using the literal strings "@dbName" and "@tableName", not the parameters' values.

Remove the N' and ' that surround them.



来源:https://stackoverflow.com/questions/55523500/dapper-cant-recognize-wildcard-param-in-the-single-quotes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!