Ste*_*eve 0 c# sql-server modifier
我试图在C#中编写一个基本查询,其中包括使用'%'修饰符.
这是我的查询:
SELECT userName
FROM tblUserInformation
WHERE userName LIKE %@userNameQuery%
Run Code Online (Sandbox Code Playgroud)
当我运行时,我得到一个SQL异常说
@userNameQuery附近有语法错误.
我之前遇到过与SQL查询和C#sql查询字符串类似的差异,但我不确定如何解决这个问题.我试过了:
SELECT userID
FROM tblUserInformation
WHERE userName LIKE '%'@userNameQuery'%'
Run Code Online (Sandbox Code Playgroud)
下面的一个不会抛出异常但不返回任何结果.它应该是因为我可以直接对数据库运行相同的查询,并返回许多结果.
SELECT userName
FROM tblUserInformation
WHERE userName LIKE '%@userNameQuery%'
Run Code Online (Sandbox Code Playgroud)
我这样做了吗?
此外,@userNameQuery正确填写SqlParameter并且是正确的数据类型等.尝试过上面和第二个工作(返回没有结果)我确信我的查询字符串中没有语法错误(额外的分号或缺少") .
感谢您的时间!
您需要传入%参数的值,而不是在查询本身中.
SELECT userName from tblUserInformation WHERE userName LIKE @userNameQuery
Run Code Online (Sandbox Code Playgroud)
哪里@userNameQuery会是这样%my query%.
| 归档时间: |
|
| 查看次数: |
339 次 |
| 最近记录: |