当前位置: 首页 > 图文教程 > 网络编程 > ASP.NET > ASP.NET遍历配置文件的连接字符串

ASP.NET
asp.net 动态生成表格
asp.net 程序优化精选
DataGridView自动调整行高和行宽
asp.net+js实现的ajax sugguest搜索提示效果
asp.net 将设有过期策略的项添加到缓存中
asp.net SqlDataAdapter对象使用札记
DataGrid 动态添加模板列 实现代码
asp.net 设置GridView的选中行
the sourcesafe database has been locked by the administrator之解决方法
asp.net 退出登陆(解决退出后点击浏览器后退问题仍然可回到页面问题)
Asp.Net HttpHandler 妙用
ASP.NET 保留文件夹详解
asp.net 中将表单提交到另一页 Code-Behind(代码和html在不同的页面)
SqlDataSource 链接Access 数据
asp.net GridView的删除对话框的两种方法
asp.net 按字节检查包含全半角的文字
asp.net String.IsNullOrEmpty 方法
asp.net System.Net.Mail 发送邮件
c# 读取文件内容存放到int数组 array.txt
asp.net Split分割字符串的方法

ASP.NET遍历配置文件的连接字符串


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-08-14   浏览: 201 ::
收藏到网摘: n/a

在ASP.NET 2.0中,提供了更方便的配置文件访问的类,具体可以到System.Configuration名称空间下进行查看。本文提供一种在开发过程中常用的得到数据库字符串的方法,为方便使用,写成一个方法进行调用:

如果web.config配置如下:

以下为引用的内容:
<connectionStrings> <add name="ConnectionString1" connectionString="Persist Security Info=False;User ID=sa;Password=;Initial Catalog=DataBase1;Server=(local);" providerName="System.Data.SqlClient"/> <add name="ConnectionString2" connectionString="Persist Security Info=False;User ID=sa;Password=;Initial Catalog=DataBase2;Server=(local);" providerName="System.Data.SqlClient"/> </connectionStrings> 

如果写成静态类方法,则可以使用下面的方法进行调用:

string ConnectString = XianhuiMengUtil.GetConnectionString("ConnectionString1"); 

另外,如果在遍历时进行输出,则可以看到多出来一个配置项,那是因为machine.config里已经默认定义理一个数据库连接,内容如下:

以下为引用的内容:
<connectionStrings> <add name="LocalSqlServer" connectionString="data source=.SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename= DataDirectory aspnetdb.mdf;User Instance=true" providerName="System.Data.SqlClient" /> </connectionStrings> 

这就是许多网友在论坛上经常会问:为什么我的程序会调用SQL EXPRESS数据库的原因,如果你的数据库配置不正确,或者无法打开时,就会使用SQL EXPRESS数据库。