Search code examples
c#sql-serverdapper

Dapper can't recognize wildcard param in the single quotes


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 ?


Solution

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

    Remove the N' and ' that surround them.