如何在Access中使用 [英] How to Use Between In Access

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

问题描述

cmd = new OleDbCommand(从Sal_Slab选择Sal_Percentage,其中'80''在Start_From和End_To之间,'=。'+ company.SelectedValue.ToString()+'',con);





和我的Sal_Slabe表是

cmd = new OleDbCommand("select Sal_Percentage from Sal_Slab where ''80'' between Start_From and End_To and Comp_Id=''"+company.SelectedValue.ToString()+"''", con);


And My Sal_Slabe Table is

Start_From   End_To     Percentage
0             80         7
81            100       10

................................... .................................................. .......

以上查询返回7& 10 ....................

我的查询中有什么错误



添加了代码块 - OriginalGriff [/ edit]

..................................................................................................
the above query return both 7 & 10....................
what is Wrong in my query

[edit]Code block added - OriginalGriff[/edit]

推荐答案

这不是BETWEEN的内容。

可能,你想要更像的东西:

That''s not what BETWEEN is about.
Probably, you want something more like:
SELECT Sal_Percentage FROM Sal_Slab WHERE StartFrom < 80 AND End_To >= 80 AND ...



但是......我担心你使用80作为字符串 - 如果Start_From和End_To值是字符串,那么任何比较都可能不会很好,因为它将使用字符串比较而不是数字 - 结果是非常不同的。

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


But... I''m concerned that you are using 80 as a string - if the Start_From and End_To Values are strings, then any comparison is probably not going to work too well, as it will use string comparisons rather than numeric - and the results are very different.
In addition, you should not 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.


SELECT Sal_Percentage

FROM Sal_Slab

WHERE(Comp_Id ='''C001'')AND('' 100''在Start_From和End_To之间'和'(''100''> = Start_From)AND(''100''< = End_To)
SELECT Sal_Percentage
FROM Sal_Slab
WHERE (Comp_Id = ''C001'') AND (''100'' BETWEEN Start_From AND End_To) AND (''100'' >= Start_From) AND (''100'' <= End_To)


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

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