简体   繁体   English

对SqlDataSource感到困惑

[英]Confused about SqlDataSource

in my C# code, I use a connection string like this: 在我的C#代码中,我使用如下连接字符串:

connection = new SqlConnection(ConfigurationManager.AppSettings["myconnectionstring"])

which works fine. 效果很好。

I'm now trying to use the <asp:SqlDataSource control, but I can't seem to use the above connection string in the control like this: 我现在正在尝试使用<asp:SqlDataSource控件,但我似乎无法在控件中使用上述连接字符串,如下所示:

<asp:SqlDataSource ID="SqlDataSource1" runat="server"   
ConnectionString="<%$ ConfigurationManager.AppSettings["myconnectionstring"] %>"   
SelectCommand="SELECT * from table1"></asp:SqlDataSource> 

I get an error saying: 我收到一条错误消息:

Parser Error Message: The server tag is not well formed. 解析器错误消息:服务器标记的格式不正确。

What am I doing wrong? 我究竟做错了什么?

it is not code you run there but a placeholder: that basically tells the DataSource 'look in the config file for a configuration block called ConnectionStrings and if you find it take the value from the element with the key myconnectionstring' 它不是在此处运行的代码,而是一个占位符:基本上告诉数据源“在配置文件中查找名为ConnectionStrings的配置块,如果找到,则使用myconnectionstring键从元素中获取值”

<asp:SqlDataSource ID="SqlDataSource1" runat="server"    
ConnectionString="<%$ ConnectionStrings:myconnectionstring%>"    
SelectCommand="SELECT * from table1"></asp:SqlDataSource>  

remember to add your connectionstring the app.config section ConnectionStrings, 记得在app.config部分的ConnectionStrings中添加连接字符串,

<connectionStrings>
  <add name="myconnectionstring" connectionString="server=localhost;database=Northwind;user=north;password=north;" providerName="System.Data.SqlClient" />
</connectionStrings>

specially because you want to keep them safe 特别是因为您要确保它们的安全

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM