C#访问数据库使用web.config配置连接字串
第一部分:C#访问Oracle数据库使用web.config配置连接字串
访问本地Oracle数据库
<configuration>
<location allowOverride="true">
<appSettings>
<add key="DbConnString" value="data source=TEST;user=TEST;password=TEST;"/>
</appSettings>
</location>
<connectionStrings/>
访问远程Oracle数据库
<configuration>
<location allowOverride="true">
<appSettings>
<add key="DbConnString" value="Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=192.168.1.20)
(PORT=1521)))(CONNECT_DATA=(SERVICE_NAME=TEST)));Persist Security Info=True;User Id=TEST; Password=TEST"/>
</appSettings>
</location>
<connectionStrings/>
第二部分:C#访问SQLServer数据库使用web.config配置连接字串
访问本地SQLServer数据库
<configuration>
<location allowOverride="true">
<appSettings>
<add key="DbConnString" value="Initial Catalog=Test;User ID=sa;pwd=by3g;"/>
</appSettings>
</location>
<connectionStrings/>
访问远程SQLServer数据库
<configuration>
<location allowOverride="true">
<appSettings>
<add key="DbConnString" value="Data Source=192.168.0.20;Initial Catalog=TEST;User ID=sa;pwd=by3g;"/>
</appSettings>
</location>
<connectionStrings/>
第三部分:在C#中调用web.config配置的连接字串
string strSqlConn = System.Configuration.ConfigurationManager.AppSettings["DbConnString"];
SqlConnection conn = new SqlConnection(strSqlConn);