Dapper无法在单引号中识别通配符参数

问题描述 投票:1回答:1

我试图从数据库中获取索引碎片信息。

这里的dapper sql查询:

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();

参数未通过预期的位置。

有人可以建议 - 也许还有另一种方法可以通过它们吗?

c# sql-server dapper
1个回答
3
投票

你使用的是文字字符串"@dbName""@tableName",而不是参数的值。

移除围绕它们的N''

© www.soinside.com 2019 - 2024. All rights reserved.