【问题标题】:Send table name as a parameter in sql c# [duplicate]在sql c#中将表名作为参数发送[重复]
【发布时间】:2021-08-22 22:51:39
【问题描述】:

我能够成功发送 id,但无法尝试对表名进行参数化, 我该如何解决这个问题?还是有另一种方法可以在 c# sql 中将表名作为参数发送

    deleteCommand.CommandText = "delete from @tableName where rowid=@ID";
    deleteCommand.Parameters.Add(new SqlParameter("@tableName", table.Name));
    deleteCommand.Parameters.Add(new SqlParameter("@ID", table.id));

【问题讨论】:

标签: c# asp.net .net asp.net-mvc c#-6.0


【解决方案1】:

为什么不使用字符串插值代替:

deleteCommand.CommandText = $"delete from {table.name} where rowid=@ID"; 

【讨论】:

  • 取决于 table.Name 的来源。如果它来自不安全的来源,他们可能会将 SQL 注入到表名中,从而把事情搞得一团糟。
  • 这并不理想,因为 DML 不允许参数化表名或列名,但如果您知道 SQL 注入,它就可以正常工作。
猜你喜欢
  • 2014-08-13
  • 2015-04-13
  • 2021-02-18
  • 1970-01-01
  • 2014-07-01
  • 2012-09-28
  • 1970-01-01
  • 2013-07-24
  • 2011-05-20
相关资源
最近更新 更多