这似乎是一种奇怪的行为,但是以下代码引发了错误:
public async Task RebuildIndex(string tableName, string indexName)
{
await _dbConnection.ExecuteAsync($@"
alter index @indexName on @tableName rebuild;",
new
{
indexName = indexName,
tableName = tableName
});
}
错误消息:
System.Data.SqlClient.SqlException (0x80131904): Неправильный синтаксис около конструкции "@indexName".
at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)
at System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady)
at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString)
at System.Data.SqlClient.SqlCommand.CompleteAsyncExecuteReader()
at System.Data.SqlClient.SqlCommand.EndExecuteNonQueryInternal(IAsyncResult asyncResult)
at System.Data.SqlClient.SqlCommand.EndExecuteNonQuery(IAsyncResult asyncResult)
at System.Threading.Tasks.TaskFactory`1.FromAsyncCoreLogic(IAsyncResult iar, Func`2 endFunction, Action`1 endAction, Task`1 promise, Boolean requiresSynchronization)
--- End of stack trace from previous location where exception was thrown ---
at Dapper.SqlMapper.ExecuteImplAsync(IDbConnection cnn, CommandDefinition command, Object param) in C:\projects\dapper\Dapper\SqlMapper.Async.cs:line 678
at Railways.DbManager.Repositories.DbmIndexDefragmentationRepository.RebuildIndex(String tableName, String indexName) in C:\Railways\asu-itk-core\Railways\Railways.DbManager.Repositories\DbmIndexDefragmentationRepository.cs:line 48
at Railways.DBManager.Services.IndexDefragmentationService.DefragIndexes() in C:\Railways\asu-itk-core\Railways\Railways.DBManager.Services\IndexDefragmentationService.cs:line 41
ClientConnectionId:7db4e7f6-e01d-4671-9fe3-e22dcb388cd4
Error Number:102,State:1,Class:15
这里有什么问题?
我只是传递参数,这应该可以工作。
也许我缺少了什么?有人可以提出解决方案吗?
答案 0 :(得分:3)
您将无法参数化SQL语句中使用的目标对象名称,即无法参数化表名称和索引名称。这不仅是Dapper的限制-例如ADO.Net和sp_executesql
同样无法参数化表。 (推测性地,参数化不仅可以防止SQL注入攻击,而且可以基于精确的数据类型改善执行计划的缓存-如果目标表是动态的,则计划缓存显然是不可能的。)
在您的情况下,您将需要进行自己的SQL注入检查(如果需要-索引名和表名来自不受信任的用户非常不寻常),然后将其替换为SQL语句,例如通过string interpolation或String.Format
。
如果您真的接受来自不受信任用户的表名或索引名,并且需要防范SQL注入攻击,则可以使用Aaron Betrand's advice并首先检查{{ 1}}和sys.tables
中的索引,然后再执行SQL语句。
修改
您可以在针对sys.indexes
表(如system
和sys.tables
(如DMVs
)的查询中参数化表名的原因是因为这里是表名或其他object是列中的值。但是类似地,您将无法参数化系统表的名称或DMV本身。