使用2个日期时间选择器访问数据库进行Winform日期搜索 [英] Winform date search with 2 date time pickers access database

查看:67
本文介绍了使用2个日期时间选择器访问数据库进行Winform日期搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个日期时间选择器的Winform,我在Access数据库中搜索日期范围。



一直运行良好,直到今天。当我从9/24/18到10/1/18搜索时,我没有得到任何结果。



我猜的原因是数字顺序数据库。



示例:日期根据构成值的各个数字排序,而不是根据数值排序。例如,值10/1/18出现在9/24/18之前。 Date的My Access数据库设置为Date。这是代码



谢谢



I have Winform with 2 date time pickers that I search a date range in a Access database.

All was working well until today. When I search from 9/24/18 to 10/1/18 I don’t get any results.

The reason I’m guessing is the numeric order of the database.

Example : Dates are sorted based on the individual digits that make up the value, instead of on the numeric value. For example, the value 10/1/18 appears before 9/24/18. My Access database for the Date is set to Date. This is the Code

Thanks

private void btn_Range_Search_Click_1(object sender, EventArgs e)
        {
            try
            {
                string queryString = "SELECT HotSheetID, Today, Part, Timeord, Timerec, sdock, LCCN, Requestor, Notes, Type, Shift, RunOutTime, CICSTYPE FROM ILC,Reasontype WHERE Reasontype.typeID = ILC.typeID";
              
                queryString += string.Format(" AND ILC.Today BETWEEN '{0}' AND '{1}' ", dt3.Text, dt4.Text);  

                                
                loadDataGrid(queryString);

               
            }
            catch (Exception ex)
            {
                MessageBox.Show("You must Refresh first before you can Search again!");
                return;

            }
        }





我的尝试: < br $>


互联网查询



What I have tried:

Internet lookup

queryString += string.Format(" AND ILC.Today BETWEEN '#{0}#' AND '#{1}#' ", dt3.Text, dt4.Text);  queryString += string.Format(" AND ILC.Today BETWEEN '{0}' AND '{1}' ", dt3.Text, dt4.Text);

推荐答案

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



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

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. Always use Parameterized 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

--'

其他一切都是评论。

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



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



Plus ...使用参数意味着该值作为SQL理解的DateTime对象传递,所以它不必对日期格式做出假设,这会引起你注意到的问题......

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?

Plus ... using parameters means that the value is passed as a DateTime object that SQL understands, so it does not have to make assumptions about date formats, which is causing the problem you have noticed...


这篇关于使用2个日期时间选择器访问数据库进行Winform日期搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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