我在<appSettings>
中的web.comfig
下定义了我的数据库连接:
<appSettings>
<add key="ConnStr"
value="Data Source=dsk-159\SQLEXPRESS;Initial Catalog=master;Integrated Security=True"/>
</appSettings>
但问题是我无法从我的aspx页面访问它,因为我正在尝试这样
<asp:SqlDataSource ID="SqlDataSource2" runat="server"
ConnectionString="<%$ ConnectionStrings:goldsConnectionString %>"
SelectCommand="SELECT distinct TechnologyId , [TechnologyName], [ChildId] FROM [TreeTable] where childid is null AND technologyid in(@hid1)">
<SelectParameters>
<asp:ControlParameter ControlID="hid1" Name="hid1" DefaultValue="23" />
</SelectParameters>
代替<connectionStrings>
我想在<appSettings>
Plesae告诉正确的语法。
答案 0 :(得分:5)
你知道你可以在后面的代码中设置连接字符串而不是内联,它更清晰。
SqlDataSource2.ConnectionString =
System.Configuration.ConfigurationManager.AppSettings["ConnStr"];
答案 1 :(得分:3)
您的web.config应如下所示:
<appSettings>
<add key="ConnStr" value="Server=yourservername;Database=yourdatabasename;UID=yourusername;Password=youruserpassword"/>
</appSettings>
你的.aspx文件应该如下:
<asp:GridView ID="grd" runat="server" DataSourceID="SqlDataSource2">
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ appSettings:ConnStr %>" SelectCommand="SELECT * FROM ticketmaster"></asp:SqlDataSource>
答案 2 :(得分:0)
尝试将aspx部分更改为:
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:ConnStr %>" SelectCommand="SELECT distinct TechnologyId , [TechnologyName], [ChildId] FROM [TreeTable] where childid is null AND technologyid in(@hid1)">
<SelectParameters>
<asp:ControlParameter ControlID="hid1" Name="hid1" DefaultValue="23" />
</SelectParameters>
我做了两件事:
答案 3 :(得分:0)
您可以在代码隐藏页面中设置连接字符串
SqlDataSource2.ConnectionString =
System.Configuration.ConfigurationManager.AppSttings["ConnStr"];