LIKE 关键字在 TableAdapter 中不起作用

发布于 2024-12-22 01:55:07 字数 2679 浏览 1 评论 0原文

我使用 C# 和连接到表适配器的数据重复器来从表中提取几列。我的适配器上有 3 个 FillBy 子句:FillByFirstName、FillByLastName 和 FillByMRN(医疗记录号)。这三者都从 SQL 2000 中名为 Clients 的表中提取数据。

First Name QUERY 看起来像这样,并且它按需要工作:

SELECT CL_LName, CL_FName, MED_REC_NO, CL_SEX, CL_DOB, RACE_Code FROM dbo.Clients WHERE CL_FName = ? ORDER BY CL_LName, CL_FName

Last Name QUERY 看起来像这样,它也按需要工作:

SELECT CL_LName, CL_FName, MED_REC_NO, CL_SEX, CL_DOB, RACE_Code FROM dbo.Clients WHERE CL_LName = ? ORDER BY CL_LName, CL_FName

MRN QUERY 是它失败的地方,它是不同的,因为它使用 SQL LIKE 语句。

经过大量的在线搜索后,我发现了一些被吹捧为有效但对我来说完全失败的“解决方案”。

我失败的第一个解决方案是:

表适配器查询:

SELECT CL_LName, CL_FName, MED_REC_NO, CL_SEX, CL_DOB, RACE_Code FROM dbo.Clients WHERE (MED_REC_NO LIKE ?) ORDER BY CL_LName, CL_FName

C# 代码:

clientsTableAdapter.FillByMRN(clinicalSystemDataSet.Clients, "%" + searchStr + "%");

这不会返回任何错误,也不会返回任何结果。

当我在 SQL 中跟踪它时,它是这样执行的(我只删除了换行符和一些空格)。

exec sp_executesql N'SELECT        CL_LName, CL_FName, MED_REC_NO, CL_SEX, CL_DOB, RACE_Code FROM            dbo.Clients WHERE        (MED_REC_NO LIKE @P1) ORDER BY CL_LName, CL_FName', N'@P1 char(1024)', '%2222%'

我删除的空格是...最后一部分中的 %2222% 被填充为 1024 个字符。我认为这不是问题,因为没有 like 语句的工作方法也被填充了。如果这是有问题的,我不知道如何更改行为,因为表适配器与 SQL 一起创建此查询。

我无法弄清楚为什么上述方法失败 直接以这种格式在 sql 中执行它会返回一个空记录集,其中没有填充空格。我的表中有 11 条记录通过此 WORKING 查询返回:

SELECT  CL_LName, CL_FName, MED_REC_NO, CL_SEX, CL_DOB, RACE_Code FROM dbo.Clients WHERE (MED_REC_NO LIKE '%2222%') ORDER BY CL_LName, CL_FName

...这是上面的 sql 跟踪转换为不带参数且具有相同选择标准的实际语句。

对我来说失败的第二个解决方案是:

表适配器查询:

SELECT CL_LName, CL_FName, MED_REC_NO, CL_SEX, CL_DOB, RACE_Code FROM dbo.Clients WHERE (MED_REC_NO LIKE '%' + ? + '%') ORDER BY CL_LName, CL_FName

虽然从代码调用方法时这不起作用,但通过数据集设计器编辑语句时,如果我单击查询生成器并测试它(它会提示输入参数) )它将返回所需的记录集。

C# 部分:

clientsTableAdapter.FillByMRN(clinicalSystemDataSet.Clients, searchStr);

这不会返回任何结果,并且 SQL 跟踪显示:

exec sp_executesql N'SELECT CL_LName, CL_FName, MED_REC_NO, CL_SEX, CL_DOB, RACE_Code FROM dbo.Clients WHERE (MED_REC_NO LIKE ''%'' + @P1 + ''%'') ORDER BY CL_LName, CL_FName', N'@P1 char(1024)', '2222'

2222 再次被填充为 1024 个字符。但注意sql中的'%'变成了''%''。适配器/SQL 正在为我将单引号修复为双单引号。

此外,我尝试在不带单引号的情况下传递 % ,但这只会导致表适配器中的语法错误。我尝试过传递双引号,也有语法错误......以及其他一些杂项“这应该失败,但为什么不”类型的东西。

几乎在每一篇文章中,这两种方法中的一种都被证明是一种可行的方法,但我就是无法使其发挥作用。

表适配器应该如何呈现 sql 以及它的实际情况似乎存在脱节..特别是当查询生成器使用运行时失败的完全相同的语句显示成功时。

I am using C# with a data repeater tied to a table adapter to pull a few columns from a table. I have 3 FillBy clauses on my adapter, FillByFirstName, FillByLastName and FillByMRN (Medical Record Number). These all three pull data from a table in SQL 2000 called Clients.

The First Name QUERY looks like this and it works as desired:

SELECT CL_LName, CL_FName, MED_REC_NO, CL_SEX, CL_DOB, RACE_Code FROM dbo.Clients WHERE CL_FName = ? ORDER BY CL_LName, CL_FName

The Last Name QUERY looks like this and it too works as desired:

SELECT CL_LName, CL_FName, MED_REC_NO, CL_SEX, CL_DOB, RACE_Code FROM dbo.Clients WHERE CL_LName = ? ORDER BY CL_LName, CL_FName

The MRN QUERY is where it fails and it is different because it utilizes the SQL LIKE statement.

After tons of searching online I've found "solutions" that are touted as working but utterly fail for me.

Firstly solution I have failed with is:

Table Adapter Query:

