如何在 SqlDataSource 中使用 appSettings 键

How to use appSettings key in SqlDataSource

Web.config:

<appSettings file="Application.config">
        <add key="stringC" value="Data Source=svr1;Initial Catalog=myDB;Integrated Security=FALSE;user=user2;pwd=@#3pop;" />
</appSettings>

ASP.net:

<asp:SqlDataSource ID="SqlDataSource1" runat="server"
    ConnectionString="<%$ {HOW TO USE THE APP SETTINGS KEY HERE} %>"
    SelectCommand="SELECT [au_id], [au_lname], [au_fname], [state] FROM [authors]" />

如何用应用程序密钥替换 {HOW TO USE THE APP SETTINGS KEY HERE}

你可以使用 <%$ ConfigurationSettings.AppSettings["stringC"] %>.

<asp:SqlDataSource ID="SqlDataSource1" runat="server"
    ConnectionString="<%$ ConfigurationSettings.AppSettings["stringC"] %>"
    SelectCommand="SELECT [au_id], [au_lname], [au_fname], [state] FROM [authors]" />

在您的 aspx 中,您需要以下内容

ConnectionString="<%$ appSettings:stringC %>"