如何在 ASP.NET 中使用数据库连接字符串?

发布于 2024-11-09 11:58:49 字数 988 浏览 0 评论 0原文

我刚刚真正开始使用 ASP.NET,我想知道是否可以使用数据源中的连接字符串而不是对其进行硬编码。我已在数据源中添加了连接并且它可以工作,我还可以在设计模式下将表拖动到页面,但我不知道如何在代码中访问它。这是我将其拖入时得到的结果:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
    DataSourceID="SqlDataSource1" 
    EmptyDataText="There are no data records to display.">
    <Columns>
        <asp:BoundField DataField="id" HeaderText="id" ReadOnly="True" 
            SortExpression="id" />
        <asp:BoundField DataField="xxx" HeaderText="xxx" 
            SortExpression="xxx" />
        <asp:BoundField DataField="xxx" HeaderText="xxx" 
            SortExpression="xxx" />
    </Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
    ConnectionString="<%$ ConnectionStrings:xxxConnectionString1 %>" 
    ProviderName="<%$ ConnectionStrings:xxxConnectionString1.ProviderName %>" 
    SelectCommand="SELECT xxx FROM xxx">
</asp:SqlDataSource>

I've just started ASP.NET for real, and I was wondering if it is possible to use connection strings from Data Sources instead of hard-coding them. I've added my connection in Data Sources and it works, and I can also drag a table to a page in design mode, but I can't figure out how to access it in code. Here's what I get when I drag it in:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
    DataSourceID="SqlDataSource1" 
    EmptyDataText="There are no data records to display.">
    <Columns>
        <asp:BoundField DataField="id" HeaderText="id" ReadOnly="True" 
            SortExpression="id" />
        <asp:BoundField DataField="xxx" HeaderText="xxx" 
            SortExpression="xxx" />
        <asp:BoundField DataField="xxx" HeaderText="xxx" 
            SortExpression="xxx" />
    </Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
    ConnectionString="<%$ ConnectionStrings:xxxConnectionString1 %>" 
    ProviderName="<%$ ConnectionStrings:xxxConnectionString1.ProviderName %>" 
    SelectCommand="SELECT xxx FROM xxx">
</asp:SqlDataSource>

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

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

发布评论

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

评论(3

总攻大人 2024-11-16 11:58:49

我认为你的问题是..如何访问你

在 aspx 页面中看到的 ConnectionString="<%$ ConnectionStrings:xxxConnectionString1 %>" 的连接字符串属性?

正如 Bala R 提到的...

使用

ConfigurationManager.ConnectionStrings["xxxConnectionString1"].ToString();

但为了做到这一点,您应该添加对 System.Configuration 的引用。

导入System.Configuration

I think your question is.. how do you access the connection string property that you see as

ConnectionString="<%$ ConnectionStrings:xxxConnectionString1 %>" in your aspx page right?

As Bala R mentioned...

use

ConfigurationManager.ConnectionStrings["xxxConnectionString1"].ToString();

but in order to do this, you should add the reference to System.Configuration.

i.e.

Imports System.Configuration

小猫一只 2024-11-16 11:58:49

您可以像这样在后面的代码中获取连接字符串

ConfigurationManager.ConnectionStrings["xxxConnectionString1"].ConnectionString;

You can get the connection string in code behind like this

ConfigurationManager.ConnectionStrings["xxxConnectionString1"].ConnectionString;
Oo萌小芽oO 2024-11-16 11:58:49

上面的帖子是准确的,您将需要探索 system.configuration 程序集/命名空间。文本内容可以存储在AppSettings或ConnectionStrings中。值得了解他们是如何向前发展的。

The above posts are accurate, you'll want to explore the system.configuration assembly / namespace. text content can be stored in AppSettings or ConnectionStrings. its worth understanding how they both work moving forward.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文