会员
商店
众包
新闻
博问
闪存
赞助商
Chat2DB
所有博客
当前博客
我的博客
我的园子
账号设置
会员中心
简洁模式
...
退出登录
注册
登录
feng
博客园
首页
新随笔
联系
订阅
管理
随笔 - 190
文章 - 1
评论 - 25
阅读 -
70万
webconfig相关
取配置节数据库连接信息
Code
1
string
connstring
=
string
.Empty;
2
ConnectionStringSettings settings
=
ConfigurationManager.ConnectionStrings[
"
DataBaseConnection
"
];
3
if
(settings
!=
null
)
4
{
5
connstring
=
settings.ConnectionString;
6
}
7
修改配置信息
Code
1
public
void
SaveConfig(
string
key,
string
value)
2
{
3
try
4
{
5
string
fileName
=
"
Web.Config
"
;
6
XmlDocument document
=
new
XmlDocument();
7
document.Load(fileName);
8
((XmlElement)document.SelectSingleNode(
@"
//AppSetting/add[@key='
"
+
key
+
"
']
"
)). SetAttribute(
"
Value
"
, value);
9
document.Save(fileName);
10
}
11
catch
(Exception ex)
12
{
13
throw
new
Exception(ex.Message);
14
}
15
}
16
修改配置通用类
Code
using
System;
using
System.Configuration;
using
System.Web;
using
System.Web.Configuration;
///
<summary>
///
ConfigurationOperator 的摘要说明
///
</summary>
public
class
ConfigurationOperator:IDisposable
{
private
Configuration config;
public
ConfigurationOperator():
this
(HttpContext.Current.Request.ApplicationPath)
{
}
public
ConfigurationOperator(
string
path)
{
config
=
WebConfigurationManager.OpenWebConfiguration(path);
}
///
<summary>
///
设置应用程序配置节点,如果已经存在此节点,则会修改该节点的值,否则添加此节点
///
</summary>
///
<param name="key">
节点名称
</param>
///
<param name="value">
节点值
</param>
public
void
SetAppSetting(
string
key,
string
value)
{
AppSettingsSection appSetting
=
(AppSettingsSection)config.GetSection(
"
appSettings
"
);
if
(appSetting.Settings[key]
==
null
)
//
如果不存在此节点,则添加
{
appSetting.Settings.Add(key, value);
}
else
//
如果存在此节点,则修改
{
appSetting.Settings[key].Value
=
value;
}
}
///
<summary>
///
设置数据库连接字符串节点,如果不存在此节点,则会添加此节点及对应的值,存在则修改
///
</summary>
///
<param name="key">
节点名称
</param>
///
<param name="value">
节点值
</param>
public
void
SetConnectionString(
string
key,
string
connectionString)
{
ConnectionStringsSection connectionSetting
=
(ConnectionStringsSection)config.GetSection(
"
connectionStrings
"
);
if
(connectionSetting.ConnectionStrings[key]
==
null
)
//
如果不存在此节点,则添加
{
ConnectionStringSettings connectionStringSettings
=
new
ConnectionStringSettings(key, connectionString);
connectionSetting.ConnectionStrings.Add(connectionStringSettings);
}
else
//
如果存在此节点,则修改
{
connectionSetting.ConnectionStrings[key].ConnectionString
=
connectionString;
}
}
///
<summary>
///
保存所作的修改
///
</summary>
public
void
Save()
{
config.Save();
config
=
null
;
}
public
void
Dispose()
{
if
(config
!=
null
)
{
config.Save();
}
}
}
把上面的代码存放到App_Code文件夹下,我们在项目中就可以直接使用了。
分类:
ASPNET
好文要顶
关注我
收藏该文
微信分享
fsl
粉丝 -
52
关注 -
4
+加关注
0
0
升级成为会员
«
上一篇:
开始计算机英语的学习,先把这里当生词本用了。
»
下一篇:
在服务器端控件中添加客户端属性
posted @
2009-09-06 10:42
fsl
阅读(
208
) 评论(
0
)
编辑
收藏
举报
刷新页面
返回顶部
登录后才能查看或发表评论,立即
登录
或者
逛逛
博客园首页
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
编辑推荐:
·
AI与.NET技术实操系列:基于图像分类模型对图像进行分类
·
go语言实现终端里的倒计时
·
如何编写易于单元测试的代码
·
10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
·
.NET Core 中如何实现缓存的预热?
阅读排行:
·
25岁的心里话
·
闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
·
零经验选手,Compose 一天开发一款小游戏!
·
通过 API 将Deepseek响应流式内容输出到前端
·
AI Agent开发,如何调用三方的API Function,是通过提示词来发起调用的吗
积分与排名
积分 - 174699
排名 - 7473
随笔分类
(179)
ASPNET(27)
English(5)
flex(4)
Go(5)
html&css(5)
javascript(10)
jquery(6)
Linux(3)
Mssql(3)
Mysql(14)
nodejs(8)
oracle学习笔记(5)
php(31)
phpcms二次开发(29)
tools(5)
服务器(13)
命令行(1)
杂项(3)
中间件(2)
随笔档案
(190)
2025年1月(4)
2024年12月(1)
2024年8月(3)
2024年7月(1)
2023年5月(1)
2022年12月(1)
2022年10月(3)
2022年3月(1)
2021年12月(3)
2021年11月(1)
2021年10月(4)
2021年9月(1)
2021年8月(1)
2021年7月(2)
2021年3月(2)
2021年2月(1)
2021年1月(1)
2020年11月(1)
2020年6月(2)
2020年5月(1)
2020年4月(1)
2020年3月(1)
2019年11月(3)
2019年10月(2)
2019年9月(3)
2019年6月(1)
2019年5月(1)
2019年4月(1)
2019年3月(1)
2019年1月(1)
2018年12月(1)
2018年2月(2)
2017年3月(1)
2016年10月(1)
2016年8月(1)
2015年10月(1)
2015年6月(2)
2015年5月(1)
2015年4月(3)
2015年3月(1)
2015年2月(1)
2014年11月(2)
2014年10月(2)
2014年6月(2)
2014年5月(3)
2014年2月(1)
2013年12月(3)
2013年11月(1)
2013年10月(1)
2013年7月(3)
2013年6月(3)
2013年4月(1)
2013年3月(1)
2013年2月(1)
2013年1月(3)
2012年11月(1)
2012年9月(1)
2012年5月(7)
2012年4月(5)
2012年2月(3)
2012年1月(2)
2011年12月(9)
2011年10月(1)
2011年9月(2)
2011年8月(9)
2011年6月(10)
2011年5月(5)
2011年4月(1)
2011年2月(2)
2011年1月(1)
2010年10月(3)
2010年6月(6)
2010年4月(1)
2010年3月(3)
2010年2月(1)
2010年1月(4)
2009年12月(2)
2009年11月(9)
2009年9月(10)
更多
阅读排行榜
1. mysql 用户管理和权限设置(339536)
2. Asp操作Cookies(设置[赋值]、读取、删除[设置过期时间])(24661)
3. Nodejs Redis 全部操作方法(21866)
4. wordpress二次开发随笔 -1(14152)
5. php防注入(12999)
6. jquery 集合操作(11092)
7. phpcms--分页修改(9399)
8. MySQL高效分页解决方案集(转)(8950)
9. js 和后台交互(8927)
10. mysql书籍(8061)
评论排行榜
1. asp.net开发的网站中使用mysql之二查增删改(5)
2. mysql 用户管理和权限设置(3)
3. mysql书籍(3)
4. MySQL高效分页解决方案集(转)(2)
5. flex中使用DataGrid显示xml 之二(2)
点击右上角即可分享
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 通过 API 将Deepseek响应流式内容输出到前端
· AI Agent开发,如何调用三方的API Function,是通过提示词来发起调用的吗