SQL 如何在 Eval 格式字符串中使用单引号

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/264414/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 00:09:25  来源:igfitidea点击:

How to use Single Quotes in Eval Format String

asp.netsqlevalstring-formatting

提问by Dhaust

I've got a Repeater and its SqlDatasource nested inside a Gridview TemplatedField.
The Repeater's datasource SelectCommand is set using the FormatString of an Eval from the Gridview.
The SelectCommand has a WHERE clause which is to compare a string.
Because I have already used the single and double quotes, I am having trouble delimiting the string in the SQL WHERE clause.

我有一个中继器和它的 SqlDatasource 嵌套在 Gridview TemplatedField 中。
中继器的数据源 SelectCommand 是使用来自 Gridview 的 Eval 的 FormatString 设置的。
SelectCommand 有一个 WHERE 子句,用于比较字符串。
因为我已经使用了单引号和双引号,所以我无法在 SQL WHERE 子句中分隔字符串。

How do I add single quotes inside an Eval FormatString?

如何在 Eval FormatString 中添加单引号?

I have tried using 'Replace'.
I have tried using 'Special Characters' (... WHERE StringField = '{0}' ...)

我试过使用“替换”。
我曾尝试使用“特殊字符”(... WHERE StringField = '{0}' ...)

No luck so far. I appreciate any help you may be able to offer.

到目前为止没有运气。感谢您提供的任何帮助。

<asp:GridView ID="GridView1" runat="server" DataSourceID="DataSource1" DataKeyNames="Foo" AutoGenerateColumns="False" AllowSorting="true" >
    <Columns>
        <asp:BoundField DataField="Foo" HeaderText="Foo" SortExpression="Foo" />
        <asp:BoundField DataField="Bar" HeaderText="Bar" SortExpression="Bar" />
        <asp:TemplateField>
            <ItemTemplate>
                <asp:Repeater ID="Repeater1" runat="server" DataSourceID="DataSourceNested">
                    <ItemTemplate>
                        <asp:Label ID="Label1" runat="server" Text='<%# Eval("Blah") %>'></asp:Label>
                    </ItemTemplate>
                </asp:Repeater>
                <asp:SqlDataSource ID="DataSourceNested" runat="server" DataFile="~/App_Data/DatabaseName"
                    SelectCommand='<%# Eval("Bar", "SELECT Blah FROM TableName WHERE (StringField = {0})") %>' >
                </asp:SqlDataSource>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

回答by Adrian Clark

Don't forget that a .aspx page is simply XML. You just escape the quotes as you normally would.

不要忘记 .aspx 页面只是 XML。您只需像往常一样转义引号即可。

For example:

例如:

<asp:Repeater ID="repeatTheLabel" runat="server">
    <ItemTemplate>
        <asp:Label ID="Label1" Text="<%# Eval(&quot;Id&quot;, &quot;This is item '{0}'.&quot;) %>" runat="server" />
    </ItemTemplate>
    <SeparatorTemplate>
        <br />
    </SeparatorTemplate>
</asp:Repeater>

When the above expression is databound the value between <%#and %>becomes:

当上述表达式是数据绑定时,<%#和之间的值%>变为:

Eval("Id", "This is item '{0}'.")

Eval("Id", "This is item '{0}'.")

...which produces on the HTML page as output when databound with an array of objects with "Id" property values from 1 to 5:

...当数据绑定具有“Id”属性值从 1 到 5 的对象数组时,在 HTML 页面上生成作为输出:

This is item '1'.
This is item '2'.
This is item '3'.
This is item '4'.
This is item '5'.

这是项目“1”。
这是项目“2”。
这是项目“3”。
这是项目“4”。
这是项目“5”。

回答by Rune Grimstad

Store your sql queries in properties in your Page class. Not only does it work :-) but it makes your code easier to read and maintain.

将 sql 查询存储在 Page 类的属性中。它不仅有效:-),而且使您的代码更易于阅读和维护。

Oh, and you should use parameters in your queries instead of doing string replacements. That will solve the problem by removing the need for single quotes.

哦,您应该在查询中使用参数而不是进行字符串替换。这将通过消除对单引号的需要来解决问题。

回答by Keltex

Why don't you define this WHERE clause as a const in your codebehind. Define:

为什么不在代码隐藏中将此 WHERE 子句定义为常量。定义:

protected const string SELECTCLAUSE = 
"SELECT Blah FROM TableName WHERE (StringField = '{0}')";

Then your SelectCommand property would be:

那么您的 SelectCommand 属性将是:

SelectCommand='<%# Eval("Bar", SELECTCLAUSE ) %>'

回答by Ty.

Have you tried escaping the single quote characters?

您是否尝试过转义单引号字符?

... WHERE (StringField = \'{0}\') ...