SqlCommand 类仅强制 SELECT?

发布于 2024-09-18 00:53:03 字数 232 浏览 3 评论 0原文

我正在开发一个用于 SQL 监视的应用程序,它只是一个控制台应用程序,每 5 分钟在一组服务器上运行一次。它有一个带有连接字符串和 SQL 查询的配置文件。问题是我希望程序只接受 SELECT 查询。

我的同事告诉我,他认为我可以在 SqlCommand 类中设置一些东西,但我已经用谷歌搜索了一段时间,但没有成功。有人对我的问题有一个干净的解决方案吗?我曾考虑过在查询中搜索不同的单词,但可能会出错。

欢迎任何建议!

I'm developing a application that's for SQL-surveillance, it's just a console application that will run each 5min on a bunch of servers. It has a config file with connection string and sql query. The thing is I would like the program to accept SELECT queries only.

I colleague told me he thought I could set something in the SqlCommand-class but I've been googling for a while without any success. Anyone got a clean solution on my problem? I've thought about searching the query for different words but it's to much that can go wrong.

Any suggestions are welcome!

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

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

发布评论

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

评论(3

°如果伤别离去 2024-09-25 00:53:03

数据库表本身可以被限制。
您可以撤销所有权限,但可以从应用程序的用户中进行选择。
搜索 SQL-SERVER 的 REVOKE/GRANT 命令。

The database table itself can be restricted.
you can revoke all permission but select from the user of your application.
search for REVOKE/GRANT commands for SQL-SERVER.

很酷不放纵 2024-09-25 00:53:03

除非您的应用程序自己构建整个 SELECT 语句,否则这是不可能的。

问题是,即使您确保第一个单词是 SELECT,也无法阻止恶意用户终止命令,然后发出 EXEC 或 UPDATE 语句。

最好的解决方案是确保连接字符串中引用的用户帐户对 SQL 对象具有有限的权限,因此只有来自支持的对象的 SELECT 才有效。

This isn't going to be possible unless your application builds the entire SELECT statement itself.

The issue is that even if you ensure the first word is SELECT there is nothing stopping a malicuous user terminating the command and then issuing an EXEC or UPDATE statement also.

The best solution is to ensure that the user account referenced in your connection string has limited permissions to the SQL objects and thus only SELECT from supported objects will work.

长不大的小祸害 2024-09-25 00:53:03

授予权限将是更好的方法,但您仍然可以解析该语句,即使它是一系列 SQL 命令,使用如下所示:

bool OkToRun = true;
foreach(string s in TheQueryString.Split(";".ToCharArray(), StringSplitOptions.RemoveEmptyEntries))
    if (!s.Trim().ToUpper().StartsWith("SELECT "))
        OkToRun = false;

Granting privileges would be the better way to go, but you can still parse the statement, even if its a series of SQL commands, using something like this:

bool OkToRun = true;
foreach(string s in TheQueryString.Split(";".ToCharArray(), StringSplitOptions.RemoveEmptyEntries))
    if (!s.Trim().ToUpper().StartsWith("SELECT "))
        OkToRun = false;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文