SELECT CL_LName, CL_FName, MED_REC_NO, CL_SEX, CL_DOB, RACE_Code FROM dbo.Clients WHERE (MED_REC_NO LIKE ?) ORDER BY CL_LName, CL_FName

C# code:

clientsTableAdapter.FillByMRN(clinicalSystemDataSet.Clients, "%" + searchStr + "%");

This returns no error and no results.

When I trace it in SQL it's executed like this (I've removed only the line breaks and some spacing).

exec sp_executesql N'SELECT        CL_LName, CL_FName, MED_REC_NO, CL_SEX, CL_DOB, RACE_Code FROM            dbo.Clients WHERE        (MED_REC_NO LIKE @P1) ORDER BY CL_LName, CL_FName', N'@P1 char(1024)', '%2222%'

The spacing I removed is... the %2222% in the last portion is padded out to 1024 characters. I don't think this is at issue because the working methods without the like statement are padded as well. IF this is at issue, I don't know how to change the behavior since the table adapter creates this query in cohort with SQL.

I CANNOT FIGURE OUT WHY THE ABOVE METHOD FAILS Executing it directly in sql in this format returns an empty recordset with our without padding spaces. There are 11 records in my table that return with this WORKING query:

SELECT  CL_LName, CL_FName, MED_REC_NO, CL_SEX, CL_DOB, RACE_Code FROM dbo.Clients WHERE (MED_REC_NO LIKE '%2222%') ORDER BY CL_LName, CL_FName

...which is the above sql trace transformed into the actual statement without params and with the identical selection criteria.

The second solution that fails for me is this:

Table Adapter Query:

SELECT CL_LName, CL_FName, MED_REC_NO, CL_SEX, CL_DOB, RACE_Code FROM dbo.Clients WHERE (MED_REC_NO LIKE '%' + ? + '%') ORDER BY CL_LName, CL_FName

While this does NOT work when calling the method from code, when editing the statement via the DataSet Designer, if I click the QUERY BUILDER and test it (it prompts for the parameter) it will return the desired set of records.

C# portion:

clientsTableAdapter.FillByMRN(clinicalSystemDataSet.Clients, searchStr);

This returns NO results and a SQL trace shows:

exec sp_executesql N'SELECT CL_LName, CL_FName, MED_REC_NO, CL_SEX, CL_DOB, RACE_Code FROM dbo.Clients WHERE (MED_REC_NO LIKE ''%'' + @P1 + ''%'') ORDER BY CL_LName, CL_FName', N'@P1 char(1024)', '2222'

Again the 2222 is padded out to 1024 characters. But notice that the '%' in the sql is changed to ''%''. The adapter/SQL is fixing single quotes to double single quotes FOR me.

Additionally I've tried passing % without the single quotes but it only results in syntax errors in the table adapter. I've tried passing double quotes, with syntax errors as well.. and a few other miscellaneous "this should fail, but why not" type things.

In almost every post one of these two ways is demonstrated to be a working way to do it, but I just can't make it work.

There appears to be a disconnect in how the tableadapter should be presenting sql and how it actually is.. especially when the query builder shows success using the exact same statement that fails during runtime.

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

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

发布评论

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

评论(1

咆哮 2024-12-29 01:55:07

注意:我没有使用过 TableAdapter。

不过,我尝试在 SQL Server 中创建一个包含 char(1024) 字段的临时表,并使用 like 查询。

我的预感是,您必须将 TableAdapter 查询修改为

SELECT CL_LName, CL_FName, MED_REC_NO, CL_SEX, CL_DOB, RACE_Code 
FROM dbo.Clients WHERE (MED_REC_NO LIKE RTRIM(?)) 
ORDER BY CL_LName, CL_FName

原因(我的猜测)是该字段的数据类型为 char(1024)。因此,它可以填充参数,使其长度为 2222 之后的 1024 个字符。

因此,查询可能会变为

exec sp_executesql N'SELECT        CL_LName, CL_FName, MED_REC_NO, CL_SEX, CL_DOB, 
RACE_Code FROM            dbo.Clients WHERE        (MED_REC_NO LIKE @P1) ORDER BY 
CL_LName, CL_FName', N'@P1 char(1024)', '%2222........%'

Where ...... will be the padding of space。因此,该查询可以解释为,获取 MRN 以 2222 开头的记录,其余部分后跟空格,您的表中可能没有匹配的记录。

我猜测,将 RTRIM 添加到查询中应该可行。

Note: I haven't worked with TableAdapter.

However, I tried creating a temporary table in SQL Server with a field of char(1024) and used the like query.

My hunch is that, you will have to modify the TableAdapter query to

SELECT CL_LName, CL_FName, MED_REC_NO, CL_SEX, CL_DOB, RACE_Code 
FROM dbo.Clients WHERE (MED_REC_NO LIKE RTRIM(?)) 
ORDER BY CL_LName, CL_FName

The reason (my guess) is that the field is of data type char(1024). So, it could be padding the argument to make it the length of 1024 characters after 2222.

So, the query could become

exec sp_executesql N'SELECT        CL_LName, CL_FName, MED_REC_NO, CL_SEX, CL_DOB, 
RACE_Code FROM            dbo.Clients WHERE        (MED_REC_NO LIKE @P1) ORDER BY 
CL_LName, CL_FName', N'@P1 char(1024)', '%2222........%'

Where ...... will be the padding of space. As a result, the query could be interpreted as, get me records with MRN that starts with 2222 and rest is followed by space, for which there might not be a matching record in your table.

In my guess, adding RTRIM to the query should work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文