如何在我的应用程序中开发自定义查询生成器?

发布于 2024-11-29 08:46:21 字数 87 浏览 1 评论 0原文

我想在我的 vb.net 应用程序中开发选择查询生成器。如果用户在富文本框中写入“选择查询”,则将执行查询并将结果显示在网格视图中。任何人为此发送示例应用程序。

I want to develop select query builder in my vb.net application. If user write the "select query" in a richtext box that query will be executed and the result will be displayed in a grid view. Any one send the Sample Application for this.

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

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

发布评论

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

评论(1

拔了角的鹿 2024-12-06 08:46:21

要回答您的问题,请查看 codeproject.com 上的这个答案

SqlConnection con = new SqlConnection("Your Connection String Here");
            con.Open();
            SqlCommand cmdQuery = new SqlCommand("Your Query Here", con);
            SqlDataReader drQuery = cmdQuery.ExecuteReader();
            DataSet dsQuery = new DataSet();
            DataTable dtQuery = new DataTable();
            dsQuery.Tables.Add(dtQuery);
            dsQuery.Load(drQuery, LoadOption.PreserveChanges, dsQuery.Tables[0]);
            DataGridView1.DataSource = dsQuery.Tables[0];

建议:永远不要让您的用户直接输入 SQL。您应该尽一切努力避免这种情况,因为您失去了对安全性和性能的控制。

如果用户运行“DELETE FROM dbo.Orders”或消耗所有可用内存的非常复杂的查询怎么办?

如果是针对 DBA 或开发人员,他们应该能够使用 SQL Server Management Studio 等开发工具。

更新:
验证 SQL 的工作示例:

string sql = "SELECT * FROM Table1 AS t1 INNER JOIN Table2 AS t2 ON t1.ID = t2.ParentID";
if (System.Text.RegularExpressions.Regex.IsMatch(sql, "^SELECT([^;])$",System.Text.RegularExpressions.RegexOptions.IgnoreCase))
{
// do something
}
else
{
throw new InvalidOperationException("You're not allowed to execute nothing but SELECT-queries.");
}

To answer your question, look at this answer on codeproject.com:

SqlConnection con = new SqlConnection("Your Connection String Here");
            con.Open();
            SqlCommand cmdQuery = new SqlCommand("Your Query Here", con);
            SqlDataReader drQuery = cmdQuery.ExecuteReader();
            DataSet dsQuery = new DataSet();
            DataTable dtQuery = new DataTable();
            dsQuery.Tables.Add(dtQuery);
            dsQuery.Load(drQuery, LoadOption.PreserveChanges, dsQuery.Tables[0]);
            DataGridView1.DataSource = dsQuery.Tables[0];

A recommendation: Never let your users enter SQL directly. You should do everything you can to avoid this since you loose control of both security and performance.

What if a user runs a "DELETE FROM dbo.Orders" or a very complex query consuming all available memory?

If it's for DBAs or developers they should be capable of using development tools like SQL Server Management Studio or such.

UPDATE:
Working example for validating SQL:

string sql = "SELECT * FROM Table1 AS t1 INNER JOIN Table2 AS t2 ON t1.ID = t2.ParentID";
if (System.Text.RegularExpressions.Regex.IsMatch(sql, "^SELECT([^;])$",System.Text.RegularExpressions.RegexOptions.IgnoreCase))
{
// do something
}
else
{
throw new InvalidOperationException("You're not allowed to execute nothing but SELECT-queries.");
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文