asp.net2.0中对Web.Config的操作

in Develop

今日在博客园看到一篇文章
ASP.NET2.0里web.config配置的读写

读取appSettings节点

Configuration config

= WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);

AppSettingsSection appSettings = (AppSettingsSection) config.GetSection("appSettings");

string[] appKeys = appSettings.Settings.AllKeys;

for (int i = 0; i < appSettings.Settings.Count; i++)

{

//这里只进行简单的输出

Response.Write(appSettings.Settings[appKeys[i]].Value);

Response.Write("<BR>");

}

读取数据库连接字符串

Configuration config= WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);
ConnectionStringsSection conSection = (ConnectionStringsSection)config.GetSection("connectionstring ");
ConnectionStringSettingsCollection conCollection = conSection.ConnectionStrings;
foreach (ConnectionStringSettings conSetting in conCollection)
{
Response.Write(conSetting.ConnectionString);
Response.Write("<BR>");
}

当然,在这里你可以进行读、写操作
但是在用Save方法保存时会机器会变得特别慢,而且此时的CPU占用80%以上,所以要慎用Save()方法了

0 Comments

Leave a Reply

Using Gravatars in the comments - get your own and be recognized!

XHTML: These are some of the tags you can use: <a href=""> <b> <blockquote> <code> <em> <i> <strike> <strong>