C# - 在表名上使用 OleDbParameter

发布于 2024-11-19 03:26:11 字数 587 浏览 2 评论 0原文

我想保护我的应用程序免受 SQL 注入。我想在 SQL 查询中使用 OleDbParameter 作为表名 ({1})。

问题是它不起作用(FROM 中的错误或类似的东西)。我可以在{3}中传递OleDbParameter。 示例:

IDbCommand cmd = m_oConnection.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = String.Format("SELECT {0} FROM {1} WHERE {2}={3}",
                "ParentID",
                "?",
                sWhere,
                "?"
);
cmd.Parameters.Add(new OleDbParameter("@sTable", sTable));
cmd.Parameters.Add(new OleDbParameter("@id", id));

我能做什么?我是否被迫编写一个手动转义某些 SQL 字符的函数?如果可以的话哪里可以找到完美的功能呢?

谢谢

I want to protect my app from SQL injection. I want to use OleDbParameter in a SQL query for the table name ({1}).

The problem is that it doesn't work (error in FROM or something like that). I can pass the OleDbParameter in {3} thought.
Example:

IDbCommand cmd = m_oConnection.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = String.Format("SELECT {0} FROM {1} WHERE {2}={3}",
                "ParentID",
                "?",
                sWhere,
                "?"
);
cmd.Parameters.Add(new OleDbParameter("@sTable", sTable));
cmd.Parameters.Add(new OleDbParameter("@id", id));

What can I do? Am I forced to write a function which escapes some SQL characters by hand? If yes, where can I find a perfect function?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

灯角 2024-11-26 03:26:11

所以你知道你不能参数化表名,但你可以这样做,

cmd.CommandText = String.Format("SELECT {0} FROM [{1}] WHERE {2}={3}",
                "ParentID",
                sTable,
                sWhere,
                "?"

但是当且仅当 sTable 来自用户输入时,这才是危险的。如果您直接控制 sTable 则不必担心。

如果它确实来自用户输入,您将需要保护自己。最简单的方法是确保 sTable 是有效的表、附加表或查询名称。

为此,只需执行

 SELECT Name FROM Myssobjects Where Type in (1,5,6,)  

即可获取 sTable 的有效值列表。

根据您的应用程序,您可能可以执行一次并缓存结果,这样您就不必每次调用此方法时都执行此操作。

So you know that you can't parameterize table names but you could do this

cmd.CommandText = String.Format("SELECT {0} FROM [{1}] WHERE {2}={3}",
                "ParentID",
                sTable,
                sWhere,
                "?"

But this is dangerous if and only if sTable comes from user input. If you directly control the sTable you don't have to worry about it.

If it does indeed come from user input you'll need to protect yourself. The easiest way is to make sure that sTable is a valid table, Attached table, or query name

To do that just execute

 SELECT Name FROM Myssobjects Where Type in (1,5,6,)  

to get the list of valid values for sTable.

Depending on your application you could probably execute it once and cache the results so you don't have do it every time you call this method.

漫雪独思 2024-11-26 03:26:11

您将需要使用动态 sql 来生成命令。

但是当您传递表名时,很容易在参数中嵌入 sql 字符串,但这并不好。

看:
http://www.csharp-station.com/Tutorials/AdoDotNet/Lesson06.aspx

'任何放入参数中的内容都将被视为字段数据,而不是 SQL 语句的一部分'

You will need to use dynamic sql to generate your command.

but as you are passing in the table name it is easily possible to embed a sql string in the parameter though, this is not good.

see:
http://www.csharp-station.com/Tutorials/AdoDotNet/Lesson06.aspx

'Anything placed into a parameter will be treated as field data, not part of the SQL statement'

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文