如何使用 C# 在 MS-Access 中的查询中使用带有通配符的 LIKE 运算符

发布于 2024-11-28 09:27:00 字数 691 浏览 0 评论 0原文

我在 C# 应用程序中使用 MS Access 数据库,需要传递类似参数化的查询。我发现各种文件说我们可以使用“*”或“?”作为通配符。但是当我尝试将其添加到我的查询中时,它会抛出异常。

实际上 MSAccess 使用类似这种格式... LIKE "United" {这里双引号是强制性的。}但是作为我的 C# 代码中的字符串,我无法在运行时创建 " 。如果我在我的 C# 代码中使用 "字符串不止一次出现红色波浪。

所以我需要一种使用具有通配符支持的查询的方法。

我的字符串是这样的

SELECT student.roll_no, student.s_name, fee.fee_date, (fee.adm_fee+fee.mon_fee+fee.lib_fee+fee.exm_fee) AS fee, class.c_name
FROM class
INNER JOIN (fee INNER JOIN student ON fee.s_id = student.s_id) ON (fee.c_id = class.c_id) AND (class.c_id = student.c_id)
WHERE student.s_name Like ""\""*" + name + "\"*";

,我希望命令文本中的类似条件为 ... WHERE Student.s_name Like "*xyx*"

I am using MS Access database in my C# app, where I need to pass parameterized-like query. I came around various documents saying we can use "*" or "?" as wildcards. But when i try to add it to my query it throws exception.

Actually MSAccess uses like in this format ... LIKE "United" {here double quotes are mandatory.} But as a string in my C# code i cant create " in runtime. If i use " in my string more than once a red wave appears.

So I need a way to use query with wildcard support.

My string is this

SELECT student.roll_no, student.s_name, fee.fee_date, (fee.adm_fee+fee.mon_fee+fee.lib_fee+fee.exm_fee) AS fee, class.c_name
FROM class
INNER JOIN (fee INNER JOIN student ON fee.s_id = student.s_id) ON (fee.c_id = class.c_id) AND (class.c_id = student.c_id)
WHERE student.s_name Like ""\""*" + name + "\"*";

I want my like condition in my command text as ... WHERE student.s_name Like "*xyx*"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你另情深 2024-12-05 09:27:00

我认为您的字符串格式可能不正确(当然,这是假设您收到 SQL 异常,因为您没有提及有关异常的任何详细信息:))

string commandtext =@"SELECT student.roll_no, student.s_name, fee.fee_date, (fee.adm_fee+fee.mon_fee+fee.lib_fee+fee.exm_fee) AS fee, class.c_name FROM class INNER JOIN (fee INNER JOIN student ON fee.s_id = student.s_id) ON (fee.c_id = class.c_id) AND (class.c_id = student.c_id) WHERE student.s_name Like \"*"+name+"*\"";

I think you may just have the formatting in your string incorrect (of course, that's assuming you're getting an SQL Exception, since you didn't mention any details about the Exception :))

string commandtext =@"SELECT student.roll_no, student.s_name, fee.fee_date, (fee.adm_fee+fee.mon_fee+fee.lib_fee+fee.exm_fee) AS fee, class.c_name FROM class INNER JOIN (fee INNER JOIN student ON fee.s_id = student.s_id) ON (fee.c_id = class.c_id) AND (class.c_id = student.c_id) WHERE student.s_name Like \"*"+name+"*\"";
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文