如何修改查询 [英] How to modify a query

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

问题描述

请帮助我

我已经尝试开发一个查询,在一年内调用休眠交易。我的查询给我一个错误查询表达式中的语法错误(缺少运算符)'NOT BETWEEN [Dte]#01-Jan-18 9:24:36 AM#AND [Dte]<>#01-Mar-18 9: 24:53 AM#'



我尝试过:



我试过这样的。

dap =新的OleDb.OleDbDataAdapter(SELECT * FROM tblRevenueRegister WHERE NOT BETWEEN [Dte]#& dtpFromDate.Value&#TO [Dte]<> ;#& dtpToDate.Value&#Order By RevID,Cnn)

dap.Fill(ds,tblRevenueRegister)

rpt.SetDataSource(ds )

CrystalReportViewer1.ReportSource = rpt

Please help me
I have try to develop a query to call dormant transactions within a year. my query giving me an error Syntax error (missing operator) in query expression 'NOT BETWEEN [Dte]#01-Jan-18 9:24:36 AM# AND [Dte] <>#01-Mar-18 9:24:53 AM#'

What I have tried:

I have tried like this.
dap = New OleDb.OleDbDataAdapter("SELECT * FROM tblRevenueRegister WHERE NOT BETWEEN [Dte]#" & dtpFromDate.Value & "# TO [Dte] <>#" & dtpToDate.Value & "# Order By RevID", Cnn)
dap.Fill(ds, "tblRevenueRegister")
rpt.SetDataSource(ds)
CrystalReportViewer1.ReportSource = rpt

推荐答案

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



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

For starters, 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

--'

其他一切都是评论。

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



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

此外,它经常会出现日期格式问题 - 你假设你运行查询的服务器工作的日期格式与计算机你生成字符串 - 如果不是,你会得到错误,或错误的日期。



其次,为什么你有<> ;在你第二次约会之前?

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?
In addition, it often gives problems with date formats - you are assuming that the server you run the query on is working to the same date format as the computer you generate the string on - and if it isn't, you will get errors, or the wrong date.

Secondly, why do you have "<>" before your second date?


这是因为你写错了。它应该是这样的:

It's because you are writing it incorrect. It should be something like this:
SELECT *
FROM tblRevenueRegister
WHERE [Dte] NOT BETWEEN date1 AND date2
ORDER BY RevID


这篇关于如何修改查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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