在 web.config 中指定配置源时如何设置 SqlDataSource 的 ConnectionString

发布于 2024-09-25 07:11:59 字数 396 浏览 1 评论 0原文

我希望能够快速创建一些没有隐藏代码文件的简单 ASP.NET 报告。每个报告都是一个 aspx 文件,其中可能有多个 SqlDataSource 控件。我希望能够使用 <%$ ConnectionStrings:MyTag %>语法来设置连接字符串,但是,我们使用的应用程序在单独的配置文件中具有实际的连接字符串,该配置文件通过使用 configSource="App_Data\database.config" 在 web.config 中引用。

在后面的代码中,我可以使用以下方式以编程方式访问 ConnectionString: ConfigurationManager.ConnectionStrings["AbleCommerce"].ConnectionString

但是我们如何在不使用隐藏代码的情况下设置连接字符串呢?

I want to be able to quickly create some simple ASP.NET reports that don't have a code behind file. Each report will be an aspx file that may have multiple SqlDataSource controls on it. I want to be able to use the <%$ ConnectionStrings:MyTag %> syntax to set the connection string but, the application we're using has the actual connection string in a separate config file that is referenced in the web.config by using configSource="App_Data\database.config".

In code behind, I can programmatically access the ConnectionString using:
ConfigurationManager.ConnectionStrings["AbleCommerce"].ConnectionString

But how do we set the connection string without using a code behind?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

地狱即天堂 2024-10-02 07:11:59

您已经提到了问题中的答案,如果您的连接字符串是在外部文件中定义的,我猜测该文件的内容将如下所示:

<connectionStrings>
    <add name="AbleCommerce" 
        connectionString="..." 
        providerName="System.Data.SqlClient" />
</connectionStrings>

您可以使用以下命令在您的 aspx 页面中引用此内容

<asp:sqldatasource id="SqlDataSource1" runat="server" 
    connectionstring="<%$ ConnectionStrings:AbleCommerce %>" 
    selectcommand="SELECT * FROM [tProducts]"></asp:sqldatasource>

You've aluded to the answer in your question, if your connection string is defined in an external file I'm guessing the contents of that file will look like:

<connectionStrings>
    <add name="AbleCommerce" 
        connectionString="..." 
        providerName="System.Data.SqlClient" />
</connectionStrings>

You would reference this in your aspx page by using

<asp:sqldatasource id="SqlDataSource1" runat="server" 
    connectionstring="<%$ ConnectionStrings:AbleCommerce %>" 
    selectcommand="SELECT * FROM [tProducts]"></asp:sqldatasource>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文