简体   繁体   English

ms access 中的“LIKE”运算符在 vb.net 中不起作用

[英]ms access “LIKE” operator in ms access does not work in vb.net

I hope someone can shed some light on this strange behavior.我希望有人能对这种奇怪的行为有所了解。 I have a very simple sql statement thus:我有一个非常简单的 sql 语句,因此:

"SELECT TOP 1 PartRefID FROM Parts WHERE PartDescription LIKE '*Backshell*' AND ParentID = 2097"

which is executed scalar.这是执行标量。 when I enter this in the access query editor it works just fine and returns the expected value.当我在访问查询编辑器中输入它时,它工作正常并返回预期值。 however, when I run it from within some vb.net code it always returns zero.然而,当我从一些 vb.net 代码中运行它时,它总是返回零。 can anyone tell me why this is the case?谁能告诉我为什么会这样? thanks谢谢

When using .net, you have to use the % sign - it more of a ANSI standard, and this works with jet/ace (access) data engine also.使用 .net 时,您必须使用 % 符号 - 它更像是 ANSI 标准,这也适用于 jet/ace(访问)数据引擎。

So, when writing .net code as opposed to VBA, then you are to use % for wild cards - not *.因此,在编写 .net 代码而不是 VBA 时,您将使用 % 作为通配符 - 而不是 *. In fact, even in Access VBA, if you were to use ADO (as opposed to the more common and recommended DAO - then even in VBA + ADO, you have to use % in place of *).事实上,即使在 Access VBA 中,如果您要使用 ADO(而不是更常见和推荐的 DAO - 那么即使在 VBA + ADO 中,您也必须使用 % 代替 *)。

so in fact most sql syntax uses %.所以实际上大多数 sql 语法都使用 %. For ADO, or now what is called ado.net?对于 ADO,或者现在叫做 ado.net 的东西? Then % is to be used.然后 % 将被使用。

However, inside access?但是,里面访问? You can turn on ANSI compatibility - but it WILL make a huge mess of existing access applications - so I don't recommend doing this (enabling Access ANSI sql compatibility mode).您可以打开 ANSI 兼容性 - 但它会使现有的访问应用程序变得一团糟 - 所以我不建议这样做(启用 Access ANSI sql 兼容模式)。

You can quite much assume for "most" software outside of Access - EVEN when hitting the Access database, you use % for wild cards.对于 Access 之外的“大多数”软件,您几乎可以假设——即使在访问 Access 数据库时,您也使用 % 作为通配符。 The only exception here is if one was to use DAO directly as a reference library, and I STRONG recommend you don't do this.这里唯一的例外是如果直接使用 DAO 作为参考库,我强烈建议您不要这样做。

so try using % - and all should be find and well.所以尝试使用 % - 一切都应该找到并且很好。

尝试替换 *

"SELECT TOP 1 PartRefID FROM Parts WHERE PartDescription LIKE '%Backshell%' AND ParentID = 2097"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM