如何过滤数据库? [英] How to filter a database?

查看:75
本文介绍了如何过滤数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用名为Submitted的列中的False来过滤我的数据库。我有创建和显示数据库的代码。我试图通过虚假数据过滤它。所以只是尝试显示该列中包含False的数据。我只是无法获得正确的过滤数据库的代码。这是我第一次尝试过滤方法。我通常会在ASP的Gridview中做到这一点。我有人可以帮忙吗?



I am trying to filter my database by using False that is in a Column named Submitted. I have the code for creating and displaying the database. I am trying to just filter it by False data. So just trying to display the data that has False in that column. I just can't get the code right for filtering the database. This is my first time trying the method on filtering. I would usually do this by the Gridview in ASP. I Can someone please help?

SqlCommand com2 = new SqlCommand("Select USER_ID, LongName, SUBMITTED, YEAR, DATE from Table12 where SUBMITTED = 'False' and YEAR = '" + TextBoxYEAR.Text + "'", con2);

DataTable dt2 = new DataTable();
            SqlDataReader sqlDataReader2 = com2.ExecuteReader();


            dt2.Load(sqlDataReader2);
            sqlDataReader2.Close();

            GridView dataGridView3 = new GridView();
            GridView3.DataSource = dt2;
            GridView3.DataBind();
            lblCount3.Text = GridView3.Rows.Count.ToString();





我的尝试:





What I have tried:

FilterExpression filter1 = new FilterExpression();

推荐答案

不是那样的!永远不要连接字符串来构建SQL命令。它让您对意外或故意的SQL注入攻击持开放态度,这可能会破坏您的整个数据库。改为使用参数化查询。



连接字符串时会导致问题,因为SQL会收到如下命令:

Not like that! Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.

When you concatenate strings, you cause problems because SQL receives commands like:
SELECT * FROM MyTable WHERE StreetAddress = 'Baker's Wood'

就SQL而言,用户添加的引号会终止字符串,并且您会遇到问题。但情况可能更糟。如果我来并改为输入:x'; DROP TABLE MyTable; - 然后SQL收到一个非常不同的命令:

The quote the user added terminates the string as far as SQL is concerned and you get problems. But it could be worse. If I come along and type this instead: "x';DROP TABLE MyTable;--" Then SQL receives a very different command:

SELECT * FROM MyTable WHERE StreetAddress = 'x';DROP TABLE MyTable;--'

哪个SQL看作三个单独的命令:

Which SQL sees as three separate commands:

SELECT * FROM MyTable WHERE StreetAddress = 'x';

完全有效的SELECT

A perfectly valid SELECT

DROP TABLE MyTable;

完全有效的删除表格通讯和

A perfectly valid "delete the table" command

--'

其他一切都是评论。

所以它确实:选择任何匹配的行,从数据库中删除表,并忽略其他任何内容。



所以总是使用参数化查询!或者准备好经常从备份中恢复数据库。你定期做备份,不是吗?



然后删除WHERE条件,看看你回来的确切行数。

如果它看起来合理,请添加WHERE的一半,然后重试。如果那样做,那就加上另一半,看看你得到了什么。

请记住AND要求两个条件都为真才能匹配一行。



我们不能为你做任何事情:我们无法访问您的数据!

And everything else is a comment.
So it does: selects any matching rows, deletes the table from the DB, and ignores anything else.

So ALWAYS use parameterized queries! Or be prepared to restore your DB from backup frequently. You do take backups regularly, don't you?

Then remove the WHERE condition, and see exactly what rows you get back.
If it looks reasonable, add back one half of the WHERE and try again. If that work, add back the other half instead and see what you get.
Remember that AND requires both conditions to be true in order to match a row.

We can't do any of that for you: we have no access to your data!


这篇关于如何过滤数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