ASP.NET 2.0 遍历配置文件的数据库连接字符串
来源:互联网 发布:三三制战术 知乎 编辑:程序博客网 时间:2024/05/17 22:32
在ASP.NET 2.0中,提供了更方便的配置文件访问的类,具体可以到 System.Configuration 名称空间下进行查看。本文提供一种在开发过程中常用的得到数据库字符串的方法,为方便使用,写成一个方法进行调用:
public string GetConnectionString( string _connectionStringsName )
{
System.Configuration.ConnectionStringSettingsCollection config = System.Configuration.ConfigurationManager.ConnectionStrings;
for (int i = 0 ; i < config.Count ; i++)
{
if (config[i].Name.Equals(_connectionStringsName, StringComparison.OrdinalIgnoreCase))
return config[i].ToString();
}
return String.Empty;
}
{
System.Configuration.ConnectionStringSettingsCollection config = System.Configuration.ConfigurationManager.ConnectionStrings;
for (int i = 0 ; i < config.Count ; i++)
{
if (config[i].Name.Equals(_connectionStringsName, StringComparison.OrdinalIgnoreCase))
return config[i].ToString();
}
return String.Empty;
}
如果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>
<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>
<add name="LocalSqlServer" connectionString="data source=.SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true" providerName="System.Data.SqlClient" />
</connectionStrings>
这就是许多网友在论坛上经常会问:为什么我的程序会调用 SQLEXPRESS 数据库的原因,如果你的数据库配置不正确,或者无法打开时,就会使用 SQLEXPRESS 数据库。
- ASP.NET 2.0 遍历配置文件的数据库连接字符串
- ASP.NET 2.0 遍历配置文件的数据库连接字符串
- ASP.NET 2.0 遍历配置文件的数据库连接字符串
- ASP.NET遍历配置文件的连接字符串
- ASP.NET遍历配置文件的连接字符串
- ASP.NET 遍历配置文件的连接字符串
- ASP.NET遍历配置文件的连接字符串
- ASP.NET数据库连接字符串写在config.web配置文件中
- ASP.NET数据库连接字符串写在config.web配置文件中
- asp.net数据库连接字符串
- asp.net数据库连接字符串
- ASP.NET数据库连接字符串
- ASP.NET中C#的数据库连接字符串
- asp.net中数据库连接字符串的写法
- 加密数据库连接字符串(asp.net 2.0)
- ASP.NET配置文件的连接字符串
- asp.net数据库连接字符串集合
- asp.net数据库连接字符串集合
- GridView控件修改、删除示例(修改含有DropDownList控件)
- 专著于C语言的单元测试工具VcSmith 2.2.3版本发布公告!
- VS2005(c#)项目调试问题解决方案集锦 (转)
- sdddddddddddd
- 弹出窗体的实现
- ASP.NET 2.0 遍历配置文件的数据库连接字符串
- 第20章 Tomcat与Apache SOAP集成
- 摩根大通和摩根斯坦利的关系(转载自人米网)
- 如何学习好 JAVA
- ASP.NET 学习历程 - 清清月儿 .NET万花筒 Asp.net技术 Asp.net教程 Asp.net源码 Asp.net基础 Asp.net控件 Asp.net入门 - CSDNBlog
- [经典]18位身份证源代码
- 滚动条样式控制
- 新的BLOG开张了
- 第24章 在Tomcat中配置SSL(书摘)