Asp.net中内联查询中的SQL查询问题

发布于 2024-11-09 10:52:53 字数 444 浏览 0 评论 0原文

我的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 技术交流群。

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

发布评论

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

评论(5

金兰素衣 2024-11-16 10:52:53

您需要转义 '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);

混浊又暗下来 2024-11-16 10:52:53

您的查询应该类似于...

select * from table where DataName like 'lipsum''s lorem'

因为您需要将 ' 转义为 ' 才能使其正常工作。

有关更多详细信息,请查看此链接: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...

select * from table where DataName like 'lipsum''s lorem'

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:

记忆消瘦 2024-11-16 10:52:53

您应该将单引号替换为双引号

String.replace("'", "''")

这意味着将搜索字符串放入字符串变量中,并将单引号替换为双引号

string str = "lipsum's lorem"
str.Replace("'", "''")

You should replace the Single Quote with Double Quote

String.replace("'", "''")

That means put the search string in the string variable and replace the single quote with double quote

string str = "lipsum's lorem"
str.Replace("'", "''")
阳光下慵懒的猫 2024-11-16 10:52:53

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)

是伱的 2024-11-16 10:52:53

使用参数化查询。

var command = new SqlCommand()
    {
        CommandText = "SELECT * FROM [table] WHERE DataName = @dataName",
        Parameters = { { "@dataName", "lipsum's lorem" } }
    }

Use a parameterized query.

var command = new SqlCommand()
    {
        CommandText = "SELECT * FROM [table] WHERE DataName = @dataName",
        Parameters = { { "@dataName", "lipsum's lorem" } }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文