Asp.net中内联查询中的SQL查询问题
我的sql表结构是这样的
ID DataName
1 Lipsum lorem
3 lipsum's lorem
我在asp.net中的内联查询是
select * from table where DataName like 'lipsum's lorem'
它给出了以下错误:
Msg 102, Level 15, State 1, Line 1
Incorrect syntax near 's'.
Msg 105, Level 15, State 1, Line 1
Unclosed quotation mark after the character string ''.
我不想创建一个存储过程来防止这种情况,我想要一个使用内联查询的解决方案。
My sql table structure is this
ID DataName
1 Lipsum lorem
3 lipsum's lorem
My inline query in asp.net is that
select * from table where DataName like 'lipsum's lorem'
It gives the following errors:
Msg 102, Level 15, State 1, Line 1
Incorrect syntax near 's'.
Msg 105, Level 15, State 1, Line 1
Unclosed quotation mark after the character string ''.
I don't want to create a stored procedure to prevent this, I want a solution to this using inline queries.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您需要转义 'lipsum's lorem'
'lipsum''s lorem' 中的
'
但真正的解决方法是使用参数化查询,以防止 SQL 注入。
SqlCommand.CommandText = "SELECT * FROM Table WHERE DataName = @DataName";
在命令对象中为 @DataName 添加一个参数及其值。
SqlCommand.Parameters.AddWithValue("@DataName", Value);
You need to escape the
'
in 'lipsum's lorem''lipsum''s lorem'
But the real fix is to a use parameterised query, to prevent SQL injection.
SqlCommand.CommandText = "SELECT * FROM Table WHERE DataName = @DataName";
In your command object add a parameter for @DataName with its value.
SqlCommand.Parameters.AddWithValue("@DataName", Value);
您的查询应该类似于...
因为您需要将
' 转义为 '
才能使其正常工作。有关更多详细信息,请查看此链接:http://blog.sqlauthority.com/2008/02/17/sql-server-how-to-escape-single-quotes -fix-error-105-unished-quotation-mark-after-the-character-string/
如果您使用
命令参数
,请查看此链接:Your query should look like...
As you need to escape the
' to '
to get it to work.For more details check out this link: http://blog.sqlauthority.com/2008/02/17/sql-server-how-to-escape-single-quotes-fix-error-105-unclosed-quotation-mark-after-the-character-string/
If you use
Command Parameters
, check this link out:您应该将单引号替换为双引号
这意味着将搜索字符串放入字符串变量中,并将单引号替换为双引号
You should replace the Single Quote with Double Quote
That means put the search string in the string variable and replace the single quote with double quote
ID DataName
1 Lipsum lorem
3 Lipsum's lorem
select * from table where DataName like 'lipsum''s lorem'
或
select * from table where DataName='lipsum''s lorem' >
从表中选择 *,其中 DataName like'lipsum%'(选择两行)
ID DataName
1 Lipsum lorem
3 lipsum's lorem
select * from table where DataName like 'lipsum''s lorem'
or
select * from table where DataName='lipsum''s lorem'
select * from table where DataName like'lipsum%' (To select both rows)
使用参数化查询。
Use a parameterized query.