如何在 OLDEB sql select 命令中检查字段值是否为特定字符长度?
我正在使用 C# OleDB 连接从 access 数据库读取数据。我不想要任何长度少于三个字符的结果。
我当前的代码显示“无效的模式字符串”错误。为什么我会收到此错误以及如何修复它?
string qry = "Select * from Table1 where ((CALLNO not like '') and " +
"((CALLNO like '%" + suggestedCallNo + "%') or ('" + suggestedCallNo + "' like '%' & CALLNO & '%')) and " +
"((Len(CALLNO))>=3))";
OleDbCommand myCommand = new OleDbCommand(qry, myConnection);
OleDbDataReader myReader = myCommand.ExecuteReader();
如果没有 ((Len(CALLNO))>=3)
这段代码可以正常工作。但是,一旦添加该行,一旦它执行读取器,我就会收到“无效模式字符串”异常。
我尝试了一些方法:
使用 (CALLNO not like '')
而不是 ((Len(CALLNO))>=3)
,这不会导致任何错误所以我没有遗漏括号。
尝试使用单字符通配符,例如 '_'
、'_ _'
、'_ _ _'
,而不是 (( Len(CALLNO))>=3)
我使用了(CALLNO not like '_')
,这会导致相同的“无效模式字符串”错误。事实上,我发现唯一不会引发错误的是空字符串 ''
和完整的通配符 '%'
。
我还直接在访问中从查询字符串运行查询,并且没有收到任何类型的错误。
感谢您提供任何信息,
LMB
I am reading from an access database using a C# OleDB connection. I don't want any results which are less than three characters in length.
My current code displays an "Invalid pattern string" error. Why am I getting this error and how can I fix it?
string qry = "Select * from Table1 where ((CALLNO not like '') and " +
"((CALLNO like '%" + suggestedCallNo + "%') or ('" + suggestedCallNo + "' like '%' & CALLNO & '%')) and " +
"((Len(CALLNO))>=3))";
OleDbCommand myCommand = new OleDbCommand(qry, myConnection);
OleDbDataReader myReader = myCommand.ExecuteReader();
Without the ((Len(CALLNO))>=3)
this code works fine. Once I add the line however I get an "Invalid pattern string" exception once it goes to execute the reader.
A few things I tried:
Instead of ((Len(CALLNO))>=3)
use (CALLNO not like '')
, this doesn't cause any error so I'm not missing brackets.
Trying to use single character wildcards like '_'
, '_ _'
, '_ _ _'
so instead of ((Len(CALLNO))>=3)
I used (CALLNO not like '_')
, this causes the same "Invalid pattern string" error. In fact the only things which I found not to throw an error are the empty string ''
and a full wildcard '%'
.
I also ran the query from the query string directly in access and there I don't get any errors of any kind.
Thanks for any info,
LMB
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该使用
?
而不是_
:http:// /www.techonthenet.com/access/queries/like.php
所以你应该能够使用
LIKE '???%'
编辑:
正如 David 指出的,它应该是
'???*'
而不是'???%'
。但是,
LEN
函数也应该可以工作,因此可能还有其他问题。Instead of
_
, you should use?
:http://www.techonthenet.com/access/queries/like.php
So you should be able to use
LIKE '???%'
EDIT:
As David pointed out, it should be
'???*'
not'???%'
.However, the
LEN
function should work too, so there is some other problem, probably.