- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想使用 MySqlParameter
将 tableName 传递到查询中(以防止 slq 注入(inject))
MySqlCommand cmd = new MySqlCommand("select * from @table"), cn)
cmd.Parameters.AddWithValue("@table",TableName);
但这行不通。如何将 tableName 作为参数传递
附言我试图将 @
更改为 ?
- 不起作用
最佳答案
您不能将表名作为参数传递。您必须使用动态 SQL 来执行此操作,因此您必须以字符串集中的方式执行此操作,例如
MySqlCommand cmd = new MySqlCommand(String.Format("select * from {0}",tableName), cn)
但是因为用户输入了表名,所以SQL注入(inject)是可能的。您可以使用此 SQL 来确定该表是否存在,然后再从中查询任何内容:
SELECT table_name
FROM information_schema.tables
WHERE table_schema = 'databasename'
AND table_name = 'tablename';
(您可以完美地参数化此查询,因此将消除 SQL 注入(inject))
一般要注意SQL注入(inject)。但是如果你使用这个内部(不暴露给用户),那么 SQL 注入(inject)应该不是问题。
更好的是,你可以构建一个存储过程来处理这个问题,就像我的另一个答案:
关于c# - MySqlParameter 作为表名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6041496/
我试图有一个函数,可以在给定参数时在数据库中搜索项目(在本例中为主板),但如果没有给出参数,我希望能够获取所有主板。 查询是: SELECT * FROM motherboard WHERE slot
您好,我发现了这个在 C# 中向 MySQL 命令添加值的代码片段 MySqlCommand command = connection.CreateCommand(); command.Command
我想使用 MySqlParameter 将 tableName 传递到查询中(以防止 slq 注入(inject)) MySqlCommand cmd = new MySqlCommand("sele
我关注了this试图设置与 MySQL 的连接并运行准备好的语句的线程(在线程中他们谈论的是 Postgresql 的细微差别)。这就是我所做的: IDbConnection cnx = new
我在 MySql 数据库中有一个存储用户帐户的表。其中一列 expires 存储到期日期,但默认为 NULL。我需要能够删除到期日期并将其设置回默认值。 目前,我所有的 CRUD 例程都是使用带参数的
我研究了这个问题,但没有找到切实可行的解决方案。供您引用,我使用我们的通用库类进行连接,从未遇到过这样的问题。而且我想说项目在我的本地 PC 上成功运行,但在服务器上运行失败。 这是我得到错误的代码片
我正在尝试使用新值更新行中的列。新值位于变量中,并且该值中包含新行。此方法接收需要更改的列名称和值作为字典,并创建动态更新命令。 该代码适用于表中的任何其他行,但是当涉及换行符时,SQL 错误显示:您
我的代码出现以下异常 You have an error in your SQL syntax; check the manual that corresponds to your MySQL ser
我创建了一个方法,它将抛出的任何异常插入到 MySql 服务器上“ExceptionLog”的表中。 异常日志表格式 idExceptionLog int (AI)用户(varchar 45)错误消息
我有一个 DAL.EntityFramework 项目,其中安装了 Pomelo.EntityFrameworkCore.MySql 包。我还有一个 DAL.MySQL 包,其中安装了 MySql.D
我的代码会有什么问题?我遇到错误 AddWithValue 不是 MySql.Data.MySqlClient.MySqlParameter 的成员 这是我的代码: Dim bn As Str
例如我有这个查询 MySQL_Query = "UPDATE `owner_info` " _ & "SET isArchived = 1 " _ & "WHERE id=@o
我遇到了一个相当奇怪的问题。 我们有一个 MySql 数据库在我们的 C#/Asp.Net 网络应用程序后面运行。 我们为每个查询使用参数以防止 SQL 注入(inject)攻击。 但是,我们最近从一
我是一名优秀的程序员,十分优秀!