如何在 ASP.NET 中使用数据库连接字符串?
我刚刚真正开始使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为你的问题是..如何访问你
在 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
您可以像这样在后面的代码中获取连接字符串
You can get the connection string in code behind like this
上面的帖子是准确的,您将需要探索 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.